Day 62: Terraform and Docker.

Day 62: Terraform and Docker.

Terraform is an Infrastructure as Code tool for provisioning and managing cloud and on-premises infrastructure. It uses declarative configurations and supports various cloud providers. Terraform enables you to plan and apply changes incrementally, maintaining the infrastructure state.

Docker is a containerization platform for packaging applications and their dependencies into portable containers. Dockerfiles define container configurations, while Docker Hub provides a registry for sharing container images. Docker is ideal for microservices, CI/CD pipelines, and ensuring consistency application deployment across different environments.

What are Blocks in Terraform?

  • A "block" in Terraform represents a particular kind of configuration stanza or section within a Terraform configuration file. Blocks are used to group related configuration settings together.

  • Blocks have a specific structure and syntax. They start with a keyword followed by an opening curly brace { and end with a closing curly brace }. The contents within the curly braces define the configuration for that block.

  • Common types of blocks in Terraform include "provider" blocks, "resource" blocks, "module" blocks, "variable" blocks, and "output" blocks.

provider "aws" {
  region = "us-east-1"
}
  • In the above example of block type, the provider block defines the configuration for the aws provider. The provider block has a single argument, region, which specifies the AWS region to use.

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.

Screenshot from 2023-09-02 23-59-24

  • 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

Screenshot from 2023-09-02 23-36-51

  • 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

Screenshot from 2023-09-02 23-37-18

  • Step 4: Verify the key's fingerprint.
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint

Screenshot from 2023-09-02 23-37-39

  • 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

Screenshot from 2023-09-02 23-37-59

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

Screenshot from 2023-09-02 23-38-25

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

Screenshot from 2023-09-02 23-39-03

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

Screenshot from 2023-09-02 23-39-31

  • Step 9: To enable the tab completion and then install the autocomplete package.
touch ~/.bashrc
terraform -install-autocomplete

Screenshot from 2023-09-02 23-40-04

Note: In case Docker is not installed.

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

Screenshot from 2023-09-11 22-14-42

docker --version

Task-01:

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

Screenshot from 2023-09-11 21-46-56

  • Step 1: Create a provider block for docker.

imageedit_3_3217547238

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

Note: kreuzwerker/docker, is shorthand for registry.terraform.io/kreuzwerker/docker.

  • Step 2: Create a resources block for docker.

imageedit_6_5114734835

provider "docker" {}

Task-02:

Create a resource Block for an nginx docker image.

  • Step 1: Create a resource block for docker_image.

imageedit_9_8175977380

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}
  • Step 2: Create a resource block for docker_container.

imageedit_13_5271309647

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

Screenshot from 2023-09-11 22-16-22

  • Step 4: Now let's plan the terraform.
terraform plan

Screenshot from 2023-09-11 22-16-42

  • Step 5: Now let's apply the terraform.
terraform apply

Screenshot from 2023-09-11 22-17-46

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

Screenshot from 2023-09-11 22-18-00

docker ps

Screenshot from 2023-09-11 22-19-00

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

imageedit_16_7114875880


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!