DEV Community

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

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
Enter fullscreen mode Exit fullscreen mode
  • Navigate into the project.
cd game_of_life
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

AWS Infrastructure

Step 1: ECR Repository Setup

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

created ecr repo

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

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

  • Next, add permissions policies

Select: AmazonECSTaskExecutionRolePolicy
select policy

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

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
Enter fullscreen mode Exit fullscreen mode

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

ECS Cluster created

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

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 .
Enter fullscreen mode Exit fullscreen mode

To view the created image, run the docker command:

docker images
Enter fullscreen mode Exit fullscreen mode

Docker image in terminal

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
Enter fullscreen mode Exit fullscreen mode

Step 7: Tag and push image

docker tag game-of-life:latest $ECR_REPOSITORY_URI
docker push $ECR_REPOSITORY_URI
Enter fullscreen mode Exit fullscreen mode

Check ECR for the pushed docker image

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
                }
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Task Definition

Deploy Game Service

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

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

game cluster

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

svc name

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

Load balancing

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

create load balancing

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

deployed Service

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

Configuration and networking

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

loadbalancer endpoint

Congratulations! 🥳🥳🥳

Game Home

Game Options

Conclusion

Successfully deploying the Game of Life on AWS ECS showcases how traditional applications can be modernized using container technology and cloud infrastructure.

Through this deployment, we've leveraged AWS's managed container orchestration to ensure our application runs reliably and can scale as needed.

The combination of Django's robust web framework capabilities with AWS's infrastructure provides a stable platform for users to explore Conway's mathematical masterpiece, demonstrating the perfect blend of classical computing concepts with modern cloud architecture.

Top comments (0)