TerraWeek Day 2:

TerraWeek Day 2:

Task 1: Familiarize yourself with HCL syntax used in Terraform

  • What is HCL (Hashicorp Configuration Language)?

    Hashicorp Configuration Language, this low-level syntax of the Terraform language is defined in terms of syntax is called HCL, which is also used by the configuration languages in other applications and in particular other Hashicorp products. Syntax-

      <block> <parameters> {
          key1 = value1
          key2 = value2
      }
    
  • Learn about HCL blocks, parameters, and arguments.

    HCL blocks - A block is a container for other content and An argument assigns a value to a particular name. Blocks define different parts of your infrastructure. The basic structure of a block consists of a block type, an optional label, and a set of parameters enclosed in braces.

    Example: Here Block name is resource and resource type is local_file.

      resource "local_file" "animal" {
        filename          = "/root/animal.txt"
        content = "We love animals!"
      }
    

    Parameters- Parameters are key-value pairs within a block.

    Arguments- Arguments are specific values assigned to parameters.

Explore the different types of resources and data sources available in Terraform.

The type of resources that you can create with your Terraform scripts are defined by the providers. A resource however is an infrastructure component that you plan to deploy using your Terraform Script. A few examples of resources could be Virtual Networks, Virtual Machines, Load Balancers, Public IPs and many more.

Data sources- Terraform data sources let you dynamically fetch data from APIs or other Terraform state backends. Examples of data sources include machine image IDs from a cloud provider or Terraform outputs from other configurations. Data sources make your configuration more flexible and dynamic and let you reference values from other configurations, helping you scope your configuration while still referencing any dependent resource attributes.

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

  • Creating a Variables File (variables.tf): To define variables in Terraform, create a variables.tf file. Here's an example of defining a variable:
hclCopy codevariable "instance_count" {
  description = "Number of instances to create"
  type        = number
  default     = 2
}
  • The Terraform language uses the following Data types for its values:

    • string: a sequence of Unicode characters representing some text, like "hello".

    • number: a numeric value. The number type can represent both whole numbers like 15 and fractional values like 6.283185.

    • bool: a boolean value, either true or false. bool values can be used in conditional logic.

    • list (or tuple): a sequence of values, like ["us-west-1a", "us-west-1c"]. Identify elements in a list with consecutive whole numbers, starting with zero.

    • set: a collection of unique values that do not have any secondary identifiers or ordering.

    • map (or object): a group of values identified by named labels, like {name = "Mabel", age = 52}.

  • Expressions- Expressions refer to or compute values within a configuration. The simplest expressions are just literal values, like "hello" or 5, but the Terraform language also allows more complex expressions such as references to data exported by resources, arithmetic, conditional evaluation, and several built-in functions.

Task 3: Practice writing Terraform configurations using HCL syntax

  1. Adding Required Providers: To use specific providers (e.g., AWS, Docker), add a required_providers block to your configuration:

terraform {

    required_providers {

    docker = {
    source = "kreuzwerker/docker"
    version = "3.0.2"
        }
    }
}
  1. Testing Your Configuration:

    • Initialize your Terraform project: terraform init

    • Apply your configuration: terraform apply

  2. Making Adjustments:

    • Make any necessary adjustments to your configuration based on Terraform's feedback.

    • Use terraform plan to see the changes Terraform will apply.