Deploying a Flask application on AWS with Gitlab CI/CD | Part 1

In addition to deploying a Flask application on AWS, I wanted to configure a Gitlab CI/CD pipeline to automatically deploy the changes made to my application. We will see step by step how it’s possible and what tools we need.

This will be divided into two parts. The first article will be dedicated to set up a small Flask application running on AWS. In a second article, we will play with the Gitlab CI/CD features to redeploy our code after each change !

Table of Contents

Pre-requirements

The requirements to follow this article are :

an AWS account to deploy our app

a Gitlab account that we will use to host our project code and also for the CI/CD part

a tool like virtualenv to cleanly isolate the Python version and the packages needed (I personally used Python 3.6)

Git installed

AWS features overview for web applications

The first step is to identify the most appropriate AWS service in order to deploy and host a Python web application. The first intuition you can have is to use an EC2 instance, which is a simple server hosted in the AWS cloud. Actually, a most robust solution exists named Elastic Beanstalk. This AWS service provides an “environment” which manage EC2 instances for you with preconfigured security settings, monitoring, automatic scalability, notifications when your server go down etc. In the future, if you want to add a database or other components to your application, Elastic Beanstalk offers an easy way to do this.

For me, one of the advantage of Elastic Beanstalk is the EB CLI, which is a specific command line tool (in addition to AWS CLI) which helps you :

create the Elastic Beanstalk environment ( eb create )

) deploy the application ( eb deploy )

) destroy the environment when you don’t need it anymore (eb terminate)

You can manage a lot of things through the EB CLI, without having to access the AWS console, and that’s what we are going to use to deploy our Flask application from Gitlab CI/CD.

Step 1 — Code a simple Flask application

First, we will create a simple “Hello world” application with Flask. First run the command pip install flask , then create a file application.py at the root of a new directory and paste the code below.

It’s important to keep application as the name of the file and even for the variable referencing our Flask application. In fact, it’s by default the way Elastic Beanstalk identifies where to find the application to run.

When running python application.py the Flask development server should be started on http://localhost:8080 !

Step 2 — Install the EB CLI

This is the offical documentation you can refer to for this step. Basically, this command gets the job done :

pip install awsebcli --upgrade --user

Step 3 — Create an IAM user to access Elastic Beanstalk from the CLI

AWS recommends not to use your root AWS account to perform tasks like managing Elastic Beanstalk environments or use any other services of Amazon plateform with it. Rather, you should consider creating a specific IAM user with limited roles (see the official documentation) and keep your root AWS locked by for example adding two-factor authentification and disabling key pairs associated.

Go on the AWS console and the “Services > IAM” tab. Click on the “Users” link and then on the create button. You should see this screen, just fill the form like the image below :

I named the user “ebadmin” but you can use the name you prefer. The important part is to check the “Programmatic access” checkbox, without it you are not able to use this user with the EB CLI we just installed.

Then, we should add some permissions to this new user. On this screen we can select each permission one by one, but good news : a specific group of users for Elastic Beanstalk adminstrators already exists with preconfigured permissions :

Just leave the “Set permissions boundary” as it is and click on the next button. A screen will ask you to confirm the creation of the user. After you have confirmed it, you will see more information about the user you just created (the link to access to the AWS console, the credentials for the programmatic access etc.). Keep these information somewhere as we will need it soon !

Step 4— Initialize the Elastic Beanstalk configuration files

In this part we will use the EB CLI we just installed to configure our Elastic Beanstalk environment. Just like when you use git init to initialize a Git repository and some configuration files associated, we will use eb init to create an Elastic Beanstalk repository with a config.yml configuration file located on the .elasticbeanstalk directory.

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

The first parameter simply tells which Python version we want to use (replace with your own), the second parameter is the application name (here flask-starter) and finally we can precise in which region the server instances will be running.

As it’s the first time we use this EB CLI, AWS needs you to provide credentials to identify your account. So the terminal will ask you the aws-access-id and the aws-secret-key , which are no more no less the credentials configured in the step 3.

Step 5 — Push the code to a new Gitlab repository

Now, we can push our code to a brand new Gitlab repository, as we will further use the Gitlab CI/CD features. First, create a new project on Gitlab and run git init on your local directory if not already done.

Before pushing our code, we have to be sure that the .elasticbeanstalk directory is not ignored. Check your .gitignore file and remove these lines automatically added by the eb init command :

# Elastic Beanstalk Files

.elasticbeanstalk/*

!.elasticbeanstalk/*.cfg.yml

!.elasticbeanstalk/*.global.yml

In fact, the .elasticbeanstalk directory is important as the Gitlab CI/CD runner will need to access the EB configuration files to redeploy our application, in the same way we’ve done it on our local machine !

Now you can push your code :



git commit -m "init commit"

git remote add origin

git push -u origin master git add .git commit -m "init commit"git remote add origin https://gitlab.com/<your-username>/<repo -name>git push -u origin master

Step 6— Create the Elastic Beanstalk environment and deploy the code

Feeew, three more commands and we are done. We will create the Elastic Beanstalk environment with the following command. It will ask you the environment name, DNS CNAME and the load balancer type, just press enter to keep the default values for now :

eb create flask-starter-dev

With the configuration of the previous steps, this command line will create an environment named “flask-starter-dev” and it will send a zip archive containing our Python code.

The next step is to deploy our code, nothing more simple :

eb deploy

Note that in order to deploy the code, it should be pushed in your remote Git repository (as seen in step 5). By default, the deploy command creates a zip archive from your last commit pushed (for small changes, you can also use the --staged option which deploys only the local staged code).

After few minutes, you should be able to open your running environment by executing this command :

eb open

A new tab should open on your browser with our Hello world page, pretty amazing, right ? Few commands later and we are done deploying our application to AWS.

Access the Elastic Beanstalk envionment on the AWS console

If something went wrong, you can check the logs with the command eb logs so you will be able to identify the problem. If you are more a GUI-fan, by accessing the AWS console (remember with your IAM user) you can go on the “Services > Elastic Beanstalk” tab and you should see something like this :

By clicking on the green rectangle representing the Elastic Beanstalk environment, it can help you know the state of the environment you just created, the eventual issues, current updates, the deployed code etc.