Terraform needs to be told which provider is to be used in the automation; hence, we need to give the provider name, source, and version. For Docker, we can use this block of code in your main.tf
Blocks and Resources in Terraform:
💡Blocks:
In Terraform, "blocks" refer to the sections or constructs used to define resources, variables, outputs, and other configurations within a Terraform configuration file. Blocks are defined using a specific syntax and are used to organize and structure the different elements of your infrastructure code.
resource "aws_instance" "example" {
ami = "abc123"
network_interface {
# ...
}
}
A block has a type (resource
in this example). Each block type defines how many labels must follow the type keyword. Theresource
block type expects two labels, which are aws_instance
and example
in the example above. A particular block type may have any number of required labels, or it may require none, as with the nested network_interface
block type.
The { and } characters separate the block body from the block type keyword and any labels. Additional parameters and blocks can be nested within the block body, forming a hierarchy of blocks and their associated arguments.
To learn more about blocks, checkout my previous blog: 👇https://iris1919.hashnode.dev/terraform-blocks
Resources:
🌀In Terraform, resources are the fundamental building blocks used to define and manage infrastructure. A resource represents a specific piece of infrastructure, such as a virtual machine, a database, a network interface, or any other component that can be provisioned and managed.
🌀Resources are defined using resource blocks in your configuration files. Each resource block defines the desired state of a particular resource and includes attributes that specify the resource type, name, and configuration options.
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id = "subnet-12345678"
tags = {
Name = "example-instance"
Environment = "production"
}
}
🌀In this example, the resource block begins with the keyword resource
, followed by the resource type "aws_instance"
, and a name for the instance "example"
. The block contains attributes specific to the EC2 instance, such as ami
, instance_type
, subnet_id
, and tags
.
🌀When you run terraform apply
, Terraform reads the resource blocks, determines the differences between the desired state specified in the configuration and the current state of the infrastructure, and takes the necessary actions to create, update, or delete resources to match the desired state.
🌀Terraform supports a wide range of resource types across various cloud providers, including AWS, Azure, Google Cloud Platform, and many others. Each resource type has its own set of attributes and configuration options, allowing you to customize and manage the specific aspects of each resource.
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 the AWS Console, create an EC2 instance, and connect to SSH, where we will perform all the following 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
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 80
}
}
In the event that Docker is not installed use the below commands:
sudo apt-get install docker.io
docker --version
sudo docker ps
sudo chown $USER /var/run/docker.sock
After you've created a Terraform configuration file, use the Terraform commands listed below to provision and manage your infrastructure:
Step 3: Initializes a new or existing Terraform working directory by downloading the necessary provider plugins using terraform init
command.
terraform init
Step 4: 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 5: 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 6: Check docker container is created using the below command:
docker ps
Step 7: Browse public IP address, you can see the Nginx default page.
Step 8: 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
Finally, these Terraform blocks will help you create durable, dynamic infrastructure. Understanding and mastering these technologies is critical to achieving the full potential of Terraform .
Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends .Hope you find it helpful🤞
Happy learning😊😊