Terraweek Day 2: HashiCorp Configuration Language (HCL)

Terraweek Day 2: HashiCorp Configuration Language (HCL)

Introduction

Welcome back to Terraweek, our week-long journey into the world of Terraform! Yesterday, we laid the foundation by installing Terraform and setting up our development environment. Today, we'll dive deeper into Terraform's core concepts and how to work with them effectively.

1. Learn about HCL Blocks, Parameters, and Arguments

Terraform talks in HashiCorp Configuration Language (HCL), a language that's not just developer-friendly but also friendly to newbies like you! 🤝

Syntax-

  <block> <parameters> {
      key1 = value1
      key2 = value2
  }
  • Blocks: Think of blocks as Lego pieces. You use them to build stuff. In Terraform, resource blocks are your go-to for creating things like servers or databases.

  • Parameters: Parameters are like the settings on your phone. Each block has these settings that you can customize. For example, the name setting tells Terraform what to call your resource.

  • Arguments: Arguments are the actual values you give to the parameters. So, if name is a parameter, you'd use an argument like "MyServer" to name your resource.

Got it? Blocks build stuff, parameters are like settings, and arguments are the values you use for those settings. Easy-peasy! 🧩

2. Explore the Different Types of Resources and Data Sources Available in Terraform

Terraform provides a wide range of resources and data sources for various cloud providers, services, and components. Resources are used to create and manage infrastructure elements, while data sources allow you to query existing resources for information.

Resources: Some common resource types include aws_instance for creating AWS EC2 instances, google_compute_instance for Google Cloud VMs, and azurerm_virtual_network for Azure virtual networks.

Data Sources: Data sources might include aws_ami to retrieve information about AWS machine images or google_container_cluster to get details about Google Kubernetes Engine clusters.

Exploring the available resources and data sources will help you choose the right components for your infrastructure.

3. Create a variables.tf File and Define a Variable

Variables in Terraform enable you to parameterize your configurations, making them more flexible and reusable. To create a variable, you'll typically define it in a variables.tf file. Here's an example of how you can define a variable:

variable "instance_count" {
  description = "The number of instances to create."
  type        = number
  default     = 2
}

In this example, we've created a variable named instance_count with a default value of 2. You can use this variable to customize your Terraform configurations.

4. Use the Variable in a main.tf File to Create a "local_file" Resource

Now that we have a variable, let's put it to use. In your main.tf file, you can reference the variable to create a local_file resource:

resource "local_file" "example" {
  filename = "/path/to/your/file"
  content  = "This is an example file with ${var.instance_count} instances."
}

Here, we're creating a file with content that depends on the instance_count variable. Terraform will replace ${var.instance_count} with the actual value.

5. Add Required Providers to Your Configuration, Such as AWS

Terraform uses providers to interact with different cloud platforms and services. Depending on your project's requirements, you need to specify the required providers in your configuration. For example, if you're working with AWS, you can add the AWS provider like this:

provider "aws" {
  region = "us-west-2"
}

Similarly, if you need to work with Docker, you would add the Docker provider configuration.

6. Test Your Configuration Using the Terraform CLI and Make Any Necessary Adjustments

Let’s write a Terraform configuration for Docker using HCL syntax.

  1. Docker

    create a folder :

    Create a folder for docker project. docker should be installed Also, add the required permission to docker.

    mkdir terraform-docker

    Adding Required Providers:

    add a required_providers block to your configuration. usually, we write providers in terraform.tf

     terraform {
    
         required_providers {
    
         docker = {
         source = "kreuzwerker/docker"
         version = "3.0.2"
             }
         }
     }
    

    Testing Your Configuration:

    initialize your Terraform project: terraform init

    Then, we configure the provider that we added in the terraform.tf. This is wriitern in main.tf

    we want to create an image and create a container .

     provider "docker" {}
    
     resource "docker_image" "nginx-img" {
             name = "nginx:latest"
             keep_locally = false
     }
     resource "docker_container" "nginxc" {
             name = "my-nginxc"
             image = docker_image.nginx-img.name
             ports  {
             internal = 80
             external = 80
             }
     }
    

    We created a Docker container called my-nginxc with the latest version of the nginx image and mapping port 80 to port 80.

    Making Adjustments:

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

    Apply your configuration: terraform apply

This sequence - terraform init terraform plan terraform apply

initializes Terraform, checks your configuration for errors, and then applies it to create or update your resources. Make any necessary adjustments based on the feedback from these commands.

docker container created :

command : docker ps

We can also access the nginx using port 80:

Conclusion

That wraps up our second day of Terraweek! We've covered the essential building blocks of Terraform, explored resource types, created variables, added providers, and tested our configuration. Tomorrow, we'll take a closer look at modules and how they can help you manage and modularize your infrastructure code. Stay tuned!