Creating an Elastic Beanstalk Application Using the AWS EB CLI
In this live AWS environment you will deploy an application in AWS using the Elastic Beanstalk service. You will use an EC2 instance workstation to replicate a local development environment so you can avoid the complications of your local machine. From this instance, you will install software to interact with the Elastic Beanstalk service more easily (the EB CLI) and also prepare the application to be deployed by Elastic Beanstalk.
Creating an Elastic Beanstalk Application Using the AWS EB CLI
In this hands-on lab, we will deploy an application in AWS using the Elastic Beanstalk service.
We will use an EC2 instance workstation to replicate a local development environment so you can avoid the complications of your local machine. From this instance, we will install software to interact with the Elastic Beanstalk service more easily (the EB CLI) and also prepare the application to be deployed by Elastic Beanstalk.
This will simplify many of the API actions we'd need to take to get set up with an Elastic Beanstalk application.
Setting Up the Environment
To start, log in to the live environment as
cloud_user with the password provided. Make sure you're in the
us-east-1 region throughout the lab.
Navigate to Elastic Beanstalk via Services at the top and click Elastic Beanstalk (under Compute). We'll mostly interact with Elastic Beanstalk through our local terminal, so we can leave this here for now.
Now we need to sign in to the EC2 instance. Open a terminal window and use the credentials provided on the lab page. You can use the Mac Terminal app, or, if you're on a Windows machine, you will need an SSH client such as PuTTY. (Learn how to connect to an EC2 instance from a Windows PC here.)
In the terminal, enter:
Make sure to replace
PUBLIC_IP_ADDRESS with the public IP address of the EC2 instance listed on the lab page.
When the prompt asks if you're sure you want to continue connecting, enter
Then, paste in the password provided on the lab page.
Clear the screen by entering:
>Note: Feel free to
clear the terminal whenever you'd like — there will be some spots where there's several lines of text. If it seems confusing or cluttered,
clear will give you a nice, clean slate.
We're now logged in to the EC2 instance and the AWS console. So, let's get started.
Deploying a Python Flask Application to Elastic Beanstalk using the Elastic Beanstalk CLI
The first thing we need to do is clone the code from the GitHub repository for the course. Open a new browser tab and navigate to the GitHub page here.
Let's run the
git clone command in the terminal to clone the application.
git clone https://github.com/linuxacademy/cda-2018-flask-app.git
Next, enter the following command:
The app directory files in our terminal should match the list of files in the GitHub repo.
Feel free to close the GitHub browser tab and clear the terminal:
Next, we need to create a Python virtual environment. In the terminal, enter:
python3.6 -m venv venv
Once we run this, Python will install a virtual environment for us in the current directory.
To turn on our Python virtual environment so we can interact with it in this lab, enter:
Let's move into our Flask application. Enter:
If we were working locally, we'd need to install some requirements. We could do this by entering:
pip install -r requirements.txt
>Note: Since we're using an EC2 instance, technically we don't need to run this command because we're not going to be testing our Flask application locally. Plus, by default, Elastic Beanstalk will handle the
requirements.txt installation process for us. When working with Python inside Elastic Beanstalk, it will expect to find a
requirements.txt file, and then it will install all those requirements in that file into the environment the application runs in. But it's good to know this is a way you could install requirements if you wanted to run this Flask application yourself locally and deploy it again to Elastic Beanstalk.
Once this is finished running, we can install the Elastic Beanstalk CLI:
pip install awsebcli
Initializing Our Elastic Beanstalk Application
Now that we have the Elastic Beanstalk CLI set up on our machine, we can use its commands. Let's start by initializing the current repository as an Elastic Beanstalk repository. Enter:
Next, we'll go through a series of questions. First, we need to select a default region. We have to answer
us-east-1 because that's where we're going to deploy our application. Enter:
Then, we need to give our application a name. You can give it any name you'd like. In the lab videos, we'll call it cool-penguin-app. You may need to try a few times to find a unique one that hasn't already been set for another application in Elastic Beanstalk.
The next question asks if we're using Python. Enter:
Next, we need to specify a platform version. Since we're using Python 3.6, enter:
Now, we could set up AWS CodeCommit for source control, but since this particular lab is about Elastic Beanstalk, we'll answer no when it asks if we'd like to continue with CodeCommit. Enter:
Next, we could set up SSH for our instances, but for the purposes of this lab, we don't need to do that. In a real-world setting, though, we most likely would so we could log in and take a look at the instances if there were any issues. For now, enter:
List the contents of the directory:
It looks like nothing has really changed. But if we enter:
We'll see we now have a folder called
.elasticbeanstalk. List its contents:
We'll see a
config.yml file. Let's open that up:
Inside Nano, we'll see that the file has a bunch of different settings we just set in the configuration process, which will let Elastic Beanstalk know what to do with our application when we're trying to deploy it and get it running. We aren't going to make any changes here, so exit by pressing Ctrl+X.
We need to do a few more things before we can deploy our application. We need to set up a VPC or subnet manually or recreate the default VPC or subnet so our application can take advantage of those.
Let's avoid doing a bunch of configuration by entering some default commands.
aws ec2 create-default-vpc
aws ec2 create-default-subnet --availability-zone us-east-1a
We may see an error saying the availability zone already exists, depending on the state of the lab. As long as we run the command, we should be able to continue either way.
Creating and Deploying Our Application
Now, let's create our application. Enter:
We can leave the default name of our environment, which should essentially be the name we gave our app earlier, so just press Enter.
We can also leave the DNS CNAME prefix default. This is the name that will be present in the URL Elastic Beanstalk provides us. Press Enter.
Next, we need to select a load balancer type. We can leave this as the default classic load balancer:
Then press Enter to continue and allow Elastic Beanstalk to create a role for the application.
Elastic Beanstalk will now start creating our application. We can see the process as it's happening in the terminal, but we could also watch it in the AWS console.
Refresh the Elastic Beanstalk page in AWS to see our app name appear in the toolbar on that page. Click its name there, and then click YOUR-APP-NAME-dev in the dropdown. We should see the creation process happening there too.
It will take a few minutes for it all to finish up, but it's good to know the different ways we can see what's happening and what Elastic Beanstalk is managing for us. For example, we don't need to do any custom work creating security groups, load balancers, or Auto Scaling launch configurations.
This frees us up to keep track of developing our Flask application.
At the top of the creation window in AWS, click the URL and open it in a new tab to see where our application should be deployed.
We should see we can't yet access it because the IP address can't be found, which makes sense since Elastic Beanstalk is still creating it all for us.
Wait a few minutes for it all to finish up, and then try the URL again. Now we should see our application.
Congratulations! You've successfully deployed your own Flask web application to Elastic Beanstalk.
Elastic Beanstalk can support more than Flask applications — you can deploy many different kinds of applications to Elastic Beanstalk. So get out there and create!