Skip to main content

Command Palette

Search for a command to run...

Day 63/64 - Terraform Variables and Terraform with AWS

Updated
3 min read

Task-01: Creating a Local File with Terraform

Create a file named create_file.tf with the following content:

resource "local_file" "devops" {
  filename = var.filename
  content  = var.content
}

Next, create a file named variables.tf with the following content:

variable "filename" {
  type    = string
  default = "example.txt"
}

variable "content" {
  type    = string
  default = "This is an example file."
}

Task-02: Use terraform to demonstrate usage of List, Set and Object datatypes

List Data Type:

A list is an ordered collection of elements, where each element can be of any data type. Let's create a list variable named tags as an example:

variable "tags" {
  type    = list(string)
  default = ["tag1", "tag2", "tag3"]
}

In the above code, we define the tags variable as a list of strings with a default value of ["tag1", "tag2", "tag3"]. You can access individual elements of the list using indexing, such as var.tags[0] to retrieve the first element.

  1. A set is an unordered collection of unique elements. It ensures that each element appears only once. Let's create a set variable named users:
variable "users" {
  type    = set(string)
  default = ["user1", "user2", "user3"]
}

In the above example, we define a variable users as a set of strings. It has a default value of ["user1", "user2", "user3"]. Sets automatically eliminate duplicate elements, ensuring that each element appears only once.

  1. An object is a collection of key-value pairs, where each key is a string and each value can be of any data type. Let's create an object variable named instance:
variable "instance" {
  type = object({
    name     = string
    size     = string
    region   = string
    tags     = list(string)
    enabled  = bool
  })
  default = {
    name     = "web-server"
    size     = "t2.micro"
    region   = "us-west-2"
    tags     = ["web", "prod"]
    enabled  = true
  }
}

Practical Example:

Now, let's put these data types into practice by creating an EC2 instance using the AWS provider. We'll use the object data type to define the configuration details of the EC2 instance, including the AMI, instance type, subnet IDs, tags, and security groups. Follow the steps below:

1. Create a file named variables.tf with the following content:

variable "ec2_instance" {
  type = object({
    ami           = string
    instance_type = string
    subnet_ids    = list(string)
    tags          = map(string)
    security_groups = set(string)
  })
  default = {
    ami           = "ami-053b0d53c279acc90"
    instance_type = "t2.micro"
    subnet_ids    = ["subnet-0da4178ade750f5e2", "subnet-070fd0b5c4195efbd"]
    tags          = {
      Name        = "MyEC2Instance"
      Environment = "Production"
    }
    security_groups = ["sg-045f7a9bc0af55aa1", "sg-09b8101c3ee5cbac8"]
  }
}
  1. Create a file named provider.tf with the following content:
provider "aws" {
  region = "us-east-1"
}
  1. Create a file named create_instance.tf with the following content:
resource "aws_instance" "ec2_instance" {
  ami           = var.ec2_instance.ami
  instance_type = var.ec2_instance.instance_type
  subnet_id     = var.ec2_instance.subnet_ids[0]
  tags          = var.ec2_instance.tags
  vpc_security_group_ids = var.ec2_instance.security_groups
}

Before running terraform plan and terraform apply, ensure that you have properly configured the AWS CLI with the necessary access and secret keys for EC2.

Now run terraform plan and terraform apply

That was all for today. If you have queries/suggestions please write in comment. See you another day with another challenge

More from this blog

Ajay Patel

116 posts