Joshua Cruz

Deploying a Flask application on AWS with Gitlab

February 15, 2020

Introduction

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.

Prerequisites

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
# application.py
from flask import Flask

application = Flask(__name__)


@application.route('/')
def hello_world():
    return 'Hello, World!'


if __name__ == '__main__':
    application.run(debug=True, port=8080)

EBS local helloworld

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: EBS user EBS groups

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. EBS keys

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.

1
eb init -p python-3.6 flask-starter --region us-east-2

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.). EBS format

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.

1
git init

After initializing git, we need to modify the .gitignore file to DELETE all three .elasticbeanstalk files.

Now we can push our code to Gitlab.

1
2
3
4
git add .
git commit -m "Initial commit"
git remote add origin https://gitlab.com/<your-username>/<repo-name>
git push -u origin master

Creating our Elastic Beanstalk application

Finally, we can create our Elastic Beanstalk application by using the command:

1
eb create hello-python-dev

Note: this process will take a couple mins to complete EBS dashboard

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. EBS helloworld

Conclusion

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.