Giter Site home page Giter Site logo

matthewcylau / python-flask-aws-terraform Goto Github PK

View Code? Open in Web Editor NEW
22.0 4.0 7.0 594 KB

Deploy a Python Flask app onto Amazon ECS on AWS Fargate with Terraform

Dockerfile 1.56% Python 24.68% HCL 68.22% Smarty 4.92% Shell 0.61%
aws python-flask terraform

python-flask-aws-terraform's Introduction

AWS Fargate Python Flask App

A reference project to deploy a Python Flask app onto Amazon ECS on AWS Fargate with Terraform. Inspired by this AWS tutorial

A todo app creating, and retrieving data from Amazon RDS

AWS Architecture

Pre-requisite

  • Make sure you have installed Python 3, pip, Terraform, AWS CLI, and configured a default AWS CLI profile (see doc here)
terraform -help # prints Terraform options
which aws # prints /usr/local/bin/aws
aws --version # prints aws-cli/2.0.36 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0
aws configure # configure your AWS CLI profile
python3 --version # prints Python 3 version
pip3 --version # prints pip version

Run/Build app locally

  • In a seperate terminal window, run docker-compose -f docker-compose.dev.yml up to run PostgreSQL database inside a container. Then, return to the original terminal window and run the following commands:
virtualenv -p /usr/bin/python3 venv # create new virtual environment venv
source venv/bin/activate # activate venv
pip3 install -r requirements.txt # installs python packages
python3 manage.py # visit app at http://localhost:5000/ping
deactivate # deactivates venv

Install new packages

pip3 install boto # installs new Python package
pip3 freeze > requirements.txt # updates requirements.txt

Run app in container

  • Run docker-compose up to start app in container. Alternatively, you may choose to build, and run the api app image:
docker build -t matlau/python-flask-aws:latest . # build Docker image
docker run -p 5000:5000 matlau/python-flask-aws # visit app at http://localhost:5000/ping

Configuration

  • Create an S3 bucket to store Terraform state. Populate bucket name in 01-main.tf

  • Create a secret on AWS Secrets Manager named RDSPostgresPassword with key POSTGRES_PASSWORD, and your database password as value

  • Populate terraform.tfvars:

default_region      = "<YOUR_AWS_DEFAULT_REGION>"
app_name            = "<GIVE_YOUR_APP_A_NAME!>"
environment         = "<ENVIRONMENT_NAME>"

Deploy AWS stack

cd deploy # change to deploy directory
terraform init # initialises Terraform
terraform apply # deploys AWS stack. See output for API url
terraform destroy # destroys AWS stack

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate

If you find this project helpful, please give a โญ or even better buy me a coffee โ˜• ๐Ÿ‘‡ because I'm a caffeine addict ๐Ÿ˜…

Buy Me A Coffee

License

MIT

python-flask-aws-terraform's People

Contributors

matthewcylau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

python-flask-aws-terraform's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.