Overview

Manual deployments do not scale. Each release requires building an image, tagging it, pushing it to a registry and updating a running service, and each step is a chance for human error.

This article documents a complete continuous integration and continuous deployment (CI/CD) pipeline on AWS. A containerized web application (the 2048 game, served by Nginx) is built, versioned and deployed automatically whenever code is pushed to GitHub.

The project is based on the "CI/CD Pipeline for the 2048 Game" lab from Tech With Lucy's 5 Advanced AWS Cloud Projects course. The implementation notes, code samples and recommendations below are my own.

Effort Approximately 2-3 hours
Estimated cost About $1 or less, provided resources are cleaned up
Compute model Serverless containers (AWS Fargate)

Objectives

  • Package the application as a Docker image
  • Store versioned images in a private registry (Amazon ECR)
  • Run the workload on Amazon ECS with the Fargate launch type
  • Trigger build and deployment automatically on every push to main
  • Apply least-privilege access between services using IAM

Architecture

The workflow is as follows:

  1. A developer pushes code to the GitHub repository.
  2. The push triggers AWS CodePipeline.
  3. AWS CodeBuild retrieves the code, builds the Docker image and tags it.
  4. The image is pushed to Amazon ECR.
  5. CodePipeline updates the ECS service. A new task starts with the new image and replaces the old task.

Services and responsibilities

Service Responsibility
AWS CodePipeline Orchestrates the source, build and deploy stages
AWS CodeBuild Builds the Docker image and pushes it to ECR
Amazon ECR Private container registry
Amazon ECS (Fargate) Runs and manages the container
Amazon S3 Stores build artifacts passed between stages
AWS IAM Controls access for each service

Prerequisites

  • An AWS account and an IAM user with programmatic access
  • Docker, Git and the AWS CLI installed and configured (aws configure)
  • A GitHub account

If you prefer not to install tools locally, a small EC2 instance accessed over SSH works equally well.

Implementation

1. Containerize the application

The application consists of static files, so a lightweight Nginx image is sufficient.

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

Validate locally before involving AWS:

docker build -t 2048-game . docker run -p 8080:80 2048-game 

2. Publish the first image to ECR

ECS requires an existing image before the service can start, so the first image is pushed manually.

aws ecr create-repository --repository-name 2048-game-repo --region <REGION> aws ecr get-login-password --region <REGION> \ | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com docker tag 2048-game:latest <ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/2048-game-repo:latest docker push <ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/2048-game-repo:latest 

3. Deploy to ECS Fargate

  1. Create an ECS cluster using the Fargate infrastructure.
  2. Register a task definition with a container named 2048-container, the ECR image URI and port 80.
  3. Create a service with one task, the default VPC and subnets, a public IP, and a security group allowing inbound HTTP (port 80).

Opening the task's public IP in a browser confirms that the manual deployment works. This gives a known-good baseline before automation is added.

4. Create the source repository

Push the project, including the Dockerfile and buildspec.yml, to a new GitHub repository on the main branch. This repository becomes the pipeline's source.

5. Configure CodeBuild (continuous integration)

CodeBuild reads its instructions from buildspec.yml:

version: 0.2 phases: pre_build: commands: - 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 - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME build: commands: - docker build -t $REPOSITORY_URI:latest . post_build: commands: - docker push $REPOSITORY_URI:latest - printf '[{"name":"%s","imageUri":"%s"}]' $CONTAINER_NAME $REPOSITORY_URI:latest > imagedefinitions.json artifacts: files: - imagedefinitions.json 

The generated imagedefinitions.json maps the container name to the new image and is consumed by the deploy stage.

Supporting configuration:

  • IAM service role: permissions to push to ECR, write artifacts to S3, write logs, and update the ECS service
  • S3 bucket: a globally unique bucket for build artifacts
  • Build project: connected to the GitHub repository, using the buildspec file and the environment variables AWS_ACCOUNT_ID, AWS_DEFAULT_REGION, IMAGE_REPO_NAME and CONTAINER_NAME
  • Privileged mode: must be enabled, as Docker builds require it

Run a manual build first. A successful build produces a new image in ECR and imagedefinitions.json in S3.

6. Configure CodePipeline (continuous deployment)

Create a pipeline with three stages:

Stage Provider Configuration
Source GitHub Repository, branch main
Build AWS CodeBuild The build project from the previous step
Deploy Amazon ECS Cluster, service and imagedefinitions.json

The pipeline runs automatically after creation and again on every push.

Validation

To verify the full workflow, the page heading was changed from 2048 to 2048 by <name>:

git add . git commit -m "Update game title" git push origin main 

CodePipeline started within moments of the push. Source, Build and Deploy completed without intervention, and once the new ECS task was running, the updated title was visible at the service's public IP.

During the Deploy stage, ECS starts a new task with the new image, then stops the old one, so the application stays available throughout.

Note that each new Fargate task receives a new public IP. For a stable endpoint, place an Application Load Balancer in front of the service.

Troubleshooting

Symptom Likely cause Resolution
Docker build fails in CodeBuild Privileged mode disabled Enable Privileged mode
Push to ECR denied Missing registry permissions Add ECR push permissions to the build role
Deploy stage fails Container name mismatch Make CONTAINER_NAME match the task definition exactly
Application unreachable Port 80 blocked or no public IP Update the security group and enable public IP
Task exits immediately Image or tag missing in ECR Verify the image URI and tag

Security recommendations

  • Grant each IAM role only the permissions it needs, scoped to specific resources, rather than broad full-access policies.
  • Treat access keys as secrets. Never post them in chats, screenshots or repositories. If a key is exposed, deactivate and delete it in IAM immediately and issue a new one.
  • Prefer short-lived credentials and IAM roles over long-lived access keys where possible.

Cost management and clean-up

Costs are low, but resources keep billing until removed. After testing, delete:

  1. The CodePipeline pipeline
  2. The CodeBuild project
  3. The ECS service and cluster
  4. The ECR repository
  5. The S3 artifact bucket
  6. IAM roles, policies and access keys created for the project
  7. Any EC2 instance used to run commands

Key takeaways

  • Automating build and deployment makes releases faster, repeatable and less error-prone.
  • Fargate removes the need to manage servers for container workloads.
  • Most failures trace back to IAM permissions, Privileged mode, or a mismatched container name, so check these first.

Recommended next steps

  • Add an automated test stage (unit tests or health checks) before deployment
  • Put an Application Load Balancer and HTTPS in front of the service
  • Add monitoring and alarms with Amazon CloudWatch
  • Define the infrastructure as code with Terraform or AWS CloudFormation

Thanks for reading. If you have questions or improvements, please share them in the comments.