4-deploying-a-flask-application-on-aws-with-docker-and-cicd-pipelines.html

Deploying a Flask Application on AWS with Docker and CI/CD Pipelines

In today’s fast-paced tech landscape, deploying applications efficiently is crucial for developers. One popular stack for developing web applications is Flask, a lightweight Python framework. When combined with Docker and AWS, it allows for scalable and manageable deployments. This article will guide you through the process of deploying a Flask application on AWS using Docker and setting up CI/CD pipelines to automate your deployment process.

What is Flask?

Flask is a micro web framework for Python, designed for simplicity and flexibility. It's ideal for small to medium-sized applications and provides ample opportunities for customization. With Flask, you can quickly set up the server-side components of your application, allowing you to focus on building features.

Use Cases for Flask

  • RESTful APIs: Flask is perfect for creating APIs due to its lightweight nature.
  • Prototyping: Rapid development of applications with minimal setup.
  • Microservices: Easily deployable components in a microservices architecture.

Why Use Docker?

Docker is a containerization platform that allows developers to package applications and their dependencies into a standardized unit called a container. This approach ensures that your application runs consistently across different environments.

Benefits of Docker

  • Isolation: Each application runs in its own container, avoiding conflicts.
  • Portability: Containers can run on any machine that supports Docker.
  • Scalability: Easily scale your applications by deploying additional containers.

Setting Up Your Flask Application

Before diving into deployment, let’s create a basic Flask application. Here’s a simple example:

Step 1: Create a Flask Application

Create a new directory for your project and navigate into it:

mkdir flask-docker-app
cd flask-docker-app

Create a file named app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask on Docker!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Step 2: Create a requirements.txt File

List the dependencies needed for your Flask app:

Flask==2.0.1

Step 3: Create a Dockerfile

Now, let’s create a Dockerfile to containerize your Flask application:

# Use the official Python image from the Docker Hub
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY app.py .

# Expose the port the app runs on
EXPOSE 5000

# Define the command to run your app
CMD ["python", "app.py"]

Step 4: Build the Docker Image

Now, you can build your Docker image:

docker build -t flask-docker-app .

Step 5: Run the Docker Container

To run your container, use the following command:

docker run -p 5000:5000 flask-docker-app

Your Flask application should now be accessible at http://localhost:5000.

Deploying to AWS

Now that our Flask application is containerized, we can deploy it to AWS using Elastic Container Service (ECS).

Step 1: Create an ECR (Elastic Container Registry)

  1. Log in to your AWS Management Console.
  2. Navigate to ECR and create a new repository named flask-docker-app.
  3. Authenticate your Docker client to your ECR:
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com
  1. Tag your Docker image:
docker tag flask-docker-app:latest your-account-id.dkr.ecr.your-region.amazonaws.com/flask-docker-app:latest
  1. Push your Docker image to ECR:
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/flask-docker-app:latest

Step 2: Create an ECS Cluster

  1. Go to the ECS service in the AWS console and create a new cluster.
  2. Choose the “EC2 Linux + Networking” cluster template and configure the settings as needed.

Step 3: Create a Task Definition

  1. Define a new task definition for your Flask application.
  2. Specify the ECR image URL and set the container port to 5000.
  3. Configure any required environment variables or networking settings.

Step 4: Run the Task

  1. Navigate to your ECS cluster and start a new task using the task definition created earlier.
  2. Ensure that the task's network settings allow public access to port 5000.

Setting Up CI/CD with AWS CodePipeline

To automate deployments, we can leverage AWS CodePipeline.

Step 1: Create a CodePipeline

  1. Go to the AWS CodePipeline service.
  2. Create a new pipeline, selecting your source repository (e.g., GitHub).
  3. Add a build stage using AWS CodeBuild to build your Docker image and push it to ECR.
  4. Finally, configure a deployment stage to deploy the updated image to ECS.

Step 2: Set Up CodeBuild

Create a buildspec.yml file in your project root:

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --no-include-email --region your-region)
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t flask-docker-app .
      - docker tag flask-docker-app:latest your-account-id.dkr.ecr.your-region.amazonaws.com/flask-docker-app:latest
  post_build:
    commands:
      - echo Pushing the Docker image...
      - docker push your-account-id.dkr.ecr.your-region.amazonaws.com/flask-docker-app:latest

Step 3: Test Your Pipeline

Push changes to your repository and watch CodePipeline automatically build and deploy your application.

Conclusion

Deploying a Flask application on AWS using Docker and CI/CD pipelines is a powerful approach for modern development. By following the steps outlined in this guide, you can set up a scalable and efficient deployment process that enhances your productivity and ensures consistent application delivery. Start building and deploying your applications today, and make the most out of the tools available in the cloud!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.