If you’re a developer and want to deploy your Flask based application on AWS Cloud without lots of manual intervention, then AWS Elastic Beanstalk is an easy-to-use service. It helps in deploying and at the same time scaling of web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS. In addition, we need not pay any additional charges for using Elastic Beanstalk –as we pay only for the AWS resources required for storing and running our applications.

Flask is a lightweight web application framework build on top of python. It can be integrated with a large number of extensions. Here are the some of the biggest sites created using the Flask framework: Pinterest, Twilio, Lyft, Patreon,etc.

Let’s see this in action:

In this blog, we’ll deploy a Flask &MySQL based web application. For that, we’ll create two environments – Development env. i.e. Cloud9 and Production env. i.e. EB.

Case 1: Deployment on Development Environment (Cloud9)

Cloud9 is a cloud IDE that is used for writing, running, and debugging code. First deploying our application on Cloud9, we need to launch our Cloud9 machine from the AWS console, as shown in image 1.

Then, after providing the configuration settings, AWS will spin up an Ubuntu-based IDE instance, as shown in image 2.

Once the IDE is running and you’re able to access it, it’s time to upload your application code in it (You can choose to build an application from scratch or take help from one of the open-source Github repositories). Instead of uploading your code, you may choose to zip your application code and drag and drop your folder, as shown in image 3. With Cloud9, you can run commands on the terminal and, at the same time, see your folder structure on the left to start development.

As our application is database-driven, it’s time to launch our RDS MySQL database, as shown in image 4. You can create a non-publicly accessible database and launch it in the same VPC where your Development Environment is running.

Now,though not a good practice, but to simplify the entire process, you can directly enter your database credentials within your code. However, in production environment,it is always advised to store your credentials in system variables and pass them within your code.

If you’re using Mysql with Flask, always declare a cursor class because that’s how you are going to fire queries. The cursor will hold records within the memory &Dict Cursor is a Cursor class that returns rows as dictionaries.

Before we proceed to test our application, we need to install certain packages. As we’re running our application on Ubuntu, below commands should meet our needs.

> pip3 install flask

> pip3 install wtforms

>sudo apt-get install libmysqlclient-dev

> pip3 install flask_mysqldb

> pip3 install passlib

Also, let’s define the host and post for our application:

if __name__ == ‘__main__’:

application.run(host=’0.0.0.0′,port=8080, debug=True)

As you can see,the port of application is 8080 but Cloud9 & Elastic Beanstalk can do port forwarding. So, now we can access our application from Port 80 i.e. HTTP.

We’ll openup our application.py file and click on the Run button. From the Preview, we can goto our application landing page, as in image 6.

The final results after deployment looks as in the image 7.

Great! Now let’s login to our application and see if our database was configured correctly.

Case 2:Deployment on Production Environment (EB)

Now that we have successfully deployed our application on the Dev environment, it’s time to deploy it over EB.But before that we need to take care of a few things:

We need to create a requirements.txt file and list all our application libraries. Thanks to the Pigar, which can generate requirements for the project.As shown in image 8,we need to run below commands on our Dev environment from the project root directory:

>pip3 install pigar

>pigar

Now that our requirements.txt has been created, let’s see what other prerequisites we have:

Elastic Beanstalk is able to find our application’s code very easily when we select application.py as the filename and enter a callable application object(in this case, the Flask object,). EB looks for an ‘application’ callable by default.That’s already been taken care of.

If you face 500 internal error, just declare a secret key below your application callable, as seen in image 9.

That’s all we need to do.Let’s download our application from Cloud9 environment& zip it in the root directory level. For that, we just need to select our files and child directories, and zip them.

Now it’s time to launch our Elastic Beanstalk instance.In the window that appears, just select Python as a platform, upload the code, and click on create an environment. As our application can communicate with our database, which is in the same VPC, no other configuration is required.Environment creation takes about 5 minutes and,finally, our application is up and running, as seen in image 10.

Let’s hit its URL!

As seen in image 11, this will open a browser window using the domain name created for your application. You would be able to see the same Flask website that you created and tested locally.

Hurray, you did it! In case you get stuck somewhere, start afresh – follow all the steps again and voila you’ll get the desired results.

References:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html

https://github.com/bradtraversy/myflaskapp

About The Author:

Mayank Singh is an Oracle Developer, accredited certified technical professional who has excellent ability to develop & implement technical solutions for any size of business and believe in learning each day. He loves to bring cloud automation, and his favorite automation tools are Terraform and Ansible. He is also an AWS Solutions Architect Professional. Besides, he is a big fan of Crayon Shin Chan and Marvel and DC movies.”

stats for wordpress