Introduction:)
Welcome to Day 56 of #90DaysOfDevOps, where our journey into DevOps mastery continues. Today, we're diving into the intricacies of Terraform blocks, each serving a unique purpose in defining and configuring elements within your infrastructure. From providers to resources, data sources, variables, outputs, modules, and locals, we're unlocking the power of Terraform's versatile configuration.
Terraform block?
In Terraform, a "block
" refers to a section of code that defines a specific element in the infrastructure configuration. These blocks are organized hierarchically and represent different components or functionalities within your Terraform configuration.
If you're looking to configure third-party tools within your Terraform configuration, you might use other specific blocks such as provider
, data
, or module
depending on the context.
Provider Block:
Purpose: Specifies the cloud or service provider and its configuration for managing resources.
example:
provider "aws" { region = "us-east-1" }
Resource Block:
Purpose: Declares a resource within the infrastructure, such as virtual machines, networks, or databases.
example:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }
Data Source Block:
Purpose: Retrieves data from a specific source, like existing infrastructure or external APIs.
example:
data "aws_subnet_ids" "example" { vpc_id = aws_vpc.example.id }
Variable Block:
Purpose: Declares input variables for a configuration, allowing for dynamic values.
example:
variable "aws_region" { type = string default = "us-west-2" } # To use this variable in the provider block provider "aws" { region = var.aws_region }
Output Block:
Purpose: Declares output values that can be used by other configurations or displayed after applying.
example:
output "instance_ip" { value = aws_instance.example.public_ip }
Module Block:
Purpose: Defines a reusable module, allowing you to encapsulate and share infrastructure configurations.
example:
module "example" { source = "terraform-aws-modules/ec2-instance/aws" version = "2.0.0" # Other module configurations... }
Local Block:
Purpose: Defines local variables within a module, enhancing code organization and reusability.
example:
locals { subnet_names = ["subnet-1", "subnet-2"] }
โ๏ธ Prerequisites for Task 01:-
Terraform Installation: Ensure that Terraform is installed on your system. You can download it from here.
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg 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 sudo apt update && sudo apt install terraform
Docker installation:
sudo apt update && sudo apt install -y docker.io
Current user add to the docker group
sudo usermod -aG docker $USER
Task-01: Configuring Docker Provider in Terraform Configuration
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
terraform
: This block is used to configure global settings for Terraform.required_providers
: This is a sub-block within theterraform
block and is used to specify the providers that the Terraform configuration depends on.docker
: This is the name of the provider. In this case, it's the Docker provider.source = "kreuzwerker/docker"
: This line specifies the source of the provider. It indicates where Terraform should find the Docker provider. In this example, it's "kreuzwerker/docker," which typically refers to a namespace/repository on a provider registry.version = "3.0.2"
: This line specifies the required version of the provider.
terraform validate
This configuration requires provider registry.terraform.io/kreuzwerker/docker, but that provider isn't available. You may be able to install it automatically by running:
terraform init
Task-02: Create the resource block for the nginx-docker image
Step 1: Create a new file named nginx_
image.tf
and add the following content:
# Declare the Docker provider
provider "docker" {
}
# Declare a Docker image resource for nginx
resource "docker_image" "nginx" {
name = "nginx:latest"
}
# Declare a Docker container resource for ngin
resource "docker_container" "nginx" {
image = docker_image.nginx.name
name = "tutorial"
ports {
internal = 80
external = 80
}
}
Step 2: Generate Terraform Plan
Run the following command to generate a Terraform execution plan. This plan will outline the changes Terraform will make to achieve the desired state:
terraform plan
This command will analyze your configuration, check the current state, and provide a summary of the changes it intends to make.
Step 3: Apply Terraform Changes
If the plan looks correct and you're ready to proceed, apply the changes using the following command:
terraform apply
Terraform will prompt you to confirm the planned changes. Type yes
and press Enter to apply the changes. Terraform will then create the Docker container based on the specified configuration.
Step 4: Check is docker-nginx-image is created or not
docker ps
Step 5: Navigate to the public URL to view the nginx default webpage.
Conclusion:)
Congratulations on completing Day 56 of #90DaysOfDevOps! Today's adventure into Terraform blocks has equipped you with essential tools to architect and orchestrate your infrastructure.
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on:
For more updates and engaging discussions on DevOps, let's connect! ๐ #DevOpsCommunity