Using GitHub Actions and Terraform to Deploy a Python Flask Web App on AWS Elastic Beanstalk

Posted by

Deploy Python Flask Web App On AWS Elastic Beanstalk Using GitHub Actions, Terraform

Deploy Python Flask Web App On AWS Elastic Beanstalk Using GitHub Actions, Terraform

If you have a Python Flask web app that you want to deploy to AWS Elastic Beanstalk using GitHub Actions and Terraform, you’ve come to the right place! In this article, we’ll show you how to set up everything you need to automate the deployment process and make your life easier.

Step 1: Create Your Python Flask Web App

Before you can deploy your web app, you’ll need to have it up and running locally. Make sure you have a working Python Flask app that you want to deploy. If you don’t already have one, you can create a simple “Hello, World!” app to test with.

Step 2: Set Up AWS Elastic Beanstalk

Next, you’ll need to create an AWS Elastic Beanstalk environment for your app. This can be done through the AWS Management Console or using Terraform to automate the process. Here’s an example Terraform configuration for an Elastic Beanstalk environment:

    
resource "aws_elastic_beanstalk_application" "flask_app" {
  name = "flask-app"
}

resource "aws_elastic_beanstalk_environment" "flask_app_env" {
  application = "${aws_elastic_beanstalk_application.flask_app.name}"
  name        = "flask-app-env"
  solution_stack_name = "64bit Amazon Linux 2 v3.4.5 running Python 3.8"
}
    
    

Step 3: Configure GitHub Actions

Now that you have your app and Elastic Beanstalk environment set up, it’s time to create a GitHub Actions workflow to automate the deployment process. You can use a workflow similar to the following example:

    
name: Deploy to AWS Elastic Beanstalk

on:
  push:
    branches:
      - main

jobs:
  deploy:
    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.x'

    - name: Install dependencies
      run: |
        python -m venv .venv
        source .venv/bin/activate
        pip install -r requirements.txt

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1

    - name: Deploy to Elastic Beanstalk
      run: |
        eb deploy
    
    

Step 4: Push Your Changes to GitHub

Once you have your GitHub Actions workflow set up, all you need to do is commit and push your changes to your GitHub repository. This will trigger the workflow and automatically deploy your Python Flask web app to AWS Elastic Beanstalk.

And that’s it! You now have a fully automated deployment process for your Python Flask web app using GitHub Actions and Terraform. With this setup, you can easily deploy updates to your app with just a simple push to your GitHub repository.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@dhananjaykulkarni8361
9 months ago

Greate video! Thanks @Cloud Quick Labs