Elastic Beanstalk is the PaaS component of AWS, it will automatically handle the details of capacity provisioning, load balancing, scaling, and application health monitoring.
We will go to AWS Elastic Beanstalk Documentation, look for “Install the EB CLI” to install the eb cli on the cloud9 environment. At the time of writing it was just cloning the Github repo, running the python installation script and adding ‘eb’ to path:
1 2 3 |
git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git python ./aws-elastic-beanstalk-cli-setup/scripts/ebcli_installer.py echo 'export PATH="/home/ec2-user/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile |
To test it we will create a simple flask page, but let’s prepare the environment first:
1 2 3 4 5 6 |
mkdir eb-flask cd eb-flask python3 -m venv virt source virt/bin/activate pip install flask pip freeze > requirements.txt |
Now to add the following simple flask app to app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from flask import Flask from flask import jsonify app = Flask(__name__) @app.route('/') def hello(): """Return a friendly HTTP greeting.""" print("I am inside hello world") return 'Hello World! CD' @app.route('/echo/<name>') def echo(name): print(f"This was placed in the url: new-{name}") val = {"new-name": name} return jsonify(val) if __name__ == '__main__': app.debug = True app.run() |
Then just run the app with
1 |
python app.py |
Then we can just test the app using curl in a new terminal, like this:
Now we can deploy the app, we just need to make sure AWS EB ignores the virtual environment we created earlier:
1 2 |
touch .ebignore echo "virt" > .ebignore |
Now we can initiate the deployment:
1 2 |
eb init -p python-3.7 flask-tutorial eb create flask-tutorial |
AWS EB will now create all needed components we would otherwise needed to create manually:
- EC2 instances (VPS)
- Security group (firewall rules to the VPS)
- S3 bucket (storage)
- CloudWatch alarms (performance monitoring for up or down scaling)
- CloudFormation stack (all the resources required to run a web application, such as a web server, a database, and networking rules)
- Domain name