DEV Community

Cover image for Automating AWS EC2 Provisioning with Terraform: A Step-by-Step Guide
Maxwell Ugochukwu
Maxwell Ugochukwu

Posted on

Automating AWS EC2 Provisioning with Terraform: A Step-by-Step Guide

Introduction

Manually setting up cloud infrastructure can be time-consuming, error-prone, and difficult to maintain. This is where Infrastructure as Code (IaC) tools like Terraform revolutionize cloud provisioning.

In this guide, we’ll walk through the step-by-step process of provisioning an AWS EC2 instance using Terraform, from installation to deployment and cleanup.

By the end of this tutorial, you'll understand:
✅ The importance of Terraform in cloud infrastructure management
✅ How to install and configure Terraform on different operating systems
✅ The step-by-step process to provision an EC2 instance on AWS
✅ Best practices for scaling and managing cloud resources efficiently

🔧 Prerequisites

Before we begin, ensure you have the following:

1. An AWS Account

If you don’t have one, sign up at AWS Console and set up your IAM user with programmatic access.

2. Install Terraform

Terraform runs on Windows, macOS, and Linux. Follow the installation steps for your OS:

Windows

  • Download Terraform from the Terraform website.
  • Extract the .zip file and add it to your system’s PATH.
  • Verify the installation: terraform --version

macOS (Using Homebrew)

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
terraform --version
Enter fullscreen mode Exit fullscreen mode

Linux

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
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-get update && sudo apt-get install terraform
terraform --version
Enter fullscreen mode Exit fullscreen mode

🌍 Step 1: Configuring Terraform for AWS

Terraform requires AWS credentials to interact with your AWS environment. You can configure AWS credentials in two ways:

Option 1: Using AWS CLI

- Install AWS CLI:

sudo apt install awscli  # Linux
brew install awscli       # macOS
Enter fullscreen mode Exit fullscreen mode

- Configure AWS:

aws configure
Enter fullscreen mode Exit fullscreen mode

Provide your AWS Access Key, Secret Key, Region, and Output Format.

Option 2: Manually Adding AWS Credentials

Store credentials in ~/.aws/credentials:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
region = eu-west-2
Enter fullscreen mode Exit fullscreen mode

📝 Step 2: Writing the Terraform Configuration File

Now, create a working directory for Terraform:

mkdir terraform-ec2 && cd terraform-ec2

Enter fullscreen mode Exit fullscreen mode

Create a file named main.tf, and add the following configuration:

provider "aws" {
    profile = "default"
    region  = "eu-west-2"
}

resource "aws_instance" "UGO_Server" {
    ami           = "ami-0cbf43fd299e3a464"
    instance_type = "t2.micro"

    tags = {
        Name = "MyNCAAInstance"
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Breaking Down the Code

✅ provider "aws" – Defines AWS as the cloud provider and sets the region.
✅ resource "aws_instance" "UGO_Server" – Creates an EC2 instance.
✅ ami – Uses an Amazon Machine Image (AMI) for the instance.
✅ instance_type – Specifies the instance type (t2.micro for free tier).
✅ tags – Assigns a name to the instance.

🚀 Step 3: Initializing and Applying Terraform

Before deploying, we need to initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Image description

This downloads the necessary provider plugins.

Check the Execution Plan
Run:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Image description

This shows what Terraform will create.

Apply the Configuration
Now, deploy your EC2 instance:

terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

Image description

This provisions the EC2 instance in AWS.

📌 Step 4: Confirming the Deployment

  1. Go to the AWS Console
  2. Navigate to EC2 → Instances
  3. Verify that your instance is running 🎉

Image description

Image description

🧹 Step 5: Destroying the Infrastructure

When you're done, clean up resources to avoid unnecessary charges:

terraform destroy -auto-approve
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

This safely removes all provisioned resources.

💡 Key Takeaways

  • Terraform simplifies cloud provisioning with Infrastructure as Code (IaC).
  • You can automate AWS EC2 deployment in a few lines of code.
  • Terraform ensures repeatability, scalability, and consistency in cloud infrastructure.
  • Cleanup is easy using terraform destroy, preventing resource waste.

🎯 Next Steps

  • Try it yourself – Clone my GitHub repo and experiment! 👉 GitHub Repository
  • Extend the setup – Add security groups, attach an EBS volume, or deploy multiple instances.
  • Learn Terraform Modules – Use modular code to manage large-scale deployments.

🚀 What’s your experience with Terraform? Let’s discuss in the comments! 👇

Top comments (1)

Collapse
 
dansasser profile image
Daniel T Sasser II

Check out ideogram.ai. You won't be disappointed. Great image generator for articles. Head over to my page here on Dev and check out some of my work for examples of images by ideogram.