How to get started with AWS Elastic Beanstalk

Elkana Mbiti
7 min readMar 14, 2023

--

An example of a resilient architecture

The example above shows one virtual private cloud (VPC) in a Region, with Public and Private Subnets in each of the two Availability Zones (AZs).

Depending on how you configure routing for the subnets, the result is either a public or a private subnet.

  • Public Subnet: This subnet is routed directly to an Internet gateway on the subnet’s Route Table. Elastic Beanstalk will deploy load balancers in these public subnets.
  • Private Subnet: Resources in a private subnet do not have direct access to the internet. They require a NAT Gateway or NAT Instance. The Private Subnet’s internet requests are routed to the NAT device as specified in the private subnet’s Route Table and pushed through the Internet Gateway.
  • NAT Gateway: This is an AWS-managed service and will incur some costs to run. See the AWS calculator for the pricing. The upside is that it is easy to set up and is fully managed. It is deployed in the public subnet and requests from the private subnet are routed to it as specified in the Route Table.
  • Internet Gateway: provides a path to the internet. It is attached to your* VPC.

Elastic beanstalk provides a UI functionality to upload and deploy your application to the cloud. In this example, we will try to set up and deploy the application programmatically using the CLI.

Assumptions:

  • You already have a working program and wish to deploy to AWS using AWS Elastic Beanstalk
  • You have created a VPC architecture as the image above or similar. Sometimes the VPC can span into a Local Zone. More on that later.
  • You can set up your architecture manually or using AWS CloudFormation. Actually, during deployment with AWS Elastic Beanstalk, CloudFormation is used to set up most of the other components automatically and deploy them. Components such as load balancers and Cloudwatch are set up during the Create leg by the Elastic Beanstalk.
  • You have already set up the AWS CLI

Follow the steps below to set up your application:

# cd into your directory
cd /path/to/my/project

# initiate elastic beanstalk into your project
eb init

# You will be prompted to specify a region you would like to deploy.
# Select the region where you have your VPV

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
19) eu-south-1 : EU (Milano)
20) ap-east-1 : Asia Pacific (Hong Kong)
21) me-south-1 : Middle East (Bahrain)
22) af-south-1 : Africa (Cape Town)
23) ap-southeast-3 : Asia Pacific (Jakarta)
24) ap-northeast-3 : Asia Pacific (Osaka)
(default is 3):

# You will be prompted to enter the name of your application:

Enter Application Name
(default is "HelloWorld"):

# Elastic Beanstalk will attempt to find the language of choice of your application

It appears you are using PHP. Is this correct?
(Y/n):

# based on you answer above elastic beanstalk will ask you the version of
# language you want to use for your application. In this example we are using php

Select a platform branch.
1) PHP 8.1 running on 64bit Amazon Linux 2
2) PHP 8.0 running on 64bit Amazon Linux 2
3) PHP 7.4 running on 64bit Amazon Linux 2 (Deprecated)
(default is 1):

# You will be asked to set up SSH.
# If you need to SSH into your application you can select Yes on this

Do you want to set up SSH for your instances?
(Y/n):

# Select key pair to use during deployment or create a new one

Select a keypair.
1) My Existing KeyPair
2) [ Create new KeyPair ]
(default is 1): 1

# This concludes setting up your application

This concludes with setting up your application. In some instances, EB may ask you to enter your user credentials (i.e. Access key ID and secret access key) if you haven’t done so already. It may also ask you to use CodeCommit if it detects that Git has been initiated in your project folder. You may reply accordingly. More on this later.

You will notice a new directory called .elasticbeanstalk . This directory contains a YAML file with the instructions that EB CLI needs to deploy your application. Here is a sample file:

branch-defaults:
default:
environment: null
group_suffix: null
global:
application_name: HelloWorld
branch: null
default_ec2_keyname: EC2 Tutorial
default_platform: PHP 8.1 running on 64bit Amazon Linux 2
default_region: us-west-2
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: null
repository: null
sc: null
workspace_type: Application

This YAML file contains instructions for the EB CLI in order for it to deploy your app. Now follow the steps below to deploy your application:

We will use eb create to deploy our application to Beanstalk. There is a somewhat longer process to use eb create and specify parameters as the command progresses, much like the setup procedure we saw above. However, EB CLI provides a bunch of flags that we can use to specify all we need in one go. See below:

# Enter this command to deploy your application
# The elastic load balancer will need to specify public subnets for reasons discussed above
# The application ec2 instances will be deployed in the private subnets for the same reasons

eb create dev-env --vpc.id my-vpc-id --vpc.elbsubnets public-subnet-A-id,public-subnet-B-id --vpc.ec2subnets private-subnet-A-id,private-subnet-B-id --vpc.securitygroup my-vpc-securitygroup

Deployments with elastic beanstalk will use a load balancer for assumed resiliency. This explains why our architecture has to have subnets in two different AZs.

During the deployment, CloudWatch Alarms, Auto-Scaling groups and their policies, load balancers, and EC2 instances for the application will be deployed automatically.

See below a sample console output for a successful launch:

Creating application version archive "app-230314_200944699279".
Uploading HelloWorld/app-230314_200944699279.zip to S3. This may take a while.
Upload Complete.
Environment details for: dev-env
Application name: HelloWorld
Region: us-west-2
Deployed Version: app-230314_200944699279
Environment ID: e-ftdspirtvg
Platform: arn:aws:elasticbeanstalk:us-west-2::platform/PHP 8.1 running on 64bit Amazon Linux 2/3.5.5
Tier: WebServer-Standard-1.0
CNAME: UNKNOWN
Updated: 2023-03-14 17:09:51.448000+00:00
Printing Status:
2023-03-14 17:09:50 INFO createEnvironment is starting.
2023-03-14 17:09:51 INFO Using elasticbeanstalk-us-west-2-286759865304 as Amazon S3 storage bucket for environment data.
2023-03-14 17:10:27 INFO Created security group named: sg-0aae155c8217f4feb
2023-03-14 17:10:27 INFO Created security group named: sg-031fa4505a4574910
2023-03-14 17:10:27 INFO Created target group named: arn:aws:elasticloadbalancing:us-west-2:286759865304:targetgroup/awseb-AWSEB-PWXSTQMWFR2M/ed8624defb892057
2023-03-14 17:10:42 INFO Created Auto Scaling launch configuration named: awseb-e-ftdspirtvg-stack-AWSEBAutoScalingLaunchConfiguration-UaWh4oxBf9L3
2023-03-14 17:11:28 INFO Created Auto Scaling group named: awseb-e-ftdspirtvg-stack-AWSEBAutoScalingGroup-1LQKY4FI3D9OM
2023-03-14 17:11:28 INFO Waiting for EC2 instances to launch. This may take a few minutes.
2023-03-14 17:11:44 INFO Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:286759865304:scalingPolicy:71389909-b941-4be3-b891-716d9c8600fa:autoScalingGroupName/awseb-e-ftdspirtvg-stack-AWSEBAutoScalingGroup-1LQKY4FI3D9OM:policyName/awseb-e-ftdspirtvg-stack-AWSEBAutoScalingScaleUpPolicy-ZcOWPVHUK6nm
2023-03-14 17:11:44 INFO Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:286759865304:scalingPolicy:faffd7f3-0561-4444-9b09-58e156229fd7:autoScalingGroupName/awseb-e-ftdspirtvg-stack-AWSEBAutoScalingGroup-1LQKY4FI3D9OM:policyName/awseb-e-ftdspirtvg-stack-AWSEBAutoScalingScaleDownPolicy-xi4PJdTLJQKn
2023-03-14 17:11:44 INFO Created CloudWatch alarm named: awseb-e-ftdspirtvg-stack-AWSEBCloudwatchAlarmHigh-OLOJYYA0B8D
2023-03-14 17:11:44 INFO Created CloudWatch alarm named: awseb-e-ftdspirtvg-stack-AWSEBCloudwatchAlarmLow-1RGC34NVROU0I
2023-03-14 17:11:59 INFO Created load balancer named: arn:aws:elasticloadbalancing:us-west-2:286759865304:loadbalancer/app/awseb-AWSEB-17BKS2EMTC6O2/e7919cac332d5ce4
2023-03-14 17:11:59 INFO Created Load Balancer listener named: arn:aws:elasticloadbalancing:us-west-2:286759865304:listener/app/awseb-AWSEB-17BKS2EMTC6O2/e7919cac332d5ce4/15b5a687fea38315
2023-03-14 17:12:07 INFO Instance deployment: You didn't include a 'composer.json' file in your source bundle. The deployment didn't install Composer dependencies.
2023-03-14 17:12:09 INFO Instance deployment completed successfully.
2023-03-14 17:12:40 INFO Application available at dev-env.eba-hvrvfvn8.us-west-2.elasticbeanstalk.com.
2023-03-14 17:12:40 INFO Successfully launched environment: dev-env

Your console view will be successful if you see this:

You can open your application using the link displayed in the console, which is the same link on your application Elastic Beanstalk dashboard. You can also use the command below from the console:

# open your application in the browser

eb open

# To view the application status

eb status

# To view the health of the computing resources created for you

eb health

# To terminate your application and all its associated resources
# You will be prompted to enter the name of your application

eb terminate --all

eb terminate — all” will delete the deployed application and all the computing resources the Elastic Beanstalk CLI has created for you such as load balancer and cloud formation from AWS. However, your VPC remains intact including NAT Gateway or NAT Instances. If you do not need these, you will have to clean up manually otherwise you may incur unnecessary costs.

If your application does not open, check the settings and associations of your NACL, security groups attached to your deployed instance, or the Route tables attached to your subnets. Also, check if the route tables allow inbound/outbound connections. On your machine, check internet connectivity and firewall settings that may block the specific URL.

Happy learning!

--

--

Elkana Mbiti

Builder | Writer | Software Engineer | Linux Administrator | Amazing gardener