Deploy a Dockerized Flask Application to AWS ECS using CircleCI and Terraform

Listen to this Post

In this article, Darryl R., a Cloud Solutions Architect, discusses the importance of automation in cloud provisioning and highlights the use of GitHub Actions, CircleCI, and Terraform for CI/CD and Infrastructure as Code (IaC). He emphasizes the benefits of using AWS Elastic Container Service (ECS) for running containers, as it eliminates the need to pay for a control plane and only charges for the compute resources used. The article also references a detailed example by Katoria Henry, which walks through the process of deploying a Dockerized Flask application to AWS ECS using CircleCI and Terraform.

You Should Know:

1. GitHub Actions for CI/CD:

name: CI/CD Pipeline
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python -m pytest

2. Terraform for AWS ECS:

provider "aws" {
region = "us-east-1"
}

resource "aws_ecs_cluster" "flask_app_cluster" {
name = "flask-app-cluster"
}

resource "aws_ecs_task_definition" "flask_app_task" {
family = "flask-app-task"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn

container_definitions = jsonencode([
{
name = "flask-app"
image = "your-docker-image"
essential = true
portMappings = [
{
containerPort = 5000
hostPort = 5000
}
]
}
])
}

3. CircleCI Configuration:

version: 2.1
jobs:
build:
docker:
- image: circleci/python:3.8
steps:
- checkout
- run:
name: Install dependencies
command: pip install -r requirements.txt
- run:
name: Run tests
command: python -m pytest

4. AWS CLI Commands for ECS:


<h1>Create an ECS cluster</h1>

aws ecs create-cluster --cluster-name flask-app-cluster

<h1>Register a task definition</h1>

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

<h1>Run a task</h1>

aws ecs run-task --cluster flask-app-cluster --task-definition flask-app-task --count 1

5. Docker Commands:


<h1>Build a Docker image</h1>

docker build -t your-docker-image .

<h1>Run a Docker container</h1>

docker run -d -p 5000:5000 your-docker-image

What Undercode Say:

Automation is key to efficient cloud provisioning and deployment. Tools like GitHub Actions, CircleCI, and Terraform streamline the CI/CD process, while AWS ECS provides a cost-effective solution for running containers. By leveraging these technologies, you can ensure consistent and reliable deployments. Additionally, mastering AWS CLI and Docker commands will further enhance your ability to manage and deploy applications in the cloud.

For more detailed steps, refer to the original article: Deploy a Dockerized Flask Application to AWS ECS using CircleCI and Terraform.

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

Whatsapp
TelegramFeatured Image