Tuesday, February 14, 2023

TERRAFORM VARIABLES

 TERRAFORM VARIABLES




                               WHAT ARE VARIABLES
 
Variables allows you to take away hard coded values within your configuration files in terraform.  Variables in terraform are declared using variable block and can be used to store values like strings, numbers, boolean or list.

Just like in CloudFormation that a reference of variable is "parameter store" 

For example:
"aws_instance" - Is the resource specified 
"instance_count" - Is the variable specified that makes tells us the number of instance being created based on the value of the variable.


resource "aws_instance" {
count = var.instance_count
}

    
TERRAFORM VARIABLES
Below are the different types of variables that Terraform support:

List: 
A list can be a collection of same values ["d", "e", "f" ]      

Number:  
We can say a number is numeric in value like  90 or 2.13    

Boolean:   
This is a value that can either be true or false

Object:
This could be AWS resources such as EC2 instance

Map:
A collection of key pair like specify the value of the key. 


                        LOCAL VALUES
  • Input Variables
  • Output Values

INPUT VARIABLES
Allows you to share modules across different terraform configuration files making your module reusable.


OUTPUT VALUES
The output values similarly shows its return values in a programming language.


MANUAL VALUES: 

Manual Values make the code less flexible, harder to manage and its not scalable. You will be able to take out the hard coded values but terraform will allow you run the values on the CLI. Meaning, when you run terraform plan and apply", terraform will ask you to provide the variables at the level of the terminal. One benefit with manual values is that, IT DOES NOT EXPOSE YOUR SECRET/VALUES. You can share your configuration file to GitHub. 

DEFAULT VALUES:

Terraform identifies the configuration value and it goes in and pick up the value as well as uses the FUNCTION (default argument)

For example:

 To pass default argument, in this case below, the default = 1
 You pass the default function  = value (inside the variable.tf )

LIMITATION OF DEFAULT VARIABLE: 
In most cases, when you are reproducing an infrastructure in another region, you would have to update the file and that could trigger the file due to the mistake. To solve this, you will remove all the values from the variable file and resources and keep it. You have all the variables configuration in a separate file and any future changes can be done in the values and can be updated.  This is when terraform.tfvars comes in.

resource aws_instance
variable "instance_count" {
type = number
default = 1
}



TERRAFORM.TFVARS:
With terraform TFVARS, you have the ability to create a tfvars file that will be used to house all the resource values. At this level you will be providing the specific variable keys from value to append to the resources.

For example:
Each files has ONLY what is stated in values, resources and variable. We are able to establish the files in a dynamic manner into Terraform.tfvars.

TFVARS CONNECTION WITH THE KEYS:

Terraform makes use of Variable reference" to achieve the connection between TFVARS and the KEYS. 

CUSTOM VARIABLES:
These are set of .tfvars files that allow you to pass in values at run time when you apply (terraform apply) in which you specified the custom name.

AUTO.TFVARS:
With this, Terraform automatically loads numbers of variables defined in files as auto.tfvars with the ability to customize the code and automatically pick up the code at run time.  

Lets' gets a few project done on custom-auto.tfvars

STEP 1:
Create a folder, for example called "input-variables" 

STEP 2:
Create another folder, inside input variables for example called "custom-auto-tfvars" 

STEP 3:

Within the 2nd folder, create a file for example called "provider.tf"

terraform {
required_version = "1.3.4"
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.39.0"
}
}
}

provider "aws" {
region = "us-east-1"
profile = "default"
}

STEP 3:
Within the same 2nd folder, create a file for example call  "dev.auto.tfvars"

Dev-vpc-cidr_block = "10.0.0.0/16"
Dev-vpc-instance-tenancy = "default"
Dev-subnet-1-cidr = "10.0.1.0/24"
Dev-subnet-1-availability-zone = "us-east-1a"


STEP 4:
Create another file inside the 2nd folder call "security-group.tf". 

To Note, Instead of making use of Port "443" which requires a certificate, we will make use of Port "80"

The "-1" referenced for egress grants access to everything.  You can also specify with TCP/ UDP for the protocol type. 

resource "aws_security_group" "development-SG" {
name = "development-SG"
vpc_id = aws_vpc.Dev-VPC.id

ingress {
description = "TLS from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}

STEP 5:
Create a file inside custom-auto-tfvars called "variable.tf"

# dev vpc cidr block
variable "Dev-vpc-cidr_block" {
description = "Development vpc cidr block"
type = string
}

# dev vpc instance tenancy
variable "Dev-vpc-instance-tenancy" {
description = "Development vpc instance tenancy "
type = string
}

# dev vpc subnet1 cidr block
variable "Dev-subnet-1-cidr" {
description = "development vpc subnet1 cidr"
type = string
}

# dev vpc subnet1 availability zone
variable "Dev-subnet-1-availability-zone" {
description = "development vpc subnet1 az"
type = string
}

STEP 6:
Create a file inside custom-auto-tfvars called "vpc-network.tf"

# Developement VPC
resource "aws_vpc" "Dev-VPC" {
cidr_block = var.Dev-vpc-cidr_block
instance_tenancy = var.Dev-vpc-instance-tenancy

tags = {
Name = "Dev-VPC"
}
}
# Developement Subnet 1
resource "aws_subnet" "Dev-Subnet-1" {
vpc_id = aws_vpc.Dev-VPC.id
cidr_block = var.Dev-subnet-1-cidr
availability_zone = var.Dev-subnet-1-availability-zone
tags = {
Name = "Dev-Subnet-1"
}

# Development VPC internet Gateway
resource "aws_internet_gateway" "Dev-VPC-IGW" {
vpc_id = aws_vpc.Dev-VPC.id

tags = {
Name = "Dev-VPC-IGW"
}
}

STEP 7:
In this hands-on, we provisioned 5 resources. In your case, you will get lesser resources.



STEP 8:
"cd" into the path (custom-auto-tfvars) where your files are located.

STEP 9:
Run the terraform commands
-Init
-Validate
-plan
-apply
-destroy

STEP 10:
Congratulations, you have successfully used terraform provision variables and values 

NB: If you successfully deploy this hands-on, kindly leave a comment and feedbacks. 😊


Referencing : Terraform- https://registry.terraform.io/
                    Mbandi  Awanmbandi






No comments:

Post a Comment

CONFIGURING A PHISHING CAMPAIGN IN MICROSOFT DEFENDER.

Configuring a phishing campaign in Microsoft Defender (specifically Microsoft Defender for Office 365) involves creating a simulated attack ...