๐ Familiarizing with HCL Syntax in Terraform & Tasks.

Hey there! ๐
I'm Pratik R. Mandge, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently on a learning adventure, I'm here to share my journey and Blog's in the world of cloud and DevOps.
๐ ๏ธ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.
๐ Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!
Follow me on LinkedIn: https://www.linkedin.com/in/pratik-mandge363
โ๏ธTask 1: Familiarize yourself with HCL syntax used in Terraform
In Terraform, HashiCorp Configuration Language (HCL) is used to define infrastructure as code. HCL is a declarative language that allows you to describe the desired state of your infrastructure. Here's an overview of HCL blocks, parameters, and arguments, as well as the types of resources and data sources available in Terraform:
๐HCL Blocks
HCL uses blocks to define different components of your infrastructure.
Blocks are defined using curly braces
{}and contain key-value pairs to specify configuration settings.Blocks are used to define resources, data sources, variables, outputs, providers, etc.
๐Parameters and Arguments
Parameters are the settings or configurations you define within a block.
Arguments are the actual values assigned to parameters.
๐Example of a Resource Block in Terraform
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
resourceis the block type.aws_instanceis the resource type.exampleis the resource name (used for referencing this resource in other parts of the configuration).amiandinstance_typeare parameters."ami-0c55b159cbfafe1f0"and"t2.micro"are arguments assigned to the parameters.
๐Types of Resources in Terraform
Compute Resources: Examples include instances, autoscaling groups, and load balancers.
Networking Resources: Examples include VPCs, subnets, route tables, and security groups.
Storage Resources: Examples include EBS volumes and S3 buckets.
Database Resources: Examples include RDS instances and DynamoDB tables.
Provider Resources: Resources provided by the Terraform providers themselves, such as AWS, Azure, Google Cloud, etc.
๐Types of Data Sources in Terraform
AWS Data Sources: Examples include
aws_ami,aws_subnet,aws_vpc, etc.Azure Data Sources: Examples include
azurerm_resource_group,azurerm_virtual_network, etc.Google Cloud Data Sources: Examples include
google_compute_instance,google_compute_network, etc.Other Data Sources: Terraform supports data sources for various other systems, such as DNS records, Docker containers, GitHub repositories, etc.
Data sources allow you to fetch information from existing resources or systems to use in your Terraform configuration.
โ๏ธTask 2: Understand variables, data types, and expressions in HCL
In HashiCorp Configuration Language (HCL), variables are used to define values that can be reused throughout your configuration. HCL supports several data types, including string, number, bool, list, map, and object. You can also use expressions to manipulate and combine values.
๐Define a Variable in variables.tf
Create a file named variables.tf and define a variable. For example, let's define a variable named output_file_path of type string.
variable "output_file_path" {
type = string
default = "output.txt"
}
๐Use the Variable in main.tf
Create a file named main.tf and use the variable in a local_file resource.
resource "local_file" "output_file" {
content = "Hello, world!"
filename = var.output_file_path
}
๐Initialize and Apply the Configuration
Run terraform init to initialize Terraform, run terraform plan to Check the plan and then run terraform apply to apply the configuration.

๐Verify the Output

โ๏ธTask 3: Practice writing Terraform configurations using HCL syntax
To practice writing Terraform configurations using HCL syntax, you can create a simple configuration that uses the docker provider to create a Docker container. Here's a step-by-step guide:
๐Install the Docker
sudo apt-get install docker.io docker-compose -y
๐Add Current User to Docker group
sudo usermod -aG docker $USER
๐Create a terraform file main.tf & Define the Required Providers.
In your main.tf file, define the required providers, such as docker
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.1"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx"
keep_locally = false
}
resource "docker_container" "nginx" { #Create a Docker Container
image = docker_image.nginx.image_id
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
๐Initialize and Apply the Configuration
Run terraform init to initialize Terraform, run terraform plan to Check the plan and then run terraform apply to apply the configuration.
Verify the Container: Use the sudo docker ps command to verify that the container has been created and is running.

๐Then Access the Nginx through Docker
Open Security group -> Edit Inbound rules -> Add 8000 -> Save it.
then, to accessing the Nginx EC2-public-ip:8000

I believe this blog will be really helpful, giving you fresh perspectives and teaching you something new and interesting. ๐
๐ Enjoy learning!




