Terraform Variables,Files & Commands

Terraform Variables,Files & Commands

Learn about Terraform variables, Terraform commands and Terraform Files.

·

4 min read

Terraform Variables

  1. String Variable: Represents a sequence of characters.

  2. Number Variable: Represents a numerical value.

  3. Boolean Variable: Represents a true or false value.

  4. List Variable: Represents an ordered collection of values.

  5. 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

  1. String Variable:

    • variable "instance_type": Defines a string variable for the EC2 instance type.
  2. Number Variable:

    • variable "instance_count": Defines a number variable to specify the number of EC2 instances to create.
  3. Boolean Variable:

    • variable "enable_monitoring": Defines a boolean variable to enable or disable monitoring for the instance.
  4. List Variable:

    • variable "availability_zones": Defines a list of availability zones.
  5. Map Variable:

    • variable "tags": Defines a map of tags to assign to the instance.
  6. Using Variables in main.tf:

    • The instance_type, instance_count, enable_monitoring, availability_zones, and tags variables are referenced using the var.<variable_name> syntax.

    • The count parameter is used to create multiple instances based on the instance_count variable.

    • The availability_zone is selected using the element function to distribute instances across the specified availability zones.

    • The tags are applied to the EC2 instance using the tags 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

  1. terraform init: Initializes a working directory with the necessary configuration files and installs provider plugins.

  2. terraform plan: Creates an execution plan, showing what actions Terraform will take to achieve the desired state.

  3. terraform apply: Applies the changes required to reach the desired state of the configuration.

  4. 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:
  1. 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.

  2. terraform plan:

    • <plan_name>.tfplan (if -out option is used): This binary file contains the execution plan, showing the actions Terraform will take. This file is optional and only created if you specify an output file with -out.
  3. 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.

  4. 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.