This article will be a quick walkthrough of how a CI/CD workflow would look like using some of the most popular cloud based tools. The tools we’ll make use of are:
- The Python Flask framework to display a simple web app
- Docker for build portability
- GitHub to store our code
- Travis to automate the building, testing and deployment process
- AWS Elastic Beanstack (EBS) to host the web app
At a high level, what we want to accomplish is an automated method which will be triggered by our code commit, build the Docker container with our app, do some basic sanity tests, and if those pass, automatically deploy the app in production on AWS.
The sample application I created is available at https://github.com/dendory/simpleflask and is a basic two-page web app that allows you to click a button, enter a username, and have it displayed on the page. The first step is to clone the git repo to your local machine and install the prerequisites. You need Python 3, along with the connix and flask modules. On CentOS 7 the commands would be:
yum install python3 docker pip3 install connix flask git clone https://github.com/dendory/simpleflask
Now if you go to the
simpleflask folder, you should be able to run the app on your system:
The web app should be accessible on http://localhost:80/ assuming no error occurred. You can also use the
dev.sh script to setup a Docker container locally. This will by default map the app to port 8080. You can take a look at the
Dockerfile to see how the container is created. The shell script basically just allows you to test this process locally.
Setting up GitHub and Travis
Now that you cloned the repository, you need to upload it to your own GitHub account. It’s free to sign up and then you can use the web interface to clone my repo to your account. After that, don’t forget to clone that URL to your local folder. This is what your repo should look like on GitHub:
You will next need to sign up to the Travis web site. You can use your GitHub credentials to log into Travis, and then if you go to the Settings page, you should see a list of all the repositories in your GitHub. Search for the one you just created and select it to enable Travis integration:
Setting up AWS Elastic Beanstack
Elastic Beanstack is basically a way to let AWS create an entire environment meant to run a specific type of app. With a single button, Amazon will create some auto-scaling EC2 instances, set up security groups, a public IP, and so on.
You will need an AWS account in order to create the Beanstack environment. On the AWS console, search for Beanstack in the search box and then click on the Create new application button. I called mine “simpleflask” and then on the next page created the “Simpleflask-env” environment using the default settings. For the platform type, select Docker. Your environment should take a couple of minutes to create, and then show up on the AWS console:
Note that you will need to gather a few settings to update the
file in your git folder. First, you will need the region you’re using,
the app name you selected, along with the environment name. Then, you
will need to find name of the S3 bucket that AWS created for you. Go to
the S3 page on the AWS console and look for the newly create bucket, and
copy the name in the file.
Setting up permissions
The last step to do before we can deploy from Travis to your newly created AWS environment is to create an IAM user with the right permissions. Go to the IAM page on the AWS console and create a new user, select Programmatic access, then assign it the AWSElasticBeanstalkFullAccess policy:
Copy the access key and secret from this user. Finally, go to your Travis page and click on the More options button on the right side of your application, then Settings. There, you can add environment variables. Create two new variables with the key and secret from the user you just created:
Testing our deployment
Once all of this is done, you can commit your changes to the code and a build process should be initiated on Travis. If everything went well, you should see a green color after a few minutes:
If it fails, you can look at the log on that page to find out what happened. Meanwhile, if you go to the Elastic Beanstack page on your AWS console you should see the environment being refreshed, and shortly you should be able to click on the URL link on the top of that page to see the running app.
All the actions that Travis does automatically are based on what’s defined in the
file. For example, we’re saying that we need a build process that
includes Docker, we specify the running command line arguments to use,
and we use the
curl command to test that the site is
actually reachable. Finally, you can see the details about the
deployment process, in this case using Beanstack.
Terminating the environment
Since AWS resources cost money, remember to terminate your application by going to the Elastic Beanstack page on your AWS console and clicking on Delete application from the menu.