Streamlining AWS Deployments: Jenkins & Terraform in Action with the 2048 Game

·

9 min read

Cover Image for Streamlining AWS Deployments: Jenkins & Terraform in Action with the 2048 Game

💡 Introduction

Welcome to the world of DevOps! Today, we're diving into an exciting project where we automate AWS infrastructure using Jenkins and Terraform while deploying the 2048 game on our server.

We’ll kick off by provisioning a t3.small EC2 instance, which will act as our host machine. On this instance, we’ll install Docker, Jenkins, Terraform, and the AWS CLI. Next, we’ll configure Terraform to provision an EC2 instance, create an S3 bucket for Terraform state and logs, and define IAM policies following the Principle of Least Privilege for security.

Once the infrastructure is ready, we’ll containerize the 2048 game using Docker and expose it via Nginx on port 80. Finally, we’ll automate everything using Jenkins, setting up a CI/CD pipeline that:
✅ Pulls Terraform code from GitHub
✅ Applies the Terraform configuration
✅ SSHs into the EC2 instance
✅ Deploys the 2048 game
✅ Restarts services automatically on changes

By the end of this blog, you’ll have a fully automated AWS deployment pipeline, giving you hands-on experience with Terraform, Jenkins, AWS, and containerized applications. Let's get started! 🚀


💡 Pre-Requisites

Before diving into the project, ensure you have a foundational understanding of the following concepts:

Basic AWS Knowledge – Since we’ll be provisioning infrastructure on AWS, familiarity with EC2, IAM Roles, AWS CLI, and S3 will be helpful.

Jenkins & Terraform Fundamentals – We’ll use Terraform for infrastructure provisioning and Jenkins to automate the deployment process, so understanding how Terraform manages cloud resources and how Jenkins pipelines work is essential.

Docker Basics – Since we’ll containerize the 2048 game using Docker, you should have a basic grasp of Docker images, containers, and networking.

If you’re new to any of these, don’t worry! This guide will walk you through the steps, but having prior knowledge will make things smoother. 🚀


💡 Setting Up the AWS EC2 Instance & Installing Required Tools

Step 1: Launch the EC2 Instance

1️⃣ Go to AWS EC2 Dashboard and click "Launch Instance".
2️⃣ Set the instance name as: 2048-Jenkins-Terraform-Automation.
3️⃣ Select Ubuntu (Latest AMI) as the operating system.
4️⃣ Choose t2.small as the instance type.
5️⃣ Set storage to 20 GiB.
6️⃣ Select an existing key pair or create a new one to connect via SSH.
7️⃣ Click "Launch Instance".

Step 2: Connect to the Instance

Once the instance is running:
🔹 Select the instance → Click Connect → Navigate to SSH Connect.
🔹 Copy the provided SSH command and paste it into your terminal:

ssh -i your-key.pem ubuntu@your-instance-ip

🔹 You are now connected to your EC2 instance! 🎉

Step 3: Install Required Tools

🛠️ Install Docker

sudo apt update -y
sudo apt install -y docker.io
sudo systemctl enable --now docker
sudo usermod -aG docker $USER && newgrp docker
docker --version

🛠️ Install Jenkins

sudo apt update -y
sudo apt install -y fontconfig openjdk-17-jre
java -version  # Verify Java installation

# Add Jenkins repository and install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update -y
sudo apt install -y jenkins
sudo systemctl enable --now jenkins

🔹 Retrieve Jenkins Initial Admin Password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

🛠️ Install Terraform

sudo apt update -y && sudo apt install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
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 -y
sudo apt install -y terraform
terraform --version  # Verify Terraform installation

🛠️ Install AWS CLI

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version  # Verify AWS CLI installation

Step 4: Configure Security Group & IAM Role

Update Security Group:

  • Navigate to EC2 → Security Groups → Edit Inbound Rules.

  • Add Rule:

    • Port: 8080

    • Protocol: TCP

    • Source: 0.0.0.0/0 (or restrict to your IP for security).

  • Click Save Rules.

Create IAM Role for EC2 Access:
1️⃣ Go to IAM → Roles → Create Role.
2️⃣ Select AWS Service and attach the following permissions:

  • AmazonEC2FullAccess

  • AmazonS3FullAccess
    3️⃣ Name the role: Terra-Jen-Role and assign it to your EC2 instance by going to the actionssecurityModify IAM Role. Select Terra-Jen-role and click update role.

Step 5: Configure AWS CLI Authentication

To authenticate Terraform and Jenkins with AWS, configure AWS CLI with an Access Key & Secret Key:

🔹 Create AWS Access Key:
1️⃣ Go to IAM → Users → Select your user.
2️⃣ Navigate to Security Credentials → Click Create Access Key.

3️⃣ Copy and store the Access Key ID & Secret Access Key.

🔹 Configure AWS CLI:
Run:

aws configure

Enter:

  • Access Key ID: Paste here

  • Secret Access Key: Paste here

  • Region: Your AWS region (e.g., us-east-1)

  • Output format: json

🔹 Verify AWS CLI is working:

aws ec2 describe-instances

You should see details of your t2.small instance. 🎯


💡 Provisioning AWS Infrastructure & Deploying the 2048 Game using Terraform & Docker

Now, we will clone the 2048 game repository and set up the necessary infrastructure using Terraform.

1️⃣ Clone the Repository & Configure Terraform

Start by cloning the repository and navigating to the Terraform setup directory:

git clone https://github.com/Pravesh-Sudha/2048-game.git
cd 2048-game/terraform-setup

Inside this directory, you’ll find a main.tf file. Adjust the Terraform configuration according to your needs:
✅ Update the AMI ID
✅ Set the Key Name
✅ Specify the Security Group
✅ Choose the AWS Region

Once the changes are done, save the file.

2️⃣ Initialize & Apply Terraform Configuration

Now, initialize Terraform and provision the required AWS resources:

terraform init       # Initializes Terraform and installs required providers
terraform plan       # Shows the planned actions (EC2 instance, S3 bucket, versioning)
terraform apply -auto-approve  # Deploys the infrastructure

Once Terraform completes execution, it will display the public IP address of the newly created EC2 instance.

3️⃣ Connect to the EC2 Instance

After waiting 2-3 minutes for the instance to be ready, use SSH to connect:

ssh -i key-name.pem ubuntu@<public-ip-of-the-new-instance>

4️⃣ Install Docker & Create a Dockerfile

Once connected, install Docker using:

sudo apt update
sudo apt install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker

Now, create a Dockerfile and paste the following content:

FROM ubuntu:22.04

RUN apt-get update
RUN apt-get install -y curl zip nginx

RUN echo "daemon off;" >> /etc/nginx/nginx.conf

RUN curl -o /var/www/html/master.zip -L https://codeload.github.com/gabrielecirulli/2048/zip/master
RUN cd /var/www/html && unzip master.zip && mv 2048-master/* . && rm -rf 2048-master master.zip

EXPOSE 80
CMD [ "/usr/sbin/nginx", "-c", "/etc/nginx/nginx.conf" ]

5️⃣ Build & Run the Docker Container

Now, build and run the Docker container:

sudo docker build -t 2048-game .
sudo docker run -d -p 80:80 2048-game

6️⃣ Verify Deployment

Open the public IP address of the EC2 instance in your browser:

http://<public-ip-of-the-new-instance>

🎉 Voila! Your 2048 game is now running inside an NGINX-powered Docker container on your AWS EC2 instance.


💡 Automating the Deployment with Jenkins Pipeline 🚀

Now that we’ve manually tested our application, it's time to automate the entire process! We'll create a Jenkins Pipeline that will:

Checkout the code from our GitHub repository 2048-game
Initialize and approve the Terraform configuration using credentials
SSH into the newly provisioned EC2 instance
Install Docker on the instance
Create a Dockerfile for our 2048 game
Build and run the Docker container
Access the application via the public IP of the deployed instance

Sounds interesting, right? Let's get started!

To get Jenkins Password, run the following command and log into Jenkins on port 8080:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Your Jenkins will be available at http://<public-ip-address-of-ec2-instance>:8080

Step 1: Install Required Jenkins Plugins

First, navigate to Manage Jenkins → Plugins and install the following plugins:

  • SSH Agent

  • Terraform

These plugins are essential for handling SSH connections and executing Terraform commands within Jenkins.

Step 2: Configure Credentials in Jenkins

Now, we need to store our AWS credentials and SSH key securely.

  1. Go to Manage Jenkins → Credentials → Global credentials → Add credentials

  2. Add the following credentials:

    • AWS_ACCESS_KEY_ID → Your AWS access key (Secret text)

    • AWS_SECRET_ACCESS_KEY → Your AWS secret access key (Secret text)

    • EC2_SSH_KEY → Upload your .pem key file (Secret file)

These credentials will be used in our pipeline for authentication and deployment.

Step 3: Create a New Jenkins Pipeline

  1. Go to New ItemPipeline

  2. Enter the name: "2048-game"

  3. Select GitHub project and paste the repository URL:

     https://github.com/Pravesh-Sudha/2048-game
     # or you can fork the project and paste your URL
    
  4. Scroll down to the Pipeline section:

    • Select Pipeline script from SCM

    • In Source, select Git and provide the same repository URL

    • Specify the branch as main

  5. Click Save

Step 4: Run the Jenkins Pipeline

Now, click Build Now and watch as Jenkins:
🔹 Provisions an EC2 instance using Terraform
🔹 SSHs into the instance and installs Docker
🔹 Builds the Docker image and runs the container
🔹 Outputs the Public IP of the running application

At the end of the pipeline console logs, you'll see a link to your deployed 2048 game. Click on it, and you'll see the application running! 🎮✨

Step 5: (Optional) Add a Webhook for Auto-Deployments

If you want the deployment to trigger automatically whenever you push new changes to GitHub, you can set up a webhook. However, since we're launching a new instance every time, it’s not strictly necessary in this setup.


💡 Conclusion

In this blog, we successfully automated the deployment of the 2048 game using Jenkins, Terraform, and Docker. We started by setting up a Jenkins Pipeline, configuring AWS credentials, provisioning an EC2 instance with Terraform, and deploying our application inside a Docker container. By the end, we had a fully functional CI/CD pipeline that could spin up infrastructure and deploy the game with a single click! 🚀

This project demonstrated how powerful Infrastructure as Code (IaC) and automation can be in modern DevOps workflows. Instead of manually provisioning servers and deploying applications, we let Jenkins handle the entire process—making deployments faster, consistent, and scalable.

Next Steps

While this pipeline works great, here are some possible improvements:
Automate cleanup – Destroy AWS resources after deployment to optimize costs.
Add monitoring – Use tools like Prometheus & Grafana to track performance.
Enhance security – Use IAM roles and least privilege access for better security.
Implement Blue-Green Deployments – Deploy updates without downtime.

By continuously refining our CI/CD pipeline, we move towards efficient, scalable, and production-ready deployments. I hope this blog helped you understand the automation behind deploying applications on AWS. Stay tuned for more DevOps projects! 💡🔧

Have any questions or suggestions? Drop them in the comments below! 💬

✨ For more informative blog, Follow me on Hashnode, X(Twitter) and LinkedIn.