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 theAWS 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 aspecific 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 namedexample
. The resource block has two arguments,ami
andinstance_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 theninstall 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
docker --version
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 fordocker
.
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 fordocker
.
provider "docker" {}
Task-02:
Create a resource Block for an nginx docker image.
- Step 1: Create a
resource
block fordocker_image
.
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
- Step 2: Create a
resource
block fordocker_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.