Automated CI/CD Pipeline for Dockerized 2048 Game on AWS

1. Introduction

In modern software development, Continuous Integration and Continuous Deployment (CI/CD) play an important role in automating application delivery.

Manually building, testing, and deploying applications can consume time and introduce human errors. CI/CD pipelines solve this problem by automating the software delivery process.

In this project, we will build an Automated CI/CD Pipeline for a Dockerized 2048 Game on AWS.

We will containerize the 2048 game using Docker and automate its deployment using AWS CodePipeline, AWS CodeBuild, Amazon ECR, and AWS CodeDeploy.

Whenever a developer pushes new code to GitHub, the pipeline will automatically:

  • Detect source code changes.
  • Build a Docker image.
  • Run application checks.
  • Push the Docker image to Amazon ECR.
  • Deploy the updated application to Amazon EC2.
  • Verify the application deployment.

This project demonstrates practical DevOps concepts and real-world AWS deployment practices.

2. Project Objectives

The main objectives are:

  • Containerize the 2048 game using Docker.
  • Create an automated CI/CD pipeline.
  • Integrate GitHub with AWS CodePipeline.
  • Build Docker images using AWS CodeBuild.
  • Store container images in Amazon ECR.
  • Deploy Docker containers using AWS CodeDeploy.
  • Host the application on Amazon EC2.
  • Configure an Application Load Balancer.
  • Implement automated deployment and monitoring.

3. Understanding CI/CD

CI/CD stands for Continuous Integration and Continuous Delivery or Continuous Deployment.

Continuous Integration (CI)

Continuous Integration automatically builds and tests application code whenever developers push changes to a source code repository.

Continuous Deployment (CD)

Continuous Deployment automatically releases successfully tested application changes to the target environment.

Why CI/CD?

  • Faster software releases.
  • Reduced manual work.
  • Consistent deployment environments.
  • Early detection of errors.
  • Improved development productivity.
  • Repeatable deployment processes.

4. AWS Architecture Overview

The proposed architecture uses the following workflow:

 Developer | v GitHub Source Code | v AWS CodePipeline | v AWS CodeBuild | +------+------+ | | v v Build Docker Run Tests Image | v Amazon ECR Docker Registry | v AWS CodeDeploy | v Amazon EC2 Docker Engine | v Application Load Balancer | v 2048 Game Live Website 

The pipeline separates source management, image building, image storage, and application deployment.

For a production deployment, EC2 instances should run in private subnets behind a public Application Load Balancer, preferably across multiple Availability Zones.

5. AWS Services Used

AWS Service Purpose
GitHub Source code repository
AWS CodePipeline Automates the CI/CD workflow
AWS CodeBuild Builds and tests Docker images
Amazon ECR Stores Docker images
AWS CodeDeploy Automates application deployment
Amazon EC2 Hosts Docker containers
Application Load Balancer Distributes incoming traffic
Amazon VPC Provides network isolation
AWS IAM Manages service permissions
Amazon CloudWatch Monitors logs and performance

6. Project Prerequisites

Before starting, make sure you have:

  • An active AWS account.
  • A GitHub account.
  • Basic Linux knowledge.
  • Basic Docker knowledge.
  • Understanding of AWS EC2 and networking.
  • AWS CLI installed (optional for console-based setup).
  • A basic understanding of CI/CD concepts.

7. Step-by-Step Implementation

Step 1: Create the 2048 Game Repository

First, create a GitHub repository for the 2048 game.

Example repository structure:

2048-docker-cicd/ │ ├── src/ │ ├── index.html │ ├── style.css │ └── script.js │ ├── Dockerfile ├── buildspec.yml ├── appspec.yml │ └── scripts/ ├── install_dependencies.sh ├── start_container.sh └── stop_container.sh 

The src directory contains the game files.

The Dockerfile defines how the application will be packaged into a Docker image.

Step 2: Create a Dockerfile

A Dockerfile defines the instructions required to build a container image.

For this project, we can use Nginx to serve the static 2048 game files.

FROM nginx:alpine COPY src/ /usr/share/nginx/html/ EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] 

This Dockerfile:

  • Uses Nginx Alpine as the base image.
  • Copies the game files into the Nginx web directory.
  • Exposes port 80.
  • Starts the Nginx web server.

Build the Docker image locally:

docker build -t 2048-game . 

Run the container:

docker run -d -p 8080:80 --name 2048-game 2048-game 

Open the following address in your browser:

http://localhost:8080 

If the game loads successfully, the Docker image is working.

Step 3: Create an Amazon ECR Repository

Amazon Elastic Container Registry (ECR) is a managed container image registry.

It stores Docker images that can be accessed by authorized AWS services and EC2 instances.

Implementation:

  1. Open the Amazon ECR console.
  2. Select Private Repositories.
  3. Click Create Repository.
  4. Enter the repository name:
2048-game 
  1. Keep the repository private.
  2. Enable image scanning if required.
  3. Create the repository.

Copy the repository URI because it will be required during the Docker image build and deployment process.

Step 4: Configure AWS CodeBuild

AWS CodeBuild automatically builds and tests application source code.

Create a CodeBuild project:

  1. Open AWS CodeBuild.
  2. Select Create Build Project.
  3. Enter the project name:
2048-docker-build 
  1. Select GitHub as the source.
  2. Connect your GitHub repository.
  3. Choose a managed Linux build environment.
  4. Select a suitable environment image.
  5. Enable privileged mode because Docker image building requires Docker daemon access in the standard CodeBuild setup.
  6. Configure the build service role.
  7. Add the required environment variables.

Example environment variables:

Variable Value
AWS_DEFAULT_REGION Your AWS Region
AWS_ACCOUNT_ID Your AWS Account ID
IMAGE_REPO_NAME 2048-game

The CodeBuild service role needs permission to authenticate with ECR and push images to the specified repository.

Step 5: Create the buildspec.yml File

The buildspec.yml file defines the build and test commands executed by CodeBuild.

Example:

version: 0.2 phases: pre_build: commands: - echo Logging in to Amazon ECR - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com - IMAGE_TAG=$CODEBUILD_RESOLVED_SOURCE_VERSION - IMAGE_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG build: commands: - echo Building Docker image - docker build -t $IMAGE_URI . - echo Running container validation - docker run -d --name test-2048 -p 8080:80 $IMAGE_URI - sleep 5 - curl --fail http://localhost:8080/ - docker stop test-2048 - docker rm test-2048 post_build: commands: - echo Pushing Docker image to ECR - docker push $IMAGE_URI - echo $IMAGE_URI > image_uri.txt - echo Build completed successfully artifacts: files: - image_uri.txt - appspec.yml - scripts/**/* 

This build process:

  1. Authenticates with Amazon ECR.
  2. Builds the Docker image.
  3. Runs a basic container test.
  4. Pushes the image to ECR.
  5. Saves the image URI for deployment.
  6. Packages the deployment files as a CodeBuild artifact.

The build environment must have Docker available and the ECR repository must already exist.

Step 6: Prepare AWS CodeDeploy

AWS CodeDeploy automates application deployments to Amazon EC2 instances.

First, create an IAM role for EC2 with the permissions required to pull images from ECR and write logs to CloudWatch.

Install and configure:

  • Docker Engine.
  • AWS CodeDeploy Agent.
  • Required operating system dependencies.

The CodeDeploy agent must be running on the target EC2 instances.

Create an application in AWS CodeDeploy:

Application Name: 2048-game-app Compute Platform: EC2/On-Premises 

Create a deployment group and configure the target EC2 instances using tags or an Auto Scaling Group.

For production deployment, use an Auto Scaling Group spanning multiple Availability Zones and configure a load balancer.

Step 7: Configure appspec.yml

The appspec.yml file tells CodeDeploy how to deploy the application.

Example:

version: 0.0 os: linux files: - source: image_uri.txt destination: /opt/2048 hooks: ApplicationStop: - location: scripts/stop_container.sh timeout: 60 runas: root ApplicationStart: - location: scripts/start_container.sh timeout: 300 runas: root 

Step 8: Create Deployment Scripts

The deployment scripts control the Docker container lifecycle.

stop_container.sh

#!/bin/bash docker stop 2048-game || true docker rm 2048-game || true 

start_container.sh

#!/bin/bash IMAGE_URI=$(cat /opt/2048/image_uri.txt) aws ecr get-login-password --region YOUR_REGION | docker login --username AWS --password-stdin YOUR_ACCOUNT_ID.dkr.ecr.YOUR_REGION.amazonaws.com docker pull "$IMAGE_URI" docker run -d \ --restart unless-stopped \ -p 8080:80 \ --name 2048-game \ "$IMAGE_URI" 

Replace the region and account ID placeholders with your actual AWS configuration.

Make sure the scripts have executable permissions in the source repository.

Important: This simple deployment script demonstrates the container lifecycle. For a production environment, configure health checks and a rolling or blue/green deployment strategy. Stopping the old container before starting the new one can cause downtime on a single instance.

Step 9: Create AWS CodePipeline

AWS CodePipeline coordinates the complete CI/CD workflow.

Create a new pipeline:

Pipeline Name: 2048-game-cicd 

Configure the following stages.

Stage 1: Source

  • Select GitHub through AWS CodeConnections.
  • Connect your GitHub account.
  • Select the repository.
  • Select the main branch.

Stage 2: Build

  • Select AWS CodeBuild.
  • Choose the previously created build project.
  • Configure the output artifact.

Stage 3: Deploy

  • Select AWS CodeDeploy.
  • Choose the application 2048-game-app.
  • Select the deployment group.

Save the pipeline.

The pipeline will now automatically execute when new code is pushed to the configured GitHub branch.

Step 10: Configure Application Load Balancer

An Application Load Balancer provides a single entry point for users accessing the application.

Configuration:

  1. Open the EC2 Load Balancers console.
  2. Create an Application Load Balancer.
  3. Select internet-facing.
  4. Choose public subnets across two Availability Zones.
  5. Create a target group for the EC2 instances.
  6. Configure the target port as 8080.
  7. Configure the health check path as /.
  8. Register the EC2 instances.
  9. Configure the HTTP or HTTPS listener.

For production, use HTTPS with an AWS Certificate Manager certificate.

The EC2 security group should allow port 8080 only from the ALB security group.

Step 11: Monitor the CI/CD Pipeline

Amazon CloudWatch helps monitor application and infrastructure performance.

Monitor:

  • EC2 CPU utilization.
  • EC2 network activity.
  • CodeBuild build logs.
  • CodeDeploy deployment events.
  • Application health.
  • Load Balancer healthy and unhealthy targets.

Configure CloudWatch alarms to detect failures and performance issues.

8. CI/CD Pipeline Workflow

After configuration, the complete workflow will operate automatically.

  1. A developer pushes new code to GitHub.
  2. CodePipeline detects the source change.
  3. CodeBuild builds the Docker image.
  4. Basic application validation is performed.
  5. The image is pushed to Amazon ECR.
  6. CodeDeploy receives the deployment artifact.
  7. The EC2 instance pulls the new Docker image.
  8. The updated 2048 game container starts.
  9. The application becomes accessible through the Load Balancer.

9. Testing and Validation

After completing the deployment, verify the following:

  • [ ] GitHub repository is connected successfully.
  • [ ] CodePipeline starts automatically after a commit.
  • [ ] CodeBuild completes successfully.
  • [ ] Docker image is available in Amazon ECR.
  • [ ] CodeDeploy deployment succeeds.
  • [ ] Docker container is running on EC2.
  • [ ] Application Load Balancer reports healthy targets.
  • [ ] The 2048 game opens in a browser.
  • [ ] A new code change triggers another deployment.

Test the pipeline by changing a small element in the game, pushing the change to GitHub, and checking whether the updated application is deployed automatically.

10. Security Best Practices

Security is an important part of production-grade DevOps infrastructure.

Recommended practices:

  • Use IAM roles instead of hardcoded AWS credentials.
  • Keep EC2 instances in private subnets.
  • Restrict security group access.
  • Use HTTPS for application traffic.
  • Enable ECR image scanning.
  • Keep Docker base images updated.
  • Store secrets securely.
  • Enable CloudWatch logging.
  • Use encrypted EBS volumes.
  • Follow the principle of least privilege.
  • Use controlled deployment strategies and health checks.

11. Benefits of This Project

Feature Benefit
Docker Consistent application environment
CodePipeline Automated delivery workflow
CodeBuild Automated image building and testing
Amazon ECR Centralized image storage
CodeDeploy Automated application deployment
EC2 Application hosting
Load Balancer Traffic distribution
CloudWatch Monitoring and logging

12. Cost Considerations

This architecture uses multiple AWS services, and some services may generate charges even when the application receives little traffic.

Important cost factors include:

  • EC2 instance hours.
  • Application Load Balancer hours and processed traffic.
  • ECR image storage.
  • CodeBuild build minutes.
  • CodePipeline usage.
  • CodeDeploy deployment resources.
  • CloudWatch logs and metrics.
  • Data transfer.

Always check the AWS Pricing Calculator before deploying the infrastructure.

For learning purposes, use small instance types and delete resources when the project is no longer required.

13. Conclusion

In this project, we designed an automated CI/CD pipeline for a Dockerized 2048 game using AWS services.

By integrating GitHub, AWS CodePipeline, CodeBuild, Amazon ECR, and CodeDeploy, we automated the process of building, testing, storing, and deploying Docker containers.

This project demonstrates important DevOps concepts, including containerization, continuous integration, continuous deployment, infrastructure security, and application monitoring.

It is a valuable hands-on project for anyone learning AWS, Docker, Cloud Computing, and DevOps.

Technologies Used

  • Amazon Web Services (AWS)
  • Docker
  • GitHub
  • AWS CodePipeline
  • AWS CodeBuild
  • Amazon ECR
  • AWS CodeDeploy
  • Amazon EC2
  • Application Load Balancer
  • Amazon CloudWatch
  • Linux
  • CI/CD

Author: Muhammad Rizwan

Project: Automated CI/CD Pipeline for Dockerized 2048 Game on AWS

Tags: #AWS #DevOps #Docker #CICD