Terraform Variables
What are terraform variables? | How these variables help in managing the changes in the values.
A Terraform variable is a way to define or allow you to set and manage values that can change, making your infrastructure code more flexible and reusable. They help customize and scale your deployments easily by allowing you to define values like secrets, AMI tags and other parameters outside the main script.
When to use Terraform variables:
When you have secrets or any confidential data that you want to move out of your script.
When there are values that may change based on requirements, such as AMI tags.
When you want to reuse the code.
Example:
Create these files,
provider.tf
This is an example file of defining an AWS provider in Terraform, setting the region based on the REGION
variable.
provider "aws" {
region = var.REGION
}
var.tf
This is an example file of defining Terraform variables. It includes three variables:
REGION
: A variable with a default value of "us-east-1".ZONE1
: A variable with a default value of "us-east-2a".AMIS
: A map variable with different AMI IDs for different regions, with default values for "us-east-2" and "us-east-1".
variable "REGION" {
default = "us-east-1"
}
variable "ZONE1" {
default = "us-east-2a"
}
variable "AMIS" {
type = map(any)
default = {
us-east-2 = "ami-03657b56516ab7912"
us-east-1 = "ami-0b72821e2f351e396"
}
}
instance.tf
This is an example file of declaring an AWS EC2 instance in Terraform.
ami
: The Amazon Machine Image (AMI) ID, is dynamically selected based on theREGION
variable.instance_type
: The type of instance, set to "t2.micro".availability_zone
: The availability zone, set to the value of theZONE1
variable.key_name
: The name of the key pair for the instance, set to "key-aws".vpc_security_group_ids
: A list of security group IDs.tags
: A set of tags assigned to the instance, including "Name" set to "sample-Instance" and "Project" set to "Terraform".
resource "aws_instance" "exercise2" {
ami = var.AMIS[var.REGION]
instance_type = "t2.micro"
availability_zone = "us-east-2a"
key_name = "Your Key pair"
vpc_security_group_ids = ["Security group id"]
tags = {
Name = "sample-Instance"
Project = "Terraform"
}
}
terraform fmt
This command formats the Terraform configuration files in the current directory to a canonical format and style. It ensures that the code is properly indented and organized, making it easier to read and maintain.
terraform fmt
terraform init
This command initializes a Terraform working directory. It prepares the directory by downloading and installing the necessary provider plugins and setting up the backend configuration. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control.
terraform init
terraform validate
This command checks the syntax and validity of the Terraform configuration files in the current directory. It ensures that the configuration is syntactically correct and that all required arguments are specified. This is a useful step to catch errors before applying the configuration.
terraform validate
terraform plan
This command creates an execution plan, showing what actions Terraform will take to achieve the desired state defined in the configuration files. It compares the current state with the desired state and lists the changes that will be made such as creating, updating, or deleting resources. This allows you to review the proposed changes before applying them.
terraform plan
terraform apply
This command applies the changes required to reach the desired state of the configuration as defined by the Terraform files. It creates, updates or deletes infrastructure resources to match the configuration. Before making any changes, Terraform will show a plan of the actions it will take and ask for your confirmation.
terraform apply
terraform destroy
This command is used to destroy the infrastructure managed by Terraform. It will remove all the resources defined in your Terraform configuration files. Before making any changes, Terraform will show a plan of the actions it will take and ask for your confirmation. This is useful for tearing down environments or cleaning up resources that are no longer needed.
terraform destroy
If you want to change the AMI of an existing instance, the instance will be deleted and recreated with the AMI updated in the instance.tf
file by var.tf
So now you will be editing the var.tf
file and instead of the current AMI, you will be adding the new one.
For example: Here my current AMI is of Amazon Linux and now I want to change my instance to an Ubuntu AMI.
Go to var.tf
and for our us-east-1a
region, change the current AMI ID to the Ubuntu AMI ID.
Then you can validate and apply.
Current AMI of my instance:
Updated AMI ID of my instance:
The old instance of Linux AMI will be deleted and a new instance will be created with Ubuntu AMI.
This is how Terraform variables are used. They simplify the process by allowing you to change the value of a variable whenever needed.
https://github.com/Ragavi04P/Terraform-Practice/blob/main/exercise2/Exercise2.pdf