Day 62 - Terraform and Docker

Day 62 - Terraform and Docker

As, we have seen about blocks and arguments in the previous blog, i.e. Components of HCL.

Blocks and Resources in Terraform

The syntax of HCL: -

<block> <resource_type> <resource_name>{
    arguments
}

What is a block in terraform?

Blocks define different aspects of the configuration, such as the provider, resource, variable, or output. Each block has a specific purpose and contains configuration settings specific to that block type.

What are arguments in terraform?

Within blocks, you define arguments to set the configuration values for resources or other block types.

Arguments are specified using the key = value syntax, where the key represents the configuration setting name and the value represents the desired value for that setting.

What are resources in terraform?

A resource is a specific type of block used to define and manage a resource within an infrastructure provider. It describes the desired state of a resource and instructs Terraform on how to create, update, or destroy that resource.

A resource block typically includes properties such as the resource type, name, and configuration options specific to the provider.

<resource_type> in HCL syntax provides two bits of information. First is the provider, which is represented by the word before the underscore in the resource_type and second is the type of resource, which is after the underscore in the resource_type.

<resource_name> in HCL syntax is the logical name used to identify the resource.

Example

Here’s an example of a resource block in a Terraform configuration file defining a file: -

resource "local_file" "demo_file" {
                 filename = "demo.txt"
                 content = "Hello World!!"
}

In this case,
1. block type is "resource"
2. resource_type is "local_file", in which
local = provider
file = type of resource
3. resource_name is "demo_file"
4. arguments are "filename and content"

When we want to configure terraform to use third-party tool, the we have to create terraform block. Same, we use for configuring providers.

block {                    terraform {
   arguments     ===           We give providers in arguments.
}                          }

Whenever, you want to make changes in terraform configuration, make sure to create a terraform.tf file.

TASKS

TASK 1: Create a Terraform script with Blocks and Resources.

Let us create a Terraform Docker script with Blocks and Resources👇

Step 1: Login to AWS Console and Create an EC2 instance and connect to SSH in which we will perform all the below steps.

Step 2: Create a terraform.tf and pass the docker provider.

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

Note: kreuzwerker/docker, is a shorthand Docker installation. kreuzwerker docker registry

Step 3:Provider Block: The provider block configures the specified provider, in this case, docker. A provider is a plugin that Terraform uses to create and manage your resources.

provider "docker" {}

Step 4: Resource Block: Use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as a Docker container, or it can be a logical resource such as a Heroku application.

Resource blocks have two strings before the block: the resource type and the resource name. In this example, the first resource type is docker_image and the name is Nginx.

TASK 2: Create a Resource Block for an nginx docker image and docker container.

Step 1: Create a resource Block for an nginx docker image in main.tf file

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

Step 2: Create a resource Block for running a docker container for Nginx in same main.tf file.

resource "docker_container" "my_nginx_container" {
      image = docker_image.my_nginx.name
      name = "nginx-container"   
      ports {
           internal = 80
           external = 80
    }
}

Step 3: In case Docker is not installed use the below commands: -

sudo apt-get update

sudo apt-get install docker.io

sudo chown $USER /var/run/docker.sock

docker --version

Step 4: Make sure terraform is installed on your server. Refer this link: Install Terraform.

Step 5: Initializes a new or existing Terraform working directory by downloading the necessary provider plugins using terraform init command.

terraform init

Step 6: Now execute the terraform plan command which will create an execution plan by comparing the desired state in the configuration to the current state of the infrastructure.

terraform plan

Step 7: Execute the terraform apply command so all the configurations get executed. It creates, modifies, or deletes resources as necessary to achieve the desired state, based on the execution plan generated by terraform plan.

terraform apply

Step 8: Check docker container is created using the below command:

docker ps

Step 9: Browse public IP address, you can see the Nginx default page.

Step 10: Execute the terraform destroy so it will prompt for confirmation and then proceeds to delete the resources, reverting the infrastructure to its pre-Terraform state.

terraform destroy

You can see there is no image and container present now after running terraform destroy command.

Conclusion

In Conclusion, We have seen about blocks, resources, and providers in Terraform: -

  1. Blocks: In Terraform configuration, blocks are the primary elements used to define configurations. Blocks are containers for configuration settings and are defined using a specific syntax. Common types of blocks include resource, provider, variable, output, etc.

  2. Resources: Resources are the most fundamental component in Terraform configurations. They represent the infrastructure objects (such as virtual machines, databases, networks, etc.) that you want to manage. Resources are declared within a resource block, specifying their type (e.g., aws_instance), a unique name, and configuration attributes.

  3. Providers: Providers are responsible for managing the lifecycle of resources, such as creating, updating, and deleting them. They act as an interface between Terraform and various cloud providers, SaaS providers, or on-premises infrastructure. Providers are declared using a provider block in your Terraform configuration, where you specify the provider type (e.g., aws, azure, google, etc.) and configuration details such as access credentials and regions.

These elements together enable Terraform users to describe their desired infrastructure in code, manage it efficiently, and enforce desired state configurations.

In this block, we have created a Terraform Docker script with Blocks and Resources and created a resource block for docker image and container.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌