New Generation Elastic Beanstalk using Amazon Linux 2

Mounick
4 min readAug 29, 2020
Elastic Beanstalk Amazon Linux 2

Elastic Beanstalk is an orchestration service offered by Amazon Web Services which brings together AWS services like EC2, Auto Scaling, Load Balancer and RDS for the purpose of deploying elastic cloud applications.

Recently AWS has launched a new version of elastic beanstalk with Amazon linux 2 (AL2) ami which is often referred as new generation of elastic beanstalk. Recently, I had an opportunity to work with this new platform during which we faced some challenges/troublesome to adapt to this new platform. Since it is new, there are not much help available in the internet. AWS documentation is also not much clear. So, I decided to write a blog about this.

First, we will see what are changes in this new version

  • Old platform hooks in the ebextension ( like yaml scripts in predeploy and postdeploy directories) don’t work in the new platform. You should migrate any hooks to the new platform hooks format. We will come to this new platform hooks format later in this blog.
  • Environment variables are not loaded in the shell. Which makes difficult to run application commands ( like rails command) manually in EC2 machine. This is not mentioned any where in the AWS documents. I am not sure whether it is intended. If not, probably they may fix this in upcoming versions. But I have written a small script to load the environment variables we will come that later in this blog.

Now we will come this new format, similar to .ebextension you should create a directory named .platform in the root your project. And the .platform folder have the folder structure shown in the below diagram.

Directory Structure

If you want to override the default main nginx config, just place that nginx config file directly in the .platform/nginx folder and additional nginx configs in .platform/nginx/conf.dfolder. You don’t have to configure the overriding by playing with get-config elements as we did in the old version which eventually leads to a lot of troublesome. Now, Beanstalk will take care of that. Yes!!! it is a very useful feature.

Place the scripts which runs during the application deployment, under the .platform/hooks directory in your source bundle, in one of the prebuild, predeploy or postdeploy subdirectories based on your use case. Don’t copy that yaml file we used in the old version in ebextension and place it here, it can contain only binary files or script files starting with a #! line containing their interpreter path, such as #!/bin/bash. ( i.e you just need to place your bash script here)

Similarly place the scripts which runs during the configuration deployment, under the .platform/confighooks directory in your source bundle, in one of the prebuild, predeploy or postdeploy subdirectories based on your use case. You may wonder what did I meant by configuration deployment. Configuration deployment occurs when you commit a change in environment properties or platform-specific settings in the elastic beanstalk.

AWS also introduced the Buildfile and Procfile which should be placed in the root of your project. Use a Procfile for long-running application processes that shouldn't exit. Elastic Beanstalk expects processes run from the Procfile to run continuously and restarts any process that terminates. For short-running processes, use Buildfile .

Example of Buildfile

make: ./build.sh

Example of Procfile

web: bundle exec pumasidekiq: bundle exec sidekiq

you can check the status of these process by using the following commands systemctl status web.service and systemctl status sidekiq.service .

In additional to it you can add the commands and container_commands and cloudformation configs in .ebextensions/ folder similar to the old version.

So, the overall directory structure is

Overall File Structure

The following diagram illustrates the order of the hooks or commands that will run during the deployment process which will give you the idea of where to place your scripts.

Flow

Now coming to that environment variables problem, The environment variables are not loaded in the shell. So, when you ran the application commands like rails command it will result error. So, I wrote a small script to load the environment variables in the current shell. Please copy and commit this script in your project. Run this script before running your application commands.

Common Pitfalls:

  • Make sure all the hooks/script files are executable. Use chmod +x to set execute permission on your hooks/script files.
  • Use Buildfile, Procfile, and platform hooks to configure. .ebextensions configuration aren't as easy to work with. For example, writing command scripts inside a YAML file can be challenging from a syntax standpoint.
  • Make sure that you ran the env load script before running your application commands directly in the ec2 machine,

Conclusion

Overall, this new version simplifies the configuration of the elasticbeanstalk. But if you are already adapted to older version then it might take some time to get used to this new version.

Writer: Mounick

Shoot your questions in the comments section. I will try to reply as soon as possible.

#StayHome #StaySafe

--

--

Mounick

DevOps Engineer | Technical Blogger | Serverless Evangelist | Cloud Architect