Day 62: Terraform and Docker.

Day 62: Terraform and Docker.

ยท

4 min read

What are Blocks in Terraform?

In Terraform, "blocks" refer to the fundamental structural elements used to define configuration. Blocks are used to create and configure various resources and settings within your infrastructure-as-code (IAC) configuration files. Terraform uses a configuration language to define infrastructure, and this language is organized into blocks, each with its own specific purpose.

Here are some common types of blocks you'll encounter in Terraform configurations:

  1. Provider Block: This block specifies the cloud or infrastructure provider you intend to use, such as AWS, Azure, Google Cloud, or others. It typically includes authentication information and configurations for the chosen provider.

    Example:

     hclCopy codeprovider "aws" {
       region = "us-west-2"
     }
    
  2. Resource Block: The resource block defines a particular resource that Terraform should manage. Resources can be virtual machines, databases, load balancers, and more. Each resource block specifies the type of resource, its attributes, and how it should be configured.

    Example:

     hclCopy coderesource "aws_instance" "example" {
       ami           = "ami-0c55b159cbfafe1f0"
       instance_type = "t2.micro"
     }
    
  3. Data Block: Data blocks are used to fetch information from the provider or external sources. You can use data blocks to retrieve information about existing resources, which can be used in other parts of your configuration.

    Example:

     hclCopy codedata "aws_ami" "example" {
       most_recent = true
    
       filter {
         name   = "name"
         values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
       }
     }
    
  4. Variable Block: Variable blocks define input variables that allow you to parameterize your configurations. This enables you to reuse and customize your Terraform code for different environments or use cases.

    Example:

     hclCopy codevariable "aws_region" {
       description = "The AWS region to deploy resources."
       type        = string
       default     = "us-west-2"
     }
    
  5. Output Block: Output blocks define values that should be made accessible outside of the Terraform configuration. This is useful for exposing information that may be needed by other parts of your infrastructure or by external scripts.

    Example:

     hclCopy codeoutput "instance_id" {
       value = aws_instance.example.id
     }
    

What are Resources in Terraform?

A "resource" in Terraform represents an infrastructure object or entity that you want to manage using Terraform. Resources are the core building blocks of your infrastructure and can be thought of as the target entities you wish to create, update, or destroy.

Resource blocks are defined within the configuration and follow a specific syntax. They typically include attributes that define the resource's properties and its configuration settings.

resource "aws_instance" "example" {
    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
}

In the above example of resource type, the resource block defines a aws_instance resource named example. The resource block has two arguments, ami and instance_type, which specifies the AMI ID and instance type to use.

Before we start with Terraform and Docker, we need to install Docker and Terraform in our system on the AWS EC2 instance.

Step 1: Create an Amazon EC2 instance.

Step 2: Now let's install Terraform on our EC2 instance and to ensure of system is up to date let's first install gnupg,software-properties-common, and curl.

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

  • Step 3: Now let's add the HashiCorp GPG key.
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

Step 4: Verify the key's fingerprint.

gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

Step 5: Now let's add the HashiCorp repository.

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list

Step 6: Now let's update the apt repository.

sudo apt update

  • Step 7: Now let's install Terraform.
sudo apt-get install terraform

  • Step 8: Now let's verify the Terraform installation.
terraform --version

Step 9: To enable the tab completion and then install the autocomplete package.

touch ~/.bashrc
terraform -install-autocomplete

Note: In case Docker is not installed.

sudo apt-get install docker.io
sudo docker ps
sudo chown $USER /var/run/docker.sock

Task-01:

  • Create a Terraform script with Blocks and Resources.
mkdir terraform
cd terraform
sudo vim docker_terraform.tf

Step 1: Create a provider block for docker.

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
        }
    }
  }

Step 2: Create a resources block for docker.

provider "docker" {}

Task-02:

Create a resource Block for an nginx docker image.

Step 1: Create a resource block for docker_image.

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

Step 2: Create a resource block for docker_container.

resource "docker_container" "nginx" {
 image = docker_image.nginx.latest
 name  = "tutorial"
 ports {
   internal = 80
   external = 80
 }
}

Step 3: Now let's initialize the terraform.

terraform init

Step 4: Now let's plan the terraform.

terraform plan

Step 5: Now let's apply the terraform.

terraform apply

Step 6: Now let's verify the docker container.

docker ps

Step 7: Now let's check the nginx page on the public IP of the EC2 instance.

ย