Deploying an EC2 Instance with a Dockerized App using Terraform

Yash Sonawane - Feb 18 - - Dev Community

Introduction

In this blog, we will walk through deploying an AWS EC2 instance using Terraform and setting up a Dockerized application (Nginx) on it. This is a great project for those looking to enhance their Terraform and AWS skills while automating infrastructure provisioning.


Prerequisites

Before we begin, ensure you have:

Terraform installed → Download Here

AWS CLI installed and configured → Run aws configure

An AWS Key Pair (to SSH into the instance)


Project Overview

We will create:

✔️ An EC2 instance with a security group allowing SSH and HTTP traffic

✔️ Docker installation via Terraform's user_data

✔️ Nginx container running on the EC2 instance


Step 1: Define the Terraform Provider

Create a file named provider.tf and specify AWS as the provider:

provider "aws" {
  region = "us-east-1"  # Change this as needed
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Define an EC2 Instance with Docker Setup

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

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"  # Amazon Linux 2 AMI (update if needed)
  instance_type = "t2.micro"
  key_name      = "your-key"  # Replace with your actual key pair name

  security_groups = [aws_security_group.ec2_sg.name]

  user_data = <<-EOF
              #!/bin/bash
              sudo yum update -y
              sudo yum install docker -y
              sudo systemctl start docker
              sudo systemctl enable docker
              sudo docker run -d -p 80:80 nginx
              EOF

  tags = {
    Name = "Terraform-EC2-Docker"
  }
}
Enter fullscreen mode Exit fullscreen mode

What is happening here?

✔️ The Amazon Machine Image (AMI) is set to Amazon Linux 2

✔️ Instance Type is t2.micro (free-tier eligible)

✔️ Docker is installed and started

✔️ Nginx container is deployed and runs on port 80


Step 3: Define a Security Group

Create a file named security-group.tf:

resource "aws_security_group" "ec2_sg" {
  name        = "terraform-sg"
  description = "Allow SSH and HTTP"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Allows SSH access
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Allows HTTP access
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]  # Allows all outbound traffic
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Output the EC2 Public IP

Create a file outputs.tf to display the instance's public IP:

output "public_ip" {
  value = aws_instance.web.public_ip
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy the Infrastructure

Now, let’s deploy the Terraform configuration.

Initialize Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

Preview the Changes

terraform plan
Enter fullscreen mode Exit fullscreen mode

Apply the Configuration

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

Terraform will output the public IP address of the instance.


Step 6: Access the Deployed Application

Once the deployment is complete, open your browser and visit:

http://<your-instance-ip>
Enter fullscreen mode Exit fullscreen mode

You should see the Nginx default page, meaning your Docker container is running successfully! 🎉


Step 7: Destroy the Resources (Optional)

To clean up everything and avoid AWS charges, run:

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

Conclusion

In this blog, we:

✅ Deployed an EC2 instance with Terraform

✅ Installed Docker and ran an Nginx container

✅ Allowed SSH & HTTP traffic with a Security Group

✅ Outputted the Public IP for easy access

This is a foundational step towards Infrastructure as Code (IaC) and automating DevOps workflows.

💡 Next Steps:

  • Deploy a custom Docker app instead of Nginx
  • Use Terraform Modules for better reusability
  • Deploy a multi-container app using Docker Compose

Have questions or suggestions? Drop them in the comments! 🚀

. . . . . . . . . . .