Infra as code (IaC) is an essential concept in DevOps, allowing infra management in an automated and reproducible way. Terraform, developed by HashiCorp, is one of the most popular tools for this purpose.
In this post, we will explore the basic concepts of Terraform and go through a practical example of how to use it to create an instance on aws.
What is Terraform?
Terraform is an open-source tool for defining, provisioning, and managing infrastructure across multiple cloud providers (AWS, Azure, GCP, etc.) using a declarative configuration language called HCL (HashiCorp Configuration Language).
Key Features:
- Multi-platform: Supports various providers such as AWS, Azure, GCP, Kubernetes, Etc.
- Declarative: You define the desired state of the infra, and Terraform takes care of the necessary changes.
- State management: Maintains a state file to track provisioned resources.
- Plan and Apply: Allows you to preview changes before applying them.
Installing Terraform
Before getting started, install Terraform by following the instructions on the official site.
To verify the installation, run:
terraform --version
If Terraform is installed correctly, the version number will be displayed in the terminal.
Creating a Terraform Project
Now, leet's go through a practical example using AWS.
Initializing the Project
Create a directory for the project and navigate into it:
mkdir my-terraform-project && cd my-terraform-project
Create a file named main.tf
and add the following basic configuration:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "MyTerraformInstance"
}
}
Understanding the main.tf
File
In terraform, configuration is divided into blocks, with the most common being:
-
Provider: Defines the cloud provider to be used. In the example above, we are using
aws
and specifying the regionus-east-1
. -
Resources: Specifies the resource to be created. In this case, an EC2 instance (
aws_instance
).-
ami
: ID of the operating system image to be used. -
instance_type
: Type of instance (e.g.,t2.micro
, ideal for testing). -
tags
: Labels applied to the instance for easier identification.
-
Initializing Terraform
In the terminal, run: terraform init
This command downloads the necessary plugins for the configured provider(in this case, AWS).
Planning Changes
Before creating resources, check what modifications will be applied: terraform plan
This displays the planned changes for the infrastructure.
Applying the Configuration
To create the instance on AWS, run: terraform apply
Type yes
when prompted. After a few minutes, your instance will be created.
Destroying Resources
If you want to remove the created resources, user: terraform destroy
This prevents unnecessary cloud infrastructure costs.
Using Terraform with Docker
In addition to cloud providers, Terraform can also be used to manage Docker containers. Let's see a basic example.
Configuring a Container with terraform
Create a new file called docker.tf
and add:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13.0"
}
}
}
provider "docker" {
host = "unix:///var/run/docker.sock"
}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.repo_digest
name = "nginx"
ports {
internal = 80
external = 8080
}
}
Initializing and Applying the Configuration
Now, execute the following commands to start Terraform and create the container:
terraform init
terraform apply
Type yes
when prompted. This will download the Nginx image and create a container exposed on port 8080
.
Testing the Container
Once Terraform finishes, open http://localhost:8080
in your browser to see Nginx running.
Removing the Container
If you want to remove the created containers and images, run: terraform destroy
This will remove all resources created by Terraform.
Conclusion
Conclusion
Terraform simplifies infrastructure management by allowing you to define everything as code. It is ideal for scalable and reproducible environments.
In this post, we covered:
What Terraform is and why to use it.
How to install and configure a basic project.
How to create and manage resources on AWS.
A basic understanding of
provider
andresource
blocks in Terraform.How to use Terraform to manage Docker containers.
If you want to deepen your knowledge, I recommended check out the official Terraform documentation.
Did you like this content? Leave a comment or share your experience with Terraform! 🚀