Day 2: Understanding  Terraform Configuration Language (HCL)

Day 2: Understanding Terraform Configuration Language (HCL)

ยท

4 min read

๐Ÿ‘‰ #Day-2 of TerraWeek Challenge ๐Ÿ‘ˆ

Hello Everyone,
In this blog, we will be discussing terraform syntax, variables in terraform, Datatypes, Expression and configurations using HCL syntax.

๐Ÿ“ HCL Syntax used in Terraform

Let's discuss first terraform syntax.

resource "PROVIDER_TYPE" "NAME" {

[CONFIG .....]

}

e.g.

Let's try to understand with the help of the above syntax.

resource: Each terraform script starts with a resource because each terraform script contains many resources which are going to provision in a particular cloud.

"PROVIDER_TYPE": PROVIDER can be any public provider AWS, Microsft, or Google. So this is the provider name. After that, we need to provide the type. what type of resource we are going to create._TYPE type can be instance etc., so it will look like this "aws_instance".

Logical Name ("web"): Third thing is we need to provide a name for the resource. The name should be unique in the configuration script.

Once we are done with the above things. we need to provide attributes of the resource.

Now let's discuss three important concepts which are used in writing code.

๐Ÿ”นBlocks :

We can write multiple blocks or tasks within the same terraform file. The block starts with opening Curly bracket { and ends with closing curly bracket }

resource "aws_instance" "ec2_example" {

ami = "ami-0767046d1677be5a0"

instance_type = "t2.micro"

}

๐Ÿ”นArgument :

The argument is written within the block between the opening and closing curly bracket { } in a key = value format.

resource "aws_instance" "ec2_example" {
                            ami = "ami-0767046d1677be5a0" 
                            instance_type = "t2.micro"
                                   }

here instance_type is a key (argument) and "t2.micro" is its value.

๐Ÿ”นExpression :

The expression represents value. It can be Numeric, String, Boolean etc., here instance_type is a key (argument) and "t2.micro" is its value**(expression)**.

๐Ÿ“ Variables and Datatype in Terraform

โ–ถ๏ธ Variable

Now let's discuss variables in Terraform. The variable is used to store the value which can be used in terraform configuration file. In the real-time world, The project has more variables, It is difficult to maintain variables in the same terraform script. So we can put the variables in a separate file called variables.tf

โ–ถ๏ธ Types of Datatypes in Terraform

๐Ÿ’Ž Primitive Types: A primitive type is a simple type that isn't made from any other type. All primitive types in Terraform are represented by a type keyword. The available primitive types are:

โœ…string: It represents some text. we can say a collection of words within double quotes. e.g. "terraform"

variable "instance_type" {
   description = "t2.micro"
   type        = string
   default     = "t2.micro"
}

โœ…number: It represents a numeric value. It can be 1,2,3 etc.,

variable "instance_count" {
  description = "EC2 instance count"
  type        = number
  default     = 2
}

โœ…bool: It represents in the form of true or false.

variable "enable_public_ip" {
  description = "Enable public IP address"
  type        = bool
  default     = true
}

๐Ÿ’Ž Complex Types: Complex data types stores multiple values in a single array. There are two categories of complex types: collection types (for grouping similar values), and structural types (for grouping potentially dissimilar values).

๐Ÿ”ตCollection Types: The three kinds of collection types in the Terraform language are:

โœ…list: a sequence of values identified by consecutive whole numbers starting with zero.

variable "user_names" {
  description = "IAM usernames"
  type        = list(string)
  default     = ["AWS", "GCP", "AZURE"]
}

โœ…set: a collection of unique values that do not have any secondary identifiers or ordering.

โœ…map: a collection of values where each is identified by a string label.

variable "dev_env" {
  description = "Development Envoirment"
  type        = map(string)
  default     = {
    project     = "Infra Provision",
    environment = "dev-env"
  }
}

๐Ÿ”ตStructural Types:

โœ…Object: a collection of named attributes that each have their type.

โœ…tuple: a sequence of elements identified by consecutive whole numbers starting with zero, where each element has its type.

๐Ÿ“ Terraform configurations using HCL syntax

Let's understand HCL syntax by writing terraforms configuration files.

First, we will create the main.tf Create vpc and create two public subnet

provider "aws" {
   region     = "eu-central-1"
   access_key = "<INSERT_YOUR_ACCESS_KEY>"
   secret_key = "<INSERT_YOUR_SECRET_KEY>"
}

# Create VPC
resource "aws_vpc" "main" {
  cidr_block                     = "172.16.0.0/16"
}

# Create public subnets1
    resource "aws_subnet" "public1" {
    vpc_id                     = aws_vpc.main.id
    cidr_block                 = "172.16.0.0/24"
    map_public_ip_on_launch    = true
    availability_zone          = "eu-central-1a"

}

# Create public subnet2
    resource "aws_subnet" "public2" {
    vpc_id                     = aws_vpc.main.id
    cidr_block                 = "172.16.1.0/24"
    map_public_ip_on_launch    = true
    availability_zone          = "eu-central-1b"
}

variable.tf

variable "region" {
        default = "eu-central-1"
    }

    variable "vpc_cidr" {
        default = "172.16.0.0/16"
    }

Thank you for reading this blog.Happy Learning!!!!!

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

GitHub Link: https://github.com/Parimal-Pradhan

Great initiative by the #TerraWeek #trainwithshubham community. Thank you Shubham Londhe

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!

ย