Welcome back to our Terraform journey! Today, we'll dive into the seamless integration of Terraform with Docker to streamline your infrastructure provisioning and management. Terraform, as you may know, is a powerful infrastructure as code (IaC) tool, and Docker is a leading containerization platform. Together, they offer a robust solution for automating the deployment and management of your applications.
Task-01 Create a Terraform script with Blocks and Resources
In Terraform, the terms "Blocks" and "Resources" refer to fundamental concepts used to define and structure your infrastructure as code (IaC). Let's explore each of these concepts:
Blocks in Terraform:
A "block" in Terraform is a section of code that groups related configurations together. Blocks define the structure and organization of your Terraform configuration. There are different types of blocks, and each serves a specific purpose. Some common types of blocks include:
Terraform Block:
Used to configure global settings for a Terraform configuration.
Specifies required providers, backend configurations, and other global settings.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "path/to/terraform.tfstate"
region = "us-east-1"
}
}
Provider Block:
Configures a specific provider (e.g., AWS, Azure, Docker) that Terraform will use to manage resources.
Specifies the source and version of the provider plugin.
provider "aws" {
region = "us-west-2"
}
Resources in Terraform:
A "resource" in Terraform represents a single infrastructure component. Resources define the desired state of a specific object in your infrastructure, such as a virtual machine, database, or network. Each resource has a type, a name, and a set of configuration parameters.
Resource Block:
Defines a specific resource type and its configuration.
Describes the infrastructure component you want to create, modify, or delete.
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
Data Block:
Retrieves and outputs data from external sources or existing infrastructure.
Allows you to query information without creating new resources.
data "aws_ami" "latest_amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
In summary, blocks provide the structure for your Terraform configuration, while resources define the infrastructure components you want to manage. By combining these elements, you can express your entire infrastructure in a declarative and version-controlled manner.
Task-02
Create a resource Block for an nginx docker image
- Create a Terraform script with Blocks and Resources
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.21.0"
}
}
}
Note: kreuzwerker/docker, is shorthand for registry.terraform.io/kreuzwerker/docker.
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" {}
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
}
}
Once you've crafted your Terraform configuration file (saved with a .tf extension), employ the following Terraform commands to set up and manage your infrastructure:
Initialize Terraform:
terraform init
This command kickstarts a new or existing Terraform working directory. It downloads and installs necessary providers and modules, initializes the backend, and fetches any required plugins.
Generate Execution Plan:
terraform plan
This command creates an execution plan detailing the actions Terraform will take to achieve the desired state outlined in the configuration file. It also highlights any changes to be made to the infrastructure.
Apply Changes:
terraform apply
Execute the proposed actions outlined in the plan generated by
terraform plan
. This command handles the provisioning and configuration of the infrastructure defined in the configuration file.
Verify that a Docker container is created:
docker ps
Finally, by navigating to the public IP address, you'll be able to observe the default page of nginx.