Terraform Variables,Files & Commands
Learn about Terraform variables, Terraform commands and Terraform Files.
Terraform Variables
String Variable: Represents a sequence of characters.
Number Variable: Represents a numerical value.
Boolean Variable: Represents a true or false value.
List Variable: Represents an ordered collection of values.
Map Variable: Represents a collection of key-value pairs.
We'll create two files: main.tf
and variables.tf
.
variables.tf
This file defines the variables that will be used in the main.tf
file.
# String variable
variable "instance_type" {
description = "Instance type for the EC2 instance"
type = string
default = "t2.micro"
}
# Number variable
variable "instance_count" {
description = "Number of instances to create"
type = number
default = 1
}
# Boolean variable
variable "enable_monitoring" {
description = "Enable monitoring for the instance"
type = bool
default = true
}
# List variable
variable "availability_zones" {
description = "List of availability zones"
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}
# Map variable
variable "tags" {
description = "Tags for the instance"
type = map(string)
default = {
Name = "MyWebServer"
Environment = "Dev"
}
}
main.tf
This file uses the variables defined in variables.tf
to create an AWS EC2 instance.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "myWebServer" {
ami = "ami-0c55b159cbfafe1f0" # Replace with a valid AMI ID
instance_type = var.instance_type
count = var.instance_count
monitoring = var.enable_monitoring
availability_zone = element(var.availability_zones, count.index % length(var.availability_zones))
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
output "instance_ids" {
value = aws_instance.myWebServer[*].id
}
Explanation
String Variable:
variable "instance_type"
: Defines a string variable for the EC2 instance type.
Number Variable:
variable "instance_count"
: Defines a number variable to specify the number of EC2 instances to create.
Boolean Variable:
variable "enable_monitoring"
: Defines a boolean variable to enable or disable monitoring for the instance.
List Variable:
variable "availability_zones"
: Defines a list of availability zones.
Map Variable:
variable "tags"
: Defines a map of tags to assign to the instance.
Using Variables in
main.tf
:The
instance_type
,instance_count
,enable_monitoring
,availability_zones
, andtags
variables are referenced using thevar.<variable_name>
syntax.The
count
parameter is used to create multiple instances based on theinstance_count
variable.The
availability_zone
is selected using theelement
function to distribute instances across the specified availability zones.The
tags
are applied to the EC2 instance using thetags
parameter.The
output
block is used to output the instance IDs after creation.
With these files, you can explain and demonstrate how different types of variables are used in Terraform to create and manage AWS resources.
Terraform command
terraform init:
Initializes a working directory with the necessary configuration files and installs provider plugins.terraform plan:
Creates an execution plan, showing what actions Terraform will take to achieve the desired state.terraform apply:
Applies the changes required to reach the desired state of the configuration.terraform destroy:
Destroys all the resources managed by the configuration, returning the infrastructure to its initial state.
Terraform Files
When you run Terraform commands like terraform init
, terraform plan
, terraform apply
, and terraform destroy
, Terraform creates and updates various files to manage the state and execution of your infrastructure. Here are the key files involved:
terraform init:
.terraform/
directory: This directory contains configuration files and modules needed by Terraform, including provider plugins..terraform.lock.hcl
: This file contains the dependency lock information for the providers, ensuring consistent provider versions.
terraform plan:
<plan_name>.tfplan
(if-out
option is used): This binary file contains theexecution plan
, showing the actions Terraform will take. This file is optional and only created if you specify an output file with-out
.
terraform apply:
terraform.tfstate
: This JSON file maintains the current state of the managed infrastructure, mapping the configuration to real-world resources.terraform.tfstate.backup
: This file is a backup of the previous state, created before each new state is written.
terraform destroy:
terraform.tfstate
: The state file is updated to reflect the removal of resources.terraform.tfstate.backup
: A backup of the state file before resources were destroyed.
These files ensure that Terraform can track the state of your infrastructure, plan changes accurately, and apply or destroy resources reliably.