Elastic Beanstalk (EB) supports developers in building and configuring the underlying infrastructure for web applications. Developers just need to upload code and the rest of the things such as capacity provisioning, building and configuring AWS resources such as EC2 instances, ELB, Auto Scaling, and application health monitoring will be taken care of by Elastic Beanstalk. Web applications developed in Java, PHP, .NET, Node.js, Python, Docker, Ruby, and Go are supported. As well as web servers such as Apache, Nginx, Passenger and IIS.
Using Elastic Beanstalk is straightforward, you just have to upload your source code bundle, for example, written in Java, then convert it into a .war file. Elastic Beanstalk automatically launches and configures the underlying infrastructure for running the application. The main purpose of Elastic Beanstalk is to set developers free from creating and configuring AWS resources and purely allow them to focus on application development. If they are comfortable creating and configuring AWS resources to host web applications, they can use CloudFormation to write templates and create stacks.
Elastic Beanstalk supports custom AMIs via Packer. This allows wrapping applications in a layer of abstraction such that Elastic Beanstalk itself would not need to support the specific language of an application and you can deploy also not supported languages.
- Application: Logical collection of EB components, including environment, versions, and environment configuration. You can imagine it as a folder.
- Application version: Specific source code version of the web application. It points to an S3 object containing deploy code. The application version is part of an application. Each application can have multiple versions.
- Environment: There are two types of environments, web servers to listen and process HTTP(s) requests and worker environments to process a background task that listens for messages on an SQS queue.
- Environment configuration: Set of parameters and settings, it defines how an environment and its associated resources will behave. Further details about configuration files will be explained later.
- Configuration template: Starting point for creating unique environment configurations. Environment configuration can be created or modified using the CLI or API.
Configuration files can be written in YAML or JSON format. These files must have a .config extension and be inside a folder called .ebextensions in the top-level directory of your application source code bundle. The use of .ebextensions is only supported for an Amazon Linux 1 environment.
Buildfile: For commands that run for short periods and then exit upon task completion.
Procfile: For long-running processes like starting your application. Place a file called Profile in the root directory of your application source.
Platform Hooks: Can be used to define custom scripts or executables that run at various stages when EC2 instances are provisioned. Store the files at .platform/hooks/prebuild, /predeploy, or /postdeploy depending on which stage they should be running.
Deploying Web Applications
You can use Elastic Beanstalk to upload an updated source bundle and deploy it to your environment or redeploy a previously uploaded version. Each deployment has a unique ID which starts at 1 and increments by one with each deployment and instance configuration change. Elastic Beanstalk provides several deployment methods:
- All at once: Deploys to all hosts concurrently. Avoid it for critical applications. As a result, the web application may be unavailable for a while. Suitable if quick deployments are important to you.
- Rolling: Deploys the new version in one batch of instances at a time. Avoids downtime and minimizes reduced availability, at a cost of longer deployment time. Suitable if you can’t accept any period of completely lost service.
- Rolling With Additional Batch: Launches an additional batch of instances. Then deploys the new version in batches. Rolling back requires a further batch which is not suitable for quick recovers from failure. Avoids any reduced availability with an even longer deployment time compared to the Rolling method.
- Immutable: Deploys the new version to a fresh group of instances before deleting the old instances. Allows to maintain full capacity without touching the production environment. The preferred option for mission-critical applications. Rolling back is very quick and easy.
- Blue/green deployment: To avoid unavailability of the web application, EB creates a new infrastructure and on successful deployment, it just changes the CNAMEs of the old environment to the new one to redirect the traffic. Rollbacks are quite easy as the URL just has to be changed to the old environment.
- Traffic Splitting: A canary testing deployment method. Installs the new version on a new set of instances. Forwards a percentage of incoming client traffic to the new application version for evaluation. Requires a further rolling back update, it’s also good for mission critical applications.
Elastic Beanstalk creates a newer application version upon uploading a newer source code bundle. Creating a newer version and not deleting the old unwanted application version leads to hitting the application version limit. As a result, it does not allow you to create any newer web application version. The default limits are:
- Applications: 75
- Application versions: 1,000
- Environments: 200
With the help of the application version lifecycle policy for an application, hitting an application version limit can be avoided.
Deploying RDS with EB
In the following section, you are going to deploy a python Flask application through Elastic Beanstalk. It is just a basic Hello World app, but it serves its purpose. So begin by creating a new file called application.py and insert the following code into it:
from flask import Flask application = Flask(__name__) @application.route('/') def hello_world(): return 'Hello, World!'
Next, create a file called requirements.txt in the same directory:
Make sure flask is installed in your environment and give it a go!
export FLASK_APP=application.py && flask run
Open up a browser of choice and navigate to the URL
And you should see a blank page with just “Hello, World!” as text. To finish the application, navigate to the project directory and zip all files together.
Now you can open up your AWS Console and look for the service Elastic Beanstalk then hit the button “Create a new environment” in the top right corner.
Select “Web server environment” and hit “Select”.
Give your application a name and select Python as the platform as well as the platform branch and platform version.
On the last section “Application code” choose “Upload your code” and look for the previously zipped file. Hit “Create environment” afterward.
It takes about 10 minutes till everything is set up and ready to use, so give yourself a break, grab a coffee and come back in a few minutes!
Below the environment name you see an URL that you can click on and it will navigate you to the ready to use Flask application.