Home » Automate Flask Deployment with AWS Copilot & Fargate

Automate Flask Deployment with AWS Copilot & Fargate

Deploy Automatic CICD web app using ECR, ECS into AWS

Introduction

When you are building modern, scalable web applications, automation isn’t just a luxury it is a necessity. In this hands-on tutorial, I will show you how to set up a complete CI/CD pipeline for a Dockerized Flask application using AWS services. You will learn how to containerize your app, push it to Amazon ECR, and deploy it to ECS Fargate with seamless load balancing through an Application Load Balancer (ALB). Best of all, we will use AWS Copilot to simplify the entire process so you can focus on development, not infrastructure.

Let’s break it down step by step.

Prerequisites

Before we dive in, ensure you have:

Step 1: Build a Simple Flask App

  • Let’s start by creating a minimal Flask app in app/app.py
Screenshot_2
  • Similarly create requirement.txt in apps/requirements.txt
Screenshot_4
  • Run locally 
Multi Copy Code Blocks
bash

pip install -r app/requirements.txt
python app/app.py   

    
  • Install git and push your code into github
Multi Copy Code Blocks
bash

git init
git add .
git commit -m "initial: flask app + Dockerfile"
git push -u origin master
    

Step 2: Dockerize the Flask App

  • Create a Dockerfile in the same directory:
Screenshot_3
  • Build and test your Docker image locally
Multi Copy Code Blocks
bash

docker build -t flask-ecs-app .
docker run -p 5000:5000 flask-ecs-app

    

Step 3: Initialize AWS Copilot

  • For macOS (Homebrew):
Multi Copy Code Blocks
bash

brew install aws/tap/copilot-cli
    
  • For Linux
Multi Copy Code Blocks
bash

curl -Lo copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux \
&& chmod +x copilot && sudo mv copilot /usr/local/bin/

    
  • For Windows
Multi Copy Code Blocks
bash

New-Item -Path 'C:\copilot' -ItemType directory; `Invoke-WebRequest -OutFile 'C:\copilot\copilot.exe' https://github.com/aws/copilot-cli/releases/latest/download/copilot-windows.exe

    

[Note: provide system path in environment variable]

Screenshot_1

Step 4 : Configure AWS CLI

  • Install and configure AWS CLI in your local machine. To see the process click here 

Step 5: Use Copilot to create app + service + deploy (fastest path)

Multi Copy Code Blocks
bash

copilot init 
    

Copilot will ask interactively:

  • Application : cicd-copilot
  • Workload name: select ALB web services
  • Service Name: web
  • Dockerfile Location: ./Dockerfile
  • Port: 5000
  • Deploy Now: Yes

Copilot will:

  • Create an ECR repo, build image locally, push to ECR
  • Create a VPC/subnets, ECS cluster, Task Definition, ALB, security groups, service (Fargate) and deploy
  • Create CloudWatch log groups

Note: If you want non-interactive: you can run similar with flags (copilot supports flags) interactive is easiest for first run.

  • After successfully init copilot then you output will be 
Multi Copy Code Blocks
bash

https://web-devops-blog-alb-123456789.ap-south-1.elb.amazonaws.com
    
  • Open that URL  you should see the JSON {“message”:”Hello from AWS DevOps project!”}.

Step 6 : Verify and view logs

  • Check the status of web 
Multi Copy Code Blocks
bash

copilot svc status --name web
    
  • Follow live logs 
Multi Copy Code Blocks
bash

copilot svc logs --name web –follow
    
  • You can also view task count or events
Multi Copy Code Blocks
bash

copilot svc show --name web
    

[Note: provide system path in environment variable]

Step 7 : Add CI/CD pipeline

  • Inside repo 
Multi Copy Code Blocks
bash

copilot pipeline init
    
  • It will prompt:
    • GitHub repository URL (enter GITHUB_USER/$GITHUB_REPO)
    • GitHub personal access token (create one in GitHub with repo scope; paste when asked)
    • Branch to track: main
    • Buildspec: Copilot can create a default buildspec.yml. Accept default.

After pipeline init you will get copilot/pipeline.yml file in your repo. Now, push that manifest file in your github.

Multi Copy Code Blocks
bash

git add copilot/pipeline.yml 
git commit –m “add copilot manifest file"
git push –u origin master 
    
  • Now, create and update pipeline in AWS

Multi Copy Code Blocks
bash

copilot pipeline update
    

This will create a CodePipeline (with CodeBuild etc.) and connect to GitHub. On creation it may mention a commit status or ask to configure webhook (Copilot guides interactively). After pipeline is ACTIVE, any push to main will trigger it.

To trigger pipeline manually, change a small thing (e.g., update welcome message) and git commit and git push.

  • Check pipeline status
Multi Copy Code Blocks
bash

copilot pipeline status
    
  • Finally , you have successfully deploy your app automatiic pipeline CI/CD using ECR, ECS (Farget) and ALB with the help of AWS copilot services 

One thought on “Automate Flask Deployment with AWS Copilot & Fargate

Leave a Reply

Your email address will not be published. Required fields are marked *