๐ถ Blocks and Resources in Terraform
Blocks: Blocks are the top-level constructs in HCL. They define the main sections of a configuration. For example, in a Terraform configuration, you'll typically have blocks for resources, providers, and variables. Blocks are identified by their type, enclosed in curly braces {}. Example:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
#In this code, the resource block creates an AWS EC2 instance, and "aws_instance" is the block type.
Resources:
Resources are used to declare and manage infrastructure elements like virtual machines, databases, networks, and more.
Example:
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }
๐ถ Task 01: 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" {}
๐ถ 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
Make a main.tf file:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
}
resource "docker_container" "nginx_container" {
name = "nginx_container"
image = docker_image.nginx.name
ports {
internal = 80
external = 8080
}
}
- Test your configuration using the Terraform CLI and make any necessary adjustments.
Terraform init :
The command "terraform init" is used to initialize a Terraform configuration directory. When you run this command in a directory containing Terraform configuration files, it initializes the working directory and downloads any required provider plugins or modules specified in the configuration. This command is typically one of the first steps when working with Terraform to set up your infrastructure
terraform init
Terraform validate:
The command "terraform validate" is used to check the syntax and validity of your Terraform configuration files without actually creating any infrastructure. It scans your configuration files (typically with the extension .tf
) and ensures that they are correctly formatted and that all references to resources and variables are valid. If there are any syntax errors or other issues in your configuration, this command will report them. It's a useful step to catch errors before attempting to apply your Terraform configuration to create or modify resources.
terraform validate
Terraform plan:
The "terraform plan" command is used to create an execution plan for your Terraform configuration. It examines the current state of your infrastructure, as recorded in your Terraform state file, and compares it to the desired state described in your configuration files.
terraform plan
Terraform apply:terraform apply
is the Terraform command used to execute the changes defined in your Terraform configuration and apply them to your infrastructure. It creates, modifies, or deletes resources according to your configuration. Use it carefully, especially in production environments, after reviewing the execution plan.
terraform apply
Add inbound rule 8080 in the security group.
Use your public IP to access the Nginx welcome page.
Note: In case Docker is not installed
sudo apt-get install
docker.io
sudo docker ps
sudo chown $USER /var/run/docker.sock
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud โ๏ธ and DevOps โพ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform