Introduction
In today’s cloud-driven world, managing infrastructure efficiently and consistently is crucial for developers and IT professionals alike. Traditionally, setting up infrastructure involved navigating cloud consoles, configuring networks, launching instances, and ensuring everything was correctly connected—a manual process that was both time-consuming and prone to errors, especially when scaling.
Enter Terraform, an open-source Infrastructure as Code (IaC) tool that revolutionizes how we build, change, and version our infrastructure. Instead of manually configuring resources, Terraform is a cloud agnostic IaC tool that allows you to define your infrastructure using a simple, human-readable configuration language called HCL (HashiCorp Configuration Language). This approach not only automates deployments but also ensures consistency across environments and makes tracking changes effortless.
In this blog post, I’ll guide you step by step through deploying a basic infrastructure setup on AWS using Terraform. Whether you're a beginner or just brushing up on your skills, this tutorial will help you get hands-on experience with Terraform and HCL. We’ll cover everything from configuring your environment and writing Terraform code to deploying your first EC2 instance and setting up a simple web server. By the end, you’ll have the foundational knowledge to confidently use Terraform to manage your infrastructure in the cloud.
So, grab a cup of coffee, fire up your terminal, and let’s dive into the world of Terraform and HCL!
1. What You’ll Need
Before we start, ensure you have the following:
- AWS Account: You'll need an AWS account to provision. You can see how to create an AWS free tier account here
- Terraform Installed: Install Terraform by following the instructions on Terraform's official website.
- AWS CLI Installed: Install the AWS Command Line Interface (CLI) to configure your credentials. Follow the instructions here.
- VSCode Setup for Terraform: Install Visual Studio Code and the HashiCorp Terraform extension for syntax highlighting, code completion, and linting. This will make writing and managing Terraform configurations easier.
2. Configuring AWS Authentication
a. Setting Up AWS Credentials
To allow Terraform to authenticate and interact with AWS, you need to configure your AWS credentials. You can do this using the AWS CLI by running the following command in your terminal:
aws configure
You'll be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and default output format. If you don’t have access keys yet, you can create them in the AWS Management Console under IAM > Users > Security Credentials.
b. Setting Environment Variables (Alternative Method)
Alternatively, you can export your credentials as environment variables:
set AWS_ACCESS_KEY_ID=your_access_key
set AWS_SECRET_ACCESS_KEY=your_secret_key
This method is useful if you don’t want to store credentials in a file and it is also a good way to keep your access key confidential.
3. Writing the Terraform Configuration
In this section, we’ll walk through the Terraform code to deploy an EC2 instance on AWS. This EC2 instance will host a simple web server. Before that, lets take a look at the architecture diagram of the webserver we are creating.
a. Provider Configuration
First, we need to specify the AWS provider and region:
provider "aws" {
region = "us-east-1"
}
-
What It Does: This block tells Terraform to use AWS as the provider and to deploy resources in the
us-east-1
region.
b. Defining the EC2 Instance
Next, we define the EC2 instance that will run our web server:
resource "aws_instance" "web_server" {
ami = "ami-04a81a99f5ec58529"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.instance.id]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
tags = {
Name = "web-server"
}
}
- AMI: The Amazon Machine Image (AMI) ID defines the operating system and applications running on the instance. Here, we're using an Ubuntu AMI.
-
Instance Type:
t2.micro
is a small, free-tier eligible instance type suitable for this exercise. -
User Data: This script runs on instance startup, creating an
index.html
file and starting a simple web server on port 8080. - Tags: Tags make it easier to identify your resources.
c. Configuring the Security Group
We need to define a security group to allow traffic on port 8080, which is where our web server will listen.
resource "aws_security_group" "instance" {
name = "terraform-example-instance"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
- Security Group Name: This is the name of the security group.
- Ingress Rules: Allows inbound traffic on port 8080 from any IP address.
4. Initializing Terraform
Once the configuration files are ready, you need to initialize Terraform. Initialization prepares the working directory and downloads the necessary provider plugins.
Run the following command in your terminal:
terraform init
- What It Does: Downloads the AWS provider and initializes your Terraform working directory. This step is essential before running any other Terraform commands.
5. Planning the Infrastructure
Before applying the configuration, you can preview the changes Terraform will make by running:
terraform plan
-
What It Does: The
plan
command shows you a preview of the infrastructure Terraform will create, modify, or destroy. It helps catch errors and confirm that the configuration is correct before applying any changes as shown on the screenshot below.
6. Applying the Configuration
Now that you’ve reviewed the plan, it’s time to apply the configuration and create the infrastructure.
Run the following command:
terraform apply
-
What It Does: The
apply
command provisions the resources as defined in your configuration. Terraform will prompt for confirmation before proceeding. Once confirmed, it will create the EC2 instance and security group as shown on the screenshot below.
7. Verifying the Infrastructure
a. Retrieving the Public IP
After Terraform finishes applying the configuration, you’ll want to verify that everything is working correctly. To do this, you’ll need the public IP of your EC2 instance. You can find it in the Terraform output or on the EC2 dashboard of the AWS Management Console.
b. Testing the Web Server
With the public IP in hand, test the web server by running the following curl
command in your terminal:
curl http://<public-ip>:8080
- What It Does: This command sends an HTTP GET request to your EC2 instance's public IP on port 8080. If everything is working correctly, you should see the response "Hello, World" in your terminal, confirming that the web server is running.
8. Cleaning Up
When you're done with the infrastructure, it's important to clean up the resources to avoid unnecessary charges. You can do this by running:
terraform destroy
-
What It Does: The
destroy
command deletes all the resources that Terraform created. Terraform will prompt for confirmation before proceeding with the deletion.
Conclusion
Congratulations! You’ve just deployed a basic infrastructure setup on AWS using Terraform. We walked through the process of configuring AWS authentication, writing Terraform code, initializing Terraform, deploying and verifying an EC2 instance running a web server, and setting up VSCode for Terraform development. This exercise introduced you to key Terraform commands and provided a hands-on example of using Terraform to manage infrastructure on AWS.
With Terraform, you can easily scale this setup, automate more complex infrastructure, and maintain consistent environments. Keep experimenting, and you’ll soon be able to manage much larger infrastructure deployments!
Resources
Feel free to drop any questions in the comments, and happy coding!
Top comments (4)
great article
This blog makes a real difference. Thank you!
Welldone! Such an Intricate work...
This is insightful. Going through it again.