Deploying a Flask application on AWS with Gitlab
This is a basic introduction to deploying a simple hello world flask web application using AWS Beanstalk and Gitlab.
This tutorial assumes you have some knowledge of python, flask, and git. I will be going into more depth on AWS Elastic Beanstalk and Gitlab.
- an AWS account to deploy our web application
- Gitlab account where we can host our project code
- git installed
- python installed
Coding our Flask web application
The first step to code our simple “Hello, World!” application. In this process we will be using pip to install flask, and virtualenv to create an isolated python environment.
Creating our Beanstalk admin
Now that we have our flask application built, we can install the AWS Elastic Beanstalk CLI (awsebcli) to deploy our application. But before we do this we will create an IAM user specifically to manage the application. on AWS.
Head to AWS IAM and create a new user, and fill out the page like the image below:
After these two steps, the following steps you DO NOT need to configure anything. Lastly, when you press the ‘Create user’, make note of both your Access Key ID and Secret access key. We will need boths keys to use awsebcli to deploy our web application.
Working with AWS Elastic Beanstalk
Now that we create our admin to manage our Beanstalk application, we can now the awsebcli to deploy our web application. Using the CLI we can let AWS do all the work for us.
First time runing the EB CLI command, AWS will ask you to provide credentials to identify the account to manage the deploymeng. Remember the two keys I told you to keep note of earlier. Here is where we need to input it to connect the EB CLI to the our AWS EB Admin we created. It will also ask for the location of where you want your application to be hosted (e.g. us-east-1, us-east-2, etc.). Lastly, you can specify the format you want the output to be displayed in (e.g. json, yml, text, etc.).
Pushing our code to Gitlab
Now that we have our flask application fully working, Elastic Beanstalk environment initialized, we can now push our project to Gitlab to host everything.
After initializing git, we need to modify the .gitignore file to DELETE all three .elasticbeanstalk files.
Now we can push our code to Gitlab.
Creating our Elastic Beanstalk application
Finally, we can create our Elastic Beanstalk application by using the command:
Note: this process will take a couple mins to complete
There you go, our application is fully deployed to AWS Elastic Beanstalk.
So if we go to the URL listed at the top we get the same result as the localhost deployment.
This tutorial is a basic flask application deployed using AWS Elastic Beanstalk with Gitlab.
In the next part, we will dive deeper into using Gitlab CI/CD to continuously modify our project. Doing this we can run piplines of scripts to build, test, and validate code changes.