Table of Content
Infrastructure as Code (IaC) has revolutionized the way IT teams manage cloud resources. Instead of manually configuring servers, networking, and storage, IaC allows you to define your infrastructure in code. This brings consistency, automation, and scalability to cloud environments.
By the end of this guide, you will be able to:
- Understand Terraform and its core features
- Install and set up Terraform for AWS
- Write and apply Terraform configurations
- Deploy an AWS EC2 instance using Terraform
- Manage and destroy resources efficiently
What is Terraform?
Terraform is an open-source infrastructure automation tool that enables users to define cloud resources in a declarative format. Unlike manually provisioning infrastructure through the AWS Console, Terraform automates the entire process, reducing the chances of human error.
Key Features of Terraform:
- Declarative Language: You describe what you want, and Terraform figures out how to create it.
- State Management: Keeps track of resources and their current state.
- Supports Multiple Providers: Although we are focusing on AWS, Terraform also supports Azure, Google Cloud, and many others.
- Execution Plan (terraform plan): Shows what changes will be made before applying them.
- Dependency Management: Terraform understands the relationships between resources and provisions them in the correct order.
Setting Up Terraform for AWS
Before we start writing Terraform code, we need to install Terraform and configure AWS credentials.
Step 1: Install Terraform on Ubuntu
Run the following commands to install Terraform on Ubuntu:
sudo apt update && sudo apt upgrade -y
sudo apt install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo tee /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 -y terraform
Verify the installation:
terraform --version
Step 2: Install and Configure AWS CLI on Ubuntu
To allow Terraform to interact with AWS, install the AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Verify the installation:
aws --version
Now, configure AWS credentials:
aws configure
It will prompt for:
- AWS Access Key ID
- AWS Secret Access Key
- Default region (e.g., us-east-1)
- Output format (leave as json)
Step 3: Install Unzip and Other Dependencies
Terraform might need unzip and other dependencies to function properly. Install them with:
sudo apt install unzip -y
Writing Your First Terraform Configuration
Terraform uses .tf files to define resources. Let's create an AWS EC2 instance using Terraform.
Step 1: Create a Working Directory
Create a new directory for the Terraform project:
mkdir terraform-aws
cd terraform-aws
Step 2: Define AWS Provider in Terraform
Create a file called main.tf and add the following code:
provider "aws" {
region = "us-east-1"
}
This tells Terraform that we are using AWS as our cloud provider in the us-east-1 region.
Step 3: Define an EC2 Instance
Now, we will define an EC2 instance inside main.tf:
resource "aws_instance" "my_ec2" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "TerraformEC2"
}
}
Here, we are specifying:
- AMI ID: The Amazon Machine Image (AMI) determines the OS of the EC2 instance. Ensure you use an appropriate AMI ID for your region.
- Instance Type: We are using t2.micro, which is free-tier eligible.
- Tags: Assigns a name to the instance.
Initializing and Applying Terraform Code
Step 1: Initialize Terraform
Run the following command to initialize the working directory:
terraform init
This downloads the AWS provider plugin required for Terraform.
Step 2: Plan Changes
Before applying changes, check what Terraform will do:
terraform plan
It will display the resources that will be created.
Step 3: Apply the Terraform Configuration
Run the following command to create the EC2 instance:
terraform apply
It will ask for confirmation. Type yes to proceed. Terraform will then create the instance and display the details.
Managing Terraform State
Terraform keeps track of resources using a state file (terraform.tfstate). This file must be stored securely.
1. Using Remote State with AWS S3
To store the state file remotely, create an S3 bucket and update main.tf:
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "terraform.tfstate"
region = "us-east-1"
}
}
Then, run:
terraform init
This configures Terraform to store the state file in the S3 bucket instead of locally.
2. Destroying Infrastructure
To remove all resources created by Terraform, run:
terraform destroy
This will delete the EC2 instance and any other resources created.
Best Practices for Using Terraform on AWS
Use Version Control
Store your Terraform code in a Git repository. Use branches to manage changes.
Implement Remote State Management
Store the state file in S3 with DynamoDB locking to prevent conflicts.
Use Terraform Modules
Create reusable modules for common resources like EC2, VPCs, and databases.
Secure Sensitive Data
Use AWS Secrets Manager or environment variables for storing credentials.
Integrate with CI/CD Pipelines
Use AWS CodePipeline, GitHub Actions, or Jenkins to automate Terraform deployments.
Conclusion
Terraform makes it easy to manage infrastructure on AWS by defining resources in code. In this guide, we:
- Installed and set up Terraform
- Defined and deployed an AWS EC2 instance
- Managed Terraform state
- Learned how to destroy resources and follow best practices
By using Terraform, teams can ensure infrastructure consistency, reduce manual errors, and scale cloud environments efficiently. Start experimenting with Terraform today and explore more advanced concepts like networking, security groups, and database provisioning!
Ready to modernize your infrastructure? Schedule a free consultation with Intuz to explore Terraform solutions for your business.