Unleashing the Power of Infrastructure as Code: Dive into the World of Terraform HCL

Unleashing the Power of Infrastructure as Code: Dive into the World of Terraform HCL

·

4 min read

What is HashiCorp Configuration Language (HCL):

HashiCorp Configuration Language (HCL) is a simple and intuitive language designed by HashiCorp for writing infrastructure configuration code. It serves as a declarative language used in tools such as Terraform, allowing users to define and manage their infrastructure as code.

In simpler terms, HCL provides a way for developers and system administrators to describe the desired state of their infrastructure using a human-readable syntax. Instead of manually configuring servers, networks, and other resources, HCL allows you to define them programmatically. This means you can specify how your infrastructure should be set up and maintained in a consistent and repeatable manner.

HCL's syntax is designed to be clear and concise, making it easy to read and write. It supports variables, data types, conditionals, and loops, allowing you to create reusable and dynamic configurations. HCL files are organized into blocks and attributes, which provide a structured way to define and configure resources.

By using HCL and tools like Terraform, you can version control your infrastructure code, collaborate with team members, and easily deploy and manage your infrastructure across multiple cloud providers or on-premises environments. HCL helps simplify the process of provisioning and managing complex infrastructure, making it a powerful tool for DevOps and infrastructure automation.

Exploring HCL Code Examples:

1- Create a main.tf (terraform file) to write syntax in HCL to create a file in Local Folder or Directory. And run the command in the terminal "terraform init" and "terraform apply" and it will create a file name "practice.txt"

2- To change the file name we just need to edit the syntax and run the command terraform apply in the terminal. The file will change from "practice.txt" to "practice_new.txt".

3- We can put syntax to create a random string whose length is put as per requirement. And run the "terraform apply" command to execute, it will display some random string in the terminal

4- Add syntax to output the random string

5- Create another folder for docker practice - here we, add syntax for providers - docker, resource - docker image, and another resource - docker container. And run the command - terraform validate to validate the syntax for any errors then, terraform init and terraform apply.

In the terminal run the command sudo docker ps to check any container built and running

6- As the nginx container is built into the local system we can, we can open the nginx page with our local IP address.

7- To delete all the services or resources created we can a simple command " terraform destroy" and it will delete every resource created in a single command.

8- As we can see there are no docker images and any docker container and executing "terraform destroy" command

Lets integrate Terraform with AWS:

1- First, we have to create an IAM USER, So that we can connect our system with AWS through CLI

2- Give the necessary permissions, here we will just give an administration access

3- For the Access key select CLI and download the access key and connect your system with the access key from the terminal with AWS configure command

4- Created another folder for AWS practice, here we create a "main.tf" file where we write syntax for - terraform required providers, provider, and region, a resource for creating an S3 bucket, a resource for creating aws EC2 instances

5- In order to execute the code we need to run commands in the terminal which are, yes you guessed it right

1- terraform init (it will be initialized)

2- terraform plan (this gives you the plan or a map of what will be executed by the code that we have written)

3- terraform apply (it will execute the code and create the service that has been given)

6- S3 bucket is created in AWS

7- And also EC2 instance

8- To add an EC2 instance replica just need to add "count" syntax in the code and run the terraform apply command

9- Created another instance

10- Now, In order to delete all the resources that we have created we just have to run a simple command - "terraform destroy".

11- Our EC2 instance has been terminated.

12- And also Our S3 bucket is deleted

That's it all the above was not possible without Terraform which makes it easy to have control over infrastructure with code with ease. It's an amazing tool to learn which reduces manual efforts and saves time along with that gives much control over your resources.

Thank you for reading the blog your readership means a lot which gives me support and motivation to learn and bring more content.