Day 2 of #TerraWeek - Terraform Configuration Language (HCL)

Day 2 of #TerraWeek - Terraform Configuration Language (HCL)

What is HCL?

Terraform is an infrastructure as code(IaC) tool developed by HashiCorp.It uses HashiCorp Configuration Language(HCL) as its native configuration language. The language was created to be both human and machine friendly. With Terraform, you can define and provision infrastructure resources across various cloud providers,on-premises environments, and other service providers.

HCL syntax:

  1. Resource Blocks: Blocks define resources, data sources, providers, and other constructs in Terraform. A resource block represents a particular resource type, such as an AWS Ec2 instance.

  2. Variables: Variables in HCL are used to parameterize your Terraform configurations and make them more flexible. You can define variables using the variable block within your Terraform configuration file

    1. Comments:

Single-line comments start with the ''#" character.

Multiline comments start with "/" and end with "/".

Variables, Data Types, and Operators in HCL

Variables:

To define a variable in HCL, create a variables.tf file and add the following code:

DataTypes:

HCL supports various data types, including strings, numbers, booleans, lists, and maps.

String: Represents a sequence of characters. Strings are typically enclosed in double quotes (" ") or single quotes (' ').

Boolean: Represents a logical value that can be either true or false.

Number: Represents numeric values, including integers and floating-point numbers.Example: 77

List: Represents an ordered collection of values. Lists are defined using square brackets ([]) and can contain elements of any data type. Example: [99,33,44], ["white", "red", "black"]

Map: Represents a collection of key-value pairs. Maps are defined using curly braces ({}) and can have string keys and values of any data type. Example: { "name": "neha", "age": 48, "city": "Delhi" }

  • Expressions :

    HCL allows you to use expressions to dynamically compute values and perform operations. Expressions are enclosed within ${}.

    Operators:
    In HashiCorp Configuration Language (HCL), there are various operators available for performing operations and comparisons. Here are some commonly used operators in HCL:

  • +, -, *, /: arithmetic operators for addition, subtraction, multiplication, and division

  • &&, ||, !: logical operators for AND, OR, and NOT operations

  • \==, !=, <, \>, <=, \>=: comparison operators for equality and inequality, and less than or greater than comparisons.

    Writing Terraform Configurations in HCL:

    Now that we have covered variables, data types, and expressions, let's see how we can write Terraform configurations using HCL. Here's a sample configuration for launching an AWS EC2 instance:

  • To execte main.tf file follow below commands:

  • terraform init:

    The terraform init command initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.

  • terraform plan:

    Reads the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.Compares the current configuration to the prior state and noting any differences.

  • terraform apply:

    Terraform apply command executes the actions proposed in a Terraform plan.

Thank you for reading!!!!