Elastic Beanstalk is a serverless platform (Platform-as-a-Service) that allows you to focus on writing an application, leaving the infrastructure management and deployment completely to AWS. Since our application is likely to become extremely popular, being able to deploy it on something like Elastic Beanstalk will allow us to seamlessly scale when our service becomes popular. Elastic Beanstalk is very similar to Elastic Container Service, but in Elastic Beanstalk, AWS provides the environment (e.g. Python, NodeJS, Ruby) and all you need to do is provide the application code while in Elastic Container Service you provide both the application and the environment (via a container).

To begin with, we will first run the sample application. Visit the Elastic Beanstalk site from the console and click on "Create Application". Configure an application named "eb-hello", select Python for the platform and "Sample application" for the application code. Then, click on "Configure more options".

This brings you to a configuration page for the environment that the application will run on. We'd like to test out the auto-scaling features of the platform so we will configure the environment to support scaling. Click on the "High availability" preset. Then, find the "Capacity" tab and click on "Edit". Select a minimum of 2 instances and a maximum of 4 instances to serve your application.

Then, scroll down and click "Save". Finally, click "Create app" to launch the configuration.

Elastic Beanstalk will automatically create all of the infrastructure (e.g. EC2 instances, Load balancers, VPCs, subnets, security groups, etc.) to run your application. This is done using the Cloud Formation service of AWS. Observe the output of Cloud Formation as it scrolls across to see the kinds of resources that are automatically set up for you to run the application and pause to appreciate how fortunate we are not to have to do it manually. Wait until the process completes (~5 minutes). At the top of the log message output, you will get a message confirming completion of the launch:

Then, you'll be automatically redirected to the application's console.

From the EC2 console, select one of the instances running your environment and terminate it.

Doing so will cause the number of instances to fall below our minimum of 2. Wait several minutes, using the Refresh icon in the upper-right hand corner of the EC2 UI to update the status. Elastic Beanstalk will notice the missing instance and launch a replacement. Take a screenshot of the replacement VM being started.

The beauty of managed platforms is that, as a developer or an operations engineer, you never have to touch any of the infrastructure to scale your application out or handle failures. Unfortunately, since we have a limited budget given to us, we'll need to bring down our application. If all we think to do is delete the EC2 instances to clean up, we'll run into billing sadness as Elastic Beanstalk will immediately bring the instances back up. In order to stop our application, go back to the Elastic Beanstalk console and click on the "eb-hello" application. Then in "Actions", select "Delete application" to bring down the application, its environment, and all of its deployed resources.

We will now deploy our Guestbook application to Elastic Beanstalk. Before we can do so, we need to understand what our application needs in terms of access to AWS Services. As in prior labs, a role is assigned to the underlying EC2 instances that are running our application. As our Guestbook requires access to DynamoDB, we need to make sure that Elastic Beanstalk configures its instances' roles to allow it.

So, to begin with, visit the IAM landing page and click on "Roles". There are two roles that should appear since they have just been created automatically by Elastic Beanstalk to run our previous deployment:

Trust policies can be defined to allow services to take on particular roles in the cloud project. In this case, a policy is in place that allows the Elastic Beanstalk service to take on the "aws-elasticbeanstalk-service-role". This particular role gives Elastic Beanstalk the permissions it needs to automatically create the EC2 instances, scaling groups, security groups, and load balancers it needs to run the application. You typically wouldn't need to touch this role. The other role "aws-elasticbeanstalk-ec2-role" is the role the EC2 instances deployed by Elastic Beanstalk can take on when launched. Since our application requires access to DynamoDB, we need to add a policy to this role to allow it. To do so, click on the role and view the policies currently attached to it. None of those policies are what we need so click on "Attach policies"

Type "dynamodb" into the search box and click on "AmazonDynamoDBFullAccess", then on "Attach policy"

As with most AWS services, Elastic Beanstalk can be accessed both via the web console and via a command-line interface. As we've already done a deployment via the web console, we'll deploy our application via the CLI. Ultimately, in CI/CD (Continuous Integration, Continuous Deployment) pipelines building and deployment would be done via automated scripts.

To deploy our application, begin by bringing up a Cloud9 IDE using Amazon Linux as the Operating System. In a terminal within Cloud9, clone the source code, change into the directory containing the application, create a Python virtual environment, activate it, and then install the AWS Elastic Beanstalk command-line interface package.

git clone https://github.com/wu4f/cs430-src
cd cs430-src/05_aws_dynamodb
python3 -m venv env
source env/bin/activate
pip install awsebcli

Then, initialize your Elastic Beanstalk application, specifying its name, a Python 3.6 environment, and the us-east-1 region to run in.

eb init -p python-3.6 guestbook --region us-east-1

Then, create an execution environment to run your application. We can give Elastic Beanstalk some operating parameters in terms of how many resources to allocate to our application including the minimum and maximum number of instances to deploy to server the application. In this case, we would like to specify a minimum of 3 and a maximum of 10 machines.

eb create guestbook-env --min-instances 3 --max-instances 10

Note that you will get an error that the role aws-elasticbeanstalk-ec2-role can't be determined to exist. It does (we just created it), but Cloud9 doesn't have the permissions to list it. As the command output shows, the resources required for the deployment are created. Wait until the command fully completes before continuing.

We can now deploy our code into the environment we just created. To do so, issue the following command:

eb deploy guestbook-env

This will create a new application version and deploy it. Wait for the command to complete. Contemplate how long this is taking because it is bringing up virtual machines. Wouldn't it be better if those machines were already up and all you needed to do is deploy a container to them? Maybe this is why people are moving to a container world!

Visit the URL exported by the environment in the creation step and enter a message using your name, PSU e-mail address, and the message "Hello Elastic Beanstalk!". Take a screenshot of the Guestbook including the URL with the entry in it.

Then, visit the EC2 console to see that the specified minimum number of instances has been created and take a screenshot of them.

We will now destroy the infrastructure we deployed. Go back to the Cloud9 console and enter:

eb terminate --all

The application and environment will both be removed.