Day 62 — Terraform and Docker
Terraform needs to be told which provider to be used in the automation, hence we need to give the provider name with source and version.
In Terraform, blocks and resources are fundamental building blocks used to define and configure infrastructure resources and their attributes within your infrastructure-as-code (IaC) configuration. Let’s take a closer look at what blocks and resources are:
1)Blocks:
- Blocks are the basic structural elements in a Terraform configuration.
- They define a particular kind of construct within a configuration, such as a provider, resource, data source, module, variable, or output.
- Blocks are typically denoted by keywords like
provider
,resource
,data
,module
,variable
, oroutput
. - Blocks can contain configuration settings and attributes specific to their type.
2) Resources:
- Resources are a specific type of block used to define infrastructure resources you want to manage using Terraform.
- Each resource block corresponds to a real-world resource, such as a virtual machine, network, database, or other cloud or on-premises assets.
- Resources have a specific type (e.g.,
aws_instance
,azurerm_virtual_network
,google_storage_bucket
) and a name that you provide. - You specify the configuration settings and attributes for the resource within the resource block.
Here’s an example of a resource block in Terraform that defines an AWS EC2 instance:
resource "aws_instance" "example" {
ami = "ami-12345678"
instance_type = "t2.micro"
count = 2
tags = {
Name = "example-instance-${count.index + 1}"
}
}
Tasks:
Task-01
Create a Terraform script with Blocks and Resources
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
}
This is a Terraform configuration block that specifies a required provider for the docker resource type. The provider is defined using the kreuzwerker/docker source and version ~> 2.21.0.
The required_providers block is used to declare the minimum version of a provider required by the Terraform configuration. In this case, the docker provider with version 2.21.0 or greater is required to run the configuration. If the provider is not installed, Terraform will automatically download and install the provider at the specified version.
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" {}
Resource
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-02
Create a resource Block for an nginx docker image
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
Create a resource Block for running a docker container for nginx
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 80
}
}
After you’ve created a Terraform configuration file (with a.tf extension), use the Terraform commands listed below to provision and manage your infrastructure:
Terraform init : Downloads and installs any required providers and modules, initializes the backend, and downloads any necessary plugins in a new or existing Terraform working directory.
terraform init
Terraform plan: The output of terraform plan gives a summary of the infrastructure modifications that will be made, such as creating, changing, or eliminating resources. It also displays any issues or warnings that must be handled prior to implementing the modifications.
terraform plan
terraform apply: When you run Terraform apply, Terraform generates or adjusts the configuration files’ resources to match the intended state. It also updates the state file to reflect the infrastructure changes. If Terraform encounters any faults or warnings during the application process, it will prompt you to confirm whether or not to proceed with the changes.
terraform apply
In case Docker is not installed use the below commands:
sudo apt-get install docker.io
sudo docker ps
sudo chown $USER /var/run/docker.sock
Check docker container is created using the below command:
docker ps
Browse public IP address, you can see nginx default page.
Our public IP is 3.208.30.13, So Let’s open the site at 3.208.30.13:80.
That’s all there is to launching a docker nginx container with terraform scripting.