Template repository for a Continuous Delivery Flask application deployment with AWS ElasticBeanstalk
- Create a GitHub repository and:
- Add a README.md and a .gitignore file with a Python template
- Copy your repository SSH link
git@github.com:[your_github_username]/aws-eb-flask.git
under 'Code > Clone > SSH'
- Initiate an AWS Cloud9 cloud IDE and cd to your environment folder
$ cd ~/environment/
- Install the ElasticBeanstalk CLI from AWS current setup
$ git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git
$ python ./aws-elastic-beanstalk-cli-setup/scripts/ebcli_installer.py
- Ensure
eb
is inPATH
$ echo 'export PATH="/home/ec2-user/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile
- You can now check your currently installed ElasticBeanstalk CLI and current Python Interpreter
$ eb --version
EB CLI 3.20.3 (Python 3.7.1)
- Setup your encripted bi-direction communication with SSH keys
$ ssh-keygen -t rsa
It will place your identification and public key on your current home directory
Your identification has been saved in /home/ec2-user/.ssh/id_rsa.
Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub.
- Copy the public key. This is not a secret key and can be stored publicly
$ cat /home/ec2-user/.ssh/id_rsa.pub
- Under your GitHub account set up a new SSH Authentication Key
- Copy the output key from cat command
- Under Github Account > Settings > SSH and GPG keys > New SSH key
- Paste key and name it to identify your Cloud9 EC2 instance
- Confirm password / authentication
- Clone your repository and cd to its root folder
$ git clone git@github.com:{your_github_username}/aws-eb-flask.git
$ cd aws-eb-flask
- Initializie your base project structure with:
- flask_app.py
- test_flask_app.py
- Makefile
- requirements.txt
$ touch flask_app.py
$ touch test_flask_app.py
$ touch Makefile
$ touch requirements.txt
- Create a Python3 virtual environment and activate it
$ python3 -m venv venv
$ source venv/bin/activate
- Install the working version of Flask for your application
$ pip install flask==2.0.3
- pip freeze your current packages to your requirements.txt file and check its contents.
$ pip freeze > requirements.txt
$ cat requirements.txt
click==8.1.3
Flask==2.0.3
importlib-metadata==5.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
typing-extensions==4.3.0
Werkzeug==2.2.2
zipp==3.8.1
- Develop your base Flask Application. You can refer to the simple running example flask_app.py from this repository.
- You can now run locally and test your
flask_app.py
$ python flask_app.py
- And on another terminal
$ curl http://127.0.0.1:5000
Continuous Delivery Demo!
$ curl http://127.0.0.1:5000/echo/Erich
{
"new-name": "Erich"
}
Before starting a new EB Environment, make sure that your current .gitignore
or .ebignore
file indicates that the EB CLI should ignore your current .venv
folder when deploying. It will avoid pushing your local virtual environment to your EB referenced GitHub repository, which would lead to conflicts.
🔥🔥 Important: Note that if an .ebignore
file isn't present, but a .gitignore
is, the EB CLI ignores files specified in .gitignore
. If .ebignore
is present, the EB CLI doesn't read .gitignore
.
- Initializie your EB project
$ eb init -p python-3.7 eb-flask-app
- Create an EB environment
$ eb create eb-flask-app-env
This will set up your complete ElasticBeanstalk Environment within your initialized project with:
- EC2 Instance
- Load Balancer
- Auto Scaling
- CloudWatch Alarm
- S3 Bucket
- Corresponding Security Groups
You can now set up your Makefile for GitHub actions. You can find a sample Makefile in this repository. Remember to update the eb deploy
command with your app name.
Before running a make all
make sure to include these extra packages to your requirements.txt according to your Makefile:
pylint
pytest
black
pytest-cov
- Setup GitHub Actions
- In your GitHub repository, under Actions, select
set up a workflow yourself
. - Configure your main.yml file to make use of your Makefile scripts.
- In your GitHub repository, under Actions, select
Here is a sample workflow for Continuous Develivery with GitHub actions.