HashiCorp Configuration language(HCL)

HashiCorp Configuration language(HCL)

#TerraWeekChallenge DAY 2 Terraform Syntax , Block parameter and arguments ,variable , data types , expressions , .tf files , terraform configuration


HCL - HashiCorp Configuration language

What is HCL HashiCorp Configuration language?

  • The HashiCorp Configuration Language (HCL) is a configuration language authored by HashiCorp. HCL is used with HashiCorp's cloud infrastructure automation tools, such as Terraform.
  • The language was created with the goal of being both human readable and machine friendly.
  • HCL is a JSON compatible language that adds features to help you use the Terraform tool to its highest potential.

Comments are available as single line or multi-line:
Single Line: # or //.
Multi-Line: /* */ (no nesting of block comments).        

  • Learn about HCL blocks, parameters, and arguments

  1. HCL uses block to define resources. HCL files have .tf extension and contain the configuration for Terraform.
  2. After the block type keyword and any labels, the block body is delimited by the { and } characters.
  3. Syntax for block:

type "label_1" "label_2" {
  argument_1 = value_1
  argument_2 = value_2
}         

Here we have block type of resource.

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "PathToTerraformCertInstance"
  }
}        

aws_instance is the first label that points to the type of AWS resource and app_server is the second label that represents the name of the resource.

AWS Provider documentation here.

Terraform blocks types here.

  • Parameters and Arguments:

  1. Blocks can have parameters that define characteristics specific to that block type.
  2. Each block type has its own set of parameters and their required or optional arguments.
  3. Arguments are used to assign values to parameters and provide specific configurations for the block.
  4. Arguments are defined using a key-value pair syntax.

Here’s an example of a simple HCL block with parameters and arguments:

resource "aws_instance" "myec2" {
  ami = " ami-04a0ae173da5807d3"
  instance_type = "t2.micro"
tags = {
    Name = "terraform-instance"
  }
}        

In the above example, we have a block of type aws_instance, representing an EC2 instance in Amazon Web Services (AWS).

Terraform Data Sources here.

  • Understand variables, data types, and expressions in HCL

  1. Variables in HCL allow you to parameterize your Terraform configuration and make it more reusable. You can define variables to represent values that may change across environments or need to be dynamic.
  2. declaration example:

create a variable.tf file and define a variable.

variable "filename" {
  type = string
}        

Here is an example of how to use the filename variable in a main.tf file:

resource "local_file" "example" {
  filename = "${var.filename}"
  content  = "Hello, World!"
}        

we are creating a local_file resource and using the filename variable as the name of the file.


  • Writing Terraform Configurations using HCL Syntax

  1. Create Ec2 instance
  2. Install Terraform using commands here.
  3. Create directory and variable.tf and main.tf file.

sudo su
mkdir terraform
cd terraform/        
variables.tf

main.tf file

main.tf
terraform init
terraform validate        

  • Use the terraform plan command to preview changes before applying them.

terraform plan        

  • Use the terraform apply the command to apply changes to your infrastructure.

terraform apply        


We learned HCL blocks, parameters, and arguments, which help structure our configurations. we also go through various resource types and data sources available in Terraform. we practiced writing Terraform configurations using HCL syntax and tested them with the Terraform.


Happy Learning!!







要查看或添加评论,请登录

Aashish R.的更多文章

  • Serverless on AWS

    Serverless on AWS

    Are you excited to know about serverless architecture!! You can build and run applications without thinking about…

  • TerraWeek Day 7 - Advanced Terraform Topics

    TerraWeek Day 7 - Advanced Terraform Topics

    Welcome to the advanced TerraWeek challenge! In this phase, we will dive into advanced topics that will enhance your…

  • TerraWeek Day 6: Terraform Providers

    TerraWeek Day 6: Terraform Providers

    Introduction In this blog, we will explore the concept of Terraform providers, compare major cloud providers such as…

    2 条评论
  • TerraWeek Day 5

    TerraWeek Day 5

    Task 1: What are modules in Terraform and why do we need modules in Terraform? You already write modules Even when you…

  • Terraweek Day 4 Knowledge about Terraform state, Local and Remote configurations

    Terraweek Day 4 Knowledge about Terraform state, Local and Remote configurations

    Task 1:The importance of Terraform state in managing infrastructure The primary purpose of Terraform state is to store…

  • TerraWeek Day 3

    TerraWeek Day 3

    Task 1:Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google…

    1 条评论
  • Deployment of a Microservices Application on K8s- Do MongoDB App Deployment

    Deployment of a Microservices Application on K8s- Do MongoDB App Deployment

    Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of…

  • How to Install terraform on Ubuntu server.

    How to Install terraform on Ubuntu server.

    What is Terraform? HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem…

    1 条评论
  • Deploying Reddit Copy on Kubernetes Cluster using Ingress.

    Deploying Reddit Copy on Kubernetes Cluster using Ingress.

    Prerequisites: ? EC2 (AMI- Ubuntu, Type- t2.medium) ? Docker ? Minikube ? Kubectl Follow below steps for install all…

    5 条评论

社区洞察

其他会员也浏览了