Running the Flask app on AWS - greendinosaur/flask-aws-template GitHub Wiki
There are a few steps to perform to run the Flask application on the AWS Elastic Beanstalk PaaS offering:
- Pre-requisites: Register for an AWS account and install some command-line tools
- Set-up Elastic Beanstalk
- Set-up the MySQL RDS Database
- Configure Auth0 and Elastic Beanstalk
- (Optional) Set-up an S3 bucket for storing Contact Us queries
- (Optional) Set-up an S3 bucket and configure CloudFront to act as a CDN
- (Optional) Register a domain name and configure Route 53 to serve your app
It can be run entirely on the AWS free-tier. Remember to terminate resources once you have finished using them. Otherwise, you may go over the Free tier limits and be charged.
Ensure that you have registered for an AWS account and also installed the AWS CLI and the AWS EB CLI. These are command-line tools that help you configure your AWS environments via the command line.
- Register for an AWS account
- Install and configure the AWS CLI
- Install and configure the EB CLI
Running the application on Elastic Beanstalk requires the creation of an application and an environment. The Flask application is deployed onto the environment where it is run.
Elastic Beanstalk provides many capabilities which makes it easier for you to focus on the core features of your application and less on the runtime environment. For example, it can automatically scale the number of environments if it detects increased load. You can easily deploy blue/green deployments with zero downtime on Elastic Beanstalk. The AWS documentation describes all of the capabilities in great detail and is worth a read if you're new to AWS and Elastic Beanstalk.
AS outlined above, ensure that you have an AWS account and have installed the AWS EB CLI
Once installed, you need to configure your application and environment.
Inside the flask-aws-template folder that contains the code, execute:
eb init
Follow the prompts to fully set up the EB CLI and to define the application.
- If you have not used AWS Command Line tools before, you will likely be asked for a username and password. For now, it is easiest to use the one used to register with AWS. It will persist these locally and will use the credentials to connect to AWS via the command line.
- Choose a suitable region where the application will be hosted
- Choose an application name. The defualt is fine.
- If it asks you about using Docker, say n
- Choose Python as the platform
- Choose Python 3.7 with Amazon Linux 2 as the runtime. This should be the default option.
- Choose n to using code commit
- Choose n to using SSH
You can now create your first environment: Enter
eb create
Follow the prompts and answer as:
- Say y to the default environment name
- Say y to the default prefix
- Say y to the default load balancer (Application)
- Say n to Spot Fleet
After answering these prompts, it will go about creating your environment. This will take a number of minutes.
As part of the initial environment creation, it will also deploy your code from your git repo. It will deploy the latest code on the master branch.
The final message on success will be something like Successfully launched environment xxx where xxx is the name of the environment.
Make a note of the security group listed on the line Created security group named: xxxx as you will require this security group in order to access the RDS database. See the later section for information on this.
To check all is okay, enter:
eb health
This will show the health of the new environment. All being well, it will show a Status of Ready and a Health of Green.
Make a note of the CNAME of your application. This is the URL for your application. It will likely contain the name of the application and the AWS region hosting the environment. This variable is required if you set-up Auth0 as the authentication mechanism for your application.
Execute the following command to launch the URL for the environment in your default browser
eb open
You should now see the default Flask app running successfully on the CNAME URL 😃 As the necessary environment variables to use Auth0 have not been set-up, you will not be able to login or register a new account just yet.
Details on setting up the database are described here
Ensure the database is set-up first before continuing with this section. This is because some of the user account data is persisted to the database. Setting up the database will ensure this data is not lost each time you restart the environment.
Follow the guidance inside the Introduction to Auth0 to create an Auth0 account.
When you configure your application on the Auth0 website, make sure you add the CNAME of your Elastic Beanstalk environment to the callback URLs. You can do this in addition to the 127.0.0.1 as multiple callback domains are allowed. Add:
-
http://<YOUR CNAME>/auth/callbackto Allowed Callback URLs e.g.http://my-flask-aws-template.us-west-1.elasticbeanstalk.com/callback -
http://<YOUR NAME>/welcometo Allowed Logout URLs e.g.http://my-flask-aws-template.us-west-1.elasticbeanstalk.com/welcome
Make a note of the Auth0 Client ID, Client Secret and Client Domain values.
Now set the environment variables within your AWS environment. This can be done through the EB CLI. Note the https:// in front of the client domain value.
eb setenv AUTH0_CLIENT_ID=<YOUR_CLIENT_ID> AUTH0_CLIENT_SECRET=<YOUR_SECRET_ID> AUTH0_CLIENT_DOMAIN=https://<YOUR_DOMAIN>
This will cause the environment to restart. Now when you view the site via your browser by entering eb open on the command line, you will be taken to Auth0 for authentication when you select Log In or Register. The user details such as email address, username and date of last login are persisted to the User table in the MySQL database
Well done! You now have a Flask application running on Elastic Beanstalk connected to a MySQL database within RDS and using an external SaaS service for user authentication.
You can now continue to learn how to use AWS S3 to store the queries raised in the Contact Us form on the website.