Challenge for the #TerraWeek - Tuesday

Challenge for the #TerraWeek - Tuesday

·

5 min read

Let's start with Day 02 of the TerraWeek Challenge :

Challenge Descriptions :

- Explore the HCL syntax used in Terraform 
- Learn about variables , data types , expressions in HCL 
- Terraform configurations using HCL Syntax

Let's start with the first question :

  • Explore the HCL syntax used in Terraform :

Terraform is an infrastructure as code (IaC) tool that allows you to define and provision infrastructure resources using declarative configuration files. The configuration language used by Terraform is called HashiCorp Configuration Language (HCL). In this response, I will provide an overview of the HCL syntax used in Terraform.

  1. Comments:

    • Single-line comments start with a '#' character.

    • Multi-line comments are enclosed between '/' and '/'.

  2. Blocks:

    • Configuration is organized into blocks, which are defined using braces '{ }'.

    • Each block represents a resource, data source, provider, or module.

    • Block type is specified as the first element inside the braces.

    • Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
  1. Attributes:
  • Inside a block, attributes are defined using the "key = value" syntax.

  • Each attribute is on a separate line.

  • String values can be written without quotes.

  • Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
  1. Variables:
  • Variables allow you to parameterize your configuration.

  • They are defined using the "variable" block.

  • Example:

variable "region" {
  description = "AWS region"
  default     = "us-west-2"
}
  1. Expressions:

    • HCL supports expressions for dynamic values.

    • Expressions are enclosed in "${ }".

    • Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "${var.instance_type}"
}
  1. Functions:

    • HCL provides built-in functions for manipulating values.

    • Functions are called using the "function_name(arguments)" syntax.

    • Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "${lower(var.instance_type)}"
}
  • Variables, data types & Expressions in HCL

we can create variables.tf files which can hold all the variables in a single file ..

String:

  • Represents a sequence of characters.

  • Example:

variable "name" {
  type    = string
  default = "Prithish-Devops"
}

Number:

  • Represents numeric values.

  • Example:

variable "count" {
  type    = number
  default = 5
}

Boolean:

  • Represents a boolean value (true or false).

  • Example:

      variable "enabled" {
        type    = bool
        default = true
      }
    

    List:

    • Represents an ordered collection of elements.

    • Example:

        variable "fruits" {
          type    = list(string)
          default = ["PG", "Welcome", "Devops"]
        }
      

Map:

  • Represents a collection of key-value pairs.

  • Example:

      variable "person" {
        type    = map(string)
        default = {
          name  = "Prithish-DevOps"
          age   = "30"
          email = "pg@gmail.com"
        }
      }
    

Object:

  • Represents a complex data structure combining multiple data types.

  • Example:

      variable "user" {
        type = object({
          name  = string
          age   = number
          email = string
        })
        default = {
          name  = "Prithish-DevOps"
          age   = 30
          email = "pg@gmail.com"
        }
      }
    

These are some of the commonly used data types in Terraform. You can specify the data type for variables using the type attribute within the variable block. It helps in type-checking and ensures that the variable values are used correctly throughout the configuration.

Create a dedicated directory where we can create terraform configuration files.

Use the following command to create a directory and change our present working directory to it.

mkdir terraform_ec2instance
cd terraform_ec2instance/

I have used Visual Studio Code as an editor to write in files, we can use an editor of our choice and copy paste the following configurations to create variables.tf, terraform.tfvars and main.tf

main.tf

Create main.tf which is responsible to create an EC2 on AWS. This main.tf will read values of variables from variables.tf and terraform.tfvars.

code main.tf
provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "us-east-1"
}

resource "aws_instance" "ec2_instance" {
    ami = "${var.ami_id}"
    count = "${var.number_of_instances}"
    subnet_id = "${var.subnet_id}"
    instance_type = "${var.instance_type}"
    key_name = "${var.ami_key_pair_name}"
}

variables.tf

Create variables.tf which contains the declaration and definition of the variables.

variable "access_key" {
        description = "Access key to AWS console"
}
variable "secret_key" {
        description = "Secret key to AWS console"
}


variable "instance_name" {
        description = "Name of the instance to be created"
        default = "TerraWeek"
}

variable "instance_type" {
        default = "t2.micro"
}

variable "subnet_id" {
        description = "The VPC subnet the instance(s) will be created in"
        default = "subnet-07ebbe60"
}

variable "ami_id" {
        description = "The AMI to use"
        default = "ami-09d56f8956ab235b3"
}

variable "number_of_instances" {
        description = "number of instances to be created"
        default = 1
}


variable "ami_key_pair_name" {
        default = "TerraWeek-key"
}

Once variables.tf file is created, We need to change the values assigned to variable. We must change ami_key_pair_name, ami_id and subnet_id as these are specific to the environment.

terraform.tfvars

Create terraform.tfvars which contains the definition of access_key and secret_key variables defined in the above file.

The following keys need to be changed with the keys of our IAM user.

access_key = "Access Key of IAM User"
secret_key = "Secret Key of IAM User"

Creating an EC2 using the Terraform configuration files

terraform init command downloads and installs plugins for providers used within the configuration. In our case it is AWS.

terraform init

terraform plan command is used to see the changes that will take place on the infrastructure.

 terraform plan

terraform apply command will create the resources on the AWS mentioned in the main.tf file. It will be prompted to provide our input to create the resources.

terraform apply

When we execute the above command, we can see that 1 new resource has been added and 0 has been destroyed in the output.

We can go to the AWS EC2 console to verify if the EC2 instance is created or not.

Deleting the created EC2 instance using Terraform

If we no longer require resources that we have created using the configuration mentioned in the main. tf file, we can use the terraform destroy command to delete all those resources.

terraform destroy

Note: Also we can create S3 buckets, create policies and many more using HCL and configuration.

Thank you so much for your support. if you like this challenge please hit the like button and follow me for more content.