Deploying a Simple Flask Application in the AWS Console with Elastic Beanstalk

Hands-On Lab

 

Photo of Fernando Medina Corey

Fernando Medina Corey

Training Architect

Length

00:30:00

Difficulty

Beginner

In this live AWS environment, we'll deploy an application in AWS using the Elastic Beanstalk service. We will start with an existing web application and deploy it using the AWS Console. The code for this Flask application can be found at this GitHub page

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying a Simple Flask Application in the AWS Console with Elastic Beanstalk

In this lab, we'll deploy an application in AWS using the Elastic Beanstalk service. We will start with an existing web application and deploy it using the AWS console.

To begin, log in to our lab environment as cloud_user with the password provided on the lab page. Make sure you are using us-east-1 (N. Virginia) as your region throughout the lab.

Under AWS services at the top, type "Elastic Beanstalk." It's also in the All services list (you can find it there by clicking All services and clicking Elastic Beanstalk under the Compute header).

Creating an Elastic Beanstalk Application in the AWS Console

Before we get started, click Services at the top and click VPC under the Networking & Content Delivery header. Click Your VPCs in the sidebar, click Actions, and click Create Default VPC. On the next screen, click Create and then click Close.

Now, head back to the Elastic Beanstalk console by clicking Services at the top and then Elastic Beanstalk under Compute. Click Get started in the Elastic Beanstalk console.

On the Create a web app page, give it an Application name of "PenguinApp." Under Base Configuration, click the Platform dropdown and select Python, since the code for this app is written in Python.

Open a new browser tab, and navigate to this GitHub page to get the code for our Flask application. Click the elastic-beanstalk folder, click csa-flask-app.zip, and click Download. This will download the .zip file to our Downloads folder, or whatever folder is configured to downloads on your machine.

Back in the Elastic Beanstalk browser tab, under Application code, select Upload your code, and click Upload. Select Local file as the Source code origin. Click Choose File, and upload the csa-flask-app.zip from your local machine. Click Upload. Now, click Create application.

The creation process will take a few minutes, so let's look at what's happening in the process. At the top, we'll see it's creating an Elastic Beanstalk environment, and then we'll see it's using an Elastic Beanstalk naming convention to create an Amazon S3 bucket for the environment's data.

Click Services at the top, and right-click S3 under Storage to see our S3 buckets in a new browser tab. On the Amazon S3 page, we should see there's now an Elastic Beanstalk bucket. There's also another bucket, but we don't need to worry about that one, as it has to do with how we create our live AWS environment for this lab. We can ignore it for the time being, since it has nothing to do with our Elastic Beanstalk application.

Back in the Create Environment browser tab, we should see that in addition to the S3 bucket, the environment health checks have started, meaning Elastic Beanstalk is keeping track of whether or not users would technically be able to access this web application. We'll also see it's created a security group, so let's check that out.

Back in the S3 Management Console tab, click Services and click EC2 under Compute. Click Security Groups under Network & Security in the sidebar. Click the box next to Penguinapp in the list, and click the Inbound tab at the bottom, where we'll see the security group is allowing inbound traffic on HTTP through the TCP protocol with a port range of 80. It's also configured to allow traffic from any source, so any IP range, anywhere on the open internet. We're going to spin up an EC2 instance inside this security group.

Back to the Create Environment browser tab, we'll see it's created an EIP (Elastic IP), which will allow us to access our application eventually.

It might still be waiting for instances to launch before it does anything else. It should already have added an instance to our environment, so head back to the EC2 Management Console tab. Click EC2 Dashboard at the top of the sidebar, and then click Running Instances. We should now see we have a running instance as part of our application. Under Description at the bottom, we'll see this instance's public IP, which we'll use in a minute to try and access our application.

Back to the Create Environment browser tab, make sure the instance created with our environment is the same as the one we saw listed in the EC2 Management Console by cross-referencing the instance IDs listed in both places.

Once the instance is created, we should see the environment health transitioned from "Pending" to "Ok," which means the instance spun up and the pings of that instance told us it was able to be an instance that's launched and serving traffic.

Go back to the EC2 Management Console, copy and paste the public IP into a new browser tab, and we should now see we've successfully created a Flask web application inside our browser from this EC2 instance.

After everything is finished up, we should be redirected to an Overview page that lets us know it's all set. Here, we'll see info related to the health of the application and some other details, including the version of Python it's running and the version of the operating system that's running underneath that.

At the top here, we should see a URL listed after the environment ID. The Elastic Beanstalk DNS name here is just the name we gave our application in Elastic Beanstalk, followed by some randomized characters, the region in which we're creating this application, and the elasticbeanstalk.com domain. Essentially, AWS hosts all of this underneath several subdomains of elasticbeanstalk.com.

Conclusion

That's pretty much all we have to do to create a Flask application on AWS' Elastic Beanstalk service. For the purposes of this lab, we're not going to dive too deep into the different quirks involved with creating our Flask application, so check out our Certified AWS Developer course if you'd like to learn more.

We should now have a good idea of how the pieces of Elastic Beanstalk fit together, as well as how we can deploy our own AWS Elastic Beanstalk applications, such as the Flask application we just created.

Good job completing this lab!