Django’s Game of Life Meets AWS ECS – The Ultimate Deployment Hack!

PressRex profile image
by PressRex
Django’s Game of Life Meets AWS ECS – The Ultimate Deployment Hack!

Table of Contents

Introduction

Prerequisites

Project Setup

  • Project Structure

AWS Infrastructure

  • ECR Repository Setup
  • Export Environmental Variables
  • IAM Roles & Permissions
  • ECS Cluster Creation

Manually build and push image to ECR

  • Build Docker Image
  • Login to ECR
  • Tag and push image

Task Definition Configuration

  • Dynamically update task definition file
  • Register task definition

Deploy Game Service

  • Input Service Details
  • Load balancing

View Deployed Game

  • Access Load Balancer End-point

Introduction

The Game of Life, created by mathematician John Conway in 1970, is a fascinating example of cellular automation where simple rules create complex patterns.

Our project takes this classic simulation and implements it as a web application using Django.

By deploying it on Amazon Elastic Container Service (ECS), we're making this mathematical marvel accessible through the cloud, demonstrating how modern container orchestration can bring traditional concepts to life in a scalable, reliable way.

Prerequisites

  • AWS Account
  • AWS CLI configured
  • Docker installed
  • Git repository with the Game of Life code

Project Setup

  • View project repo Here
git clone https://github.com/UkemeSkywalker/game_of_life
  • Navigate into the project.
cd game_of_life

Project Structure

game-of-life/
├── Dockerfile
├── buildspec.yml
├── requirements.txt
├── manage.py
├── game_of_life/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
├── life/
│   ├── templates/
│   │   └── life/
│   │       ├── landing.html
│   │       ├── select_pattern.html
│   │       └── game.html
│   └── [other app files]
└── ecs/
    └── task-definition.json

AWS Infrastructure

Step 1: ECR Repository Setup

  • Create ECR Repo
aws ecr create-repository \
  --repository-name game-of-life \
  --image-scanning-configuration scanOnPush=true

Login to your AWS console and navigate to the ECR service to find the created ECR repository

Next steps, would be to export the needed environmental variables, run the below command

Step 2: Export Environmental Variables

export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
export AWS_REGION=us-east-1
export ECR_REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/game-of-life
  • Test Login Command
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

expected output should be Login Succeeded

Step 3: IAM Roles & Permissions

  • Login to your AWS console and create an IAM ROLE
IAM > Roles > Create Role > Use Case: Elastic Container Service > 
ECS Task > Select Policy: AmazonECSTaskExecutionRolePolicy

Trusted entity type: AWS service
Service: Elastic Container Service
Use Case: Elastic Container Service Task

  • Next, add permissions policies

Select: AmazonECSTaskExecutionRolePolicy

Role Name: ecsTaskExecutionRole
Then create the role, it should look similar to the below.

Step 4: ECS Cluster Creation
Run the below command to create the cluster

aws ecs create-cluster \
  --cluster-name game-of-life \
  --capacity-providers FARGATE

Once cluster is created, it should output data similar to the below

  • Confirm created cluster Login to your AWS console and navigate to ECS, you should see the created cluster.

Manually build and push image to ECR

Next step is to build and push the project docker image to ECS

Step 5: Build Docker Image

The Dockerfile, already exist in the root directory of the project.
Run the below command to build the image.

docker build --platform linux/x86_64 -t game-of-life .

To view the created image, run the docker command:

docker images

Step 6: Login to ECR

aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

Step 7: Tag and push image

docker tag game-of-life:latest $ECR_REPOSITORY_URI
docker push $ECR_REPOSITORY_URI

Check ECR for the pushed docker image

Task Definition Configuration

In the project directory, you would find the task definition file in ecs/task-definition.json

Navigate to the ecs directory in the project folder

cd ecs

Then run the below command to replace the placeholders with previously exported environmental variable values in the task-definition.json file.

Step 8: Dynamically update task definition file

sed -i '' "s/\${AWS_ACCOUNT_ID}/$AWS_ACCOUNT_ID/g" task-definition.json
sed -i '' "s/\${AWS_REGION}/$AWS_REGION/g" task-definition.json
escaped_uri=$(echo $ECR_REPOSITORY_URI | sed 's/\//\\\//g')
sed -i '' "s/\${ECR_REPOSITORY_URI}/$escaped_uri/g" task-definition.json

Below is the content of the task-definition.json file

{
    "family": "game-of-life",
    "networkMode": "awsvpc",
    "requiresCompatibilities": ["FARGATE"],
    "cpu": "256",
    "memory": "512",
    "executionRoleArn": "arn:aws:iam::${AWS_ACCOUNT_ID}:role/ecsTaskExecutionRole",
    "runtimePlatform": {
        "operatingSystemFamily": "LINUX",
        "cpuArchitecture": "X86_64"
    },
    "containerDefinitions": [
        {
            "name": "game-of-life",
            "image": "${ECR_REPOSITORY_URI}:latest",
            "portMappings": [
                {
                    "containerPort": 8000,
                    "protocol": "tcp"
                }
            ],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "/ecs/game-of-life",
                    "awslogs-region": "${AWS_REGION}",
                    "awslogs-stream-prefix": "ecs"
                }
            }
        }
    ]
}

Step 9: Register task definition

To create the task definition, run the below command while in the ecs directory.

aws ecs register-task-definition --cli-input-json file://task-definition.json

Login to your AWS console and navigate to ECS using the below path view the registered task definition:

Amazon Elastic Container Service > Task > definitions >game-of-life

Deploy Game Service

Login to the AWS console and navigate to our previously created cluster

Amazon Elastic Container Service > Clusters > game-of-life
  • In the service tab click on create

Step 10: Input Service Details

  • Environment section: leave everything as default
  • Deployment configuration: Only change the Family dropdown to the created task definition game-of-life and the Revision as latest
  • Service name: game-of-life-svc

Step 11: Load balancing

  • Scroll down and select the Use load balancing check box.
  • Load balancer type: select Application Load Balancer
  • Add the Load balancer name:game-of-life

Leave every other thing as default, and scroll to the end and click on create

View Deployed Game

Step 12: Access Load Balancer End-point

  • Once the service has been deployed, click on the service name game-of-life-svc

  • In the service section, select the Configuration and networking tab

  • Scroll down to Network configuration, copy the loadbalancer endpoint DNS names, and input it in your browser URL.

Congratulations! 🥳🥳🥳

View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More