TERRAFORM, Day - 2

TERRAFORM, Day - 2

TERRAFORM CONFIGURATION LANGUAGE [HCL]

Task 1: Familiarize yourself with the HCL syntax used in Terraform

HCL (HashiCorp Configuration Language) is the language used in Terraform to write infrastructure configurations. Here are some key points to familiarize yourself with HCL syntax:

  • Block Syntax: HCL uses a block-based syntax, where configurations are written as blocks of nested key-value pairs. Blocks are defined using curly braces {} and contain multiple attributes.

  • Attribute Assignment: Attributes are assigned using the = symbol within blocks. Example:

      resource "aws_instance" "example" {
        ami           = "ami-0c94855ba95c71c99"
        instance_type = "t2.micro"
      }
    
  • String Values: String values are typically enclosed in double quotes.
    Example:

      name = "my-instance"
    
  • Numeric Values: Numeric values are written as plain numbers without quotes. Example:

      count = 3
    
  • Comments: Single-line comments start with a # symbol.

    Example:

      # This is a comment
    
  • Lists: Lists are defined using square brackets [] and can contain multiple values separated by commas.
    Example:

      security_groups = ["sg-12345678", "sg-87654321"]
    
  • Maps: Maps, or key-value pairs, are defined using curly braces {}. Keys and values are separated by an equal sign =.
    Example:

      tags = {
        key1 = "value1"
        key2 = "value2"
      }
    
  • Interpolation: Interpolation allows you to reference variables or expressions within strings or attribute values using the ${} syntax.
    Example:

      ami = "ami-${var.ami_suffix}"
    
  1. Learn about HCL blocks, parameters, and arguments

    In HCL (HashiCorp Configuration Language) used in Terraform, there are three key concepts: blocks, parameters, and arguments. Here's an overview of each:

    • Blocks: Blocks are the fundamental building blocks of HCL configurations. They define a logical section of configuration and are defined using curly braces {}. Blocks represent different types of resources, data sources, provisioners, and other constructs in Terraform. Each block has a block type and a block label.

      Example:

        resource "aws_instance" "example" {
          ...
        }
      
    • Parameters: Parameters are the key-value pairs within a block that define the properties and settings for that block. Parameters are specific to the type of block being used. They provide the necessary information to configure the resource or construct represented by the block. For example, in the aws_instance block, parameters can include attributes like ami, instance_type, subnet_id, etc.
      Example:

        resource "aws_instance" "example" {
          ami           = "ami-0c94855ba95c71c99"
          instance_type = var.instance_type
          subnet_id     = aws_subnet.example.id
        }
      
    • Arguments: Arguments are the values assigned to the parameters within a block. They provide the actual values for the properties defined by the parameters. Arguments can be fixed values, variables, or expressions. They determine the specific configuration values for the block.
      Example:

        resource "aws_instance" "example" {
          ami           = "ami-0c94855ba95c71c99"
          instance_type = "t2.micro"
          subnet_id     = aws_subnet.example.id
        }
      
  1. Explore the different types of resources and data sources available in Terraform

    RESOURCES

    • Compute Resources: Provision and manage virtual machines or instances in the cloud.

    • Networking Resources: Define and manage networking components for your infrastructure, such as virtual networks, subnets, and security groups.

    • Storage Resources: Manage storage-related components like object storage, block storage, and file storage services.

    • Database Resources: Provision and manage databases, including relational databases, NoSQL databases, and data warehousing services.

    • Security Resources: Define and manage security-related components, such as access control policies, encryption keys, and security groups, to secure your infrastructure.

DATA SOURCES

  • AWS Data Sources: Retrieve information from existing AWS resources, such as EC2 instances or S3 buckets, to be used within Terraform configurations.

  • GCP Data Sources: Access data from Google Cloud Platform (GCP) resources like Compute Engine instances or Google Cloud Storage buckets for use in Terraform configurations.

  • Azure Data Sources: Fetch data from Azure resources, such as virtual machines or storage accounts, to incorporate that information into Terraform configurations.

  • DNS Data Sources: Query DNS servers to obtain information about DNS records, such as IP addresses, and use them within Terraform configurations.

  • Template Data Sources: Generate dynamic configuration data using templates, enabling you to transform or process data from various sources before using it in Terraform configurations.

  • File Data Sources: Import data from files on the local system or from remote file sources, allowing you to use file content or attributes within Terraform configurations.

  • External Data Sources: Interact with external systems or APIs to retrieve data dynamically and integrate it into Terraform configurations.

  • Null Data Sources: Create a placeholder data source that can be used to provide static or hardcoded values within Terraform configurations.


Task 2: Understand variables, data types, and expressions in HCL

Variables: Variables in HCL allow you to define and reference values that can be used throughout your Terraform configuration. They provide a way to make your configurations more dynamic and flexible. Variables can be declared using the variable block and assigned values using different methods like default values or input from command-line flags or environment variables.

Data Types: HCL supports various data types, including:

String: Represents a sequence of characters and is enclosed in double quotes.
Number: Represents numerical values, either integers or floating-point numbers.
Boolean: Represents true or false values.
List: Represents an ordered collection of values enclosed in square brackets [].
Map: Represents a collection of key-value pairs enclosed in curly braces {}.

Expressions: Expressions in HCL allow you to perform computations, transformations, and conditionals within your Terraform configurations. They can be used for manipulating variables, constructing values, and making decisions. HCL supports a range of operators and functions to work with expressions.

Here's an example that showcases variables, data types, and expressions in HCL:

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

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95c71c99"
  instance_type = var.instance_type
  subnet_id     = aws_subnet.example.id

  tags = {
    Name        = "Example Instance"
    Environment = var.environment
  }
}
  1. Create a variable.tf file and define a variable

  2. Use the variable in a main.tf file to create a "local_file" resource


Task 3: Practice writing Terraform configurations using HCL syntax

  1. Add required_providers to your configuration, such as Docker or AWS

  2. Test your configuration using the Terraform CLI and make any necessary adjustments

    Initialize: Use terraform init to initialize the working directory and download necessary dependencies.

    Validate: Run terraform validate to check the syntax and validate the Terraform configuration files.

    Plan: Execute terraform plan to create an execution plan and preview the changes to the infrastructure.

    Apply: Use terraform apply to apply the changes and provision the infrastructure

    Now let's check in the browser, whether our nginx is running or not :)

    SUCCESS!

Thank you so much for reading.

Follow me on LinkedIn to see interesting posts like this : )

Linkedin