AWS DevOps & Developer Productivity Blog

Customize Ephemeral and EBS Volumes in Elastic Beanstalk Environments

Did you know that Elastic Beanstalk supports attaching and customizing ephemeral and EBS volumes to Elastic Beanstalk environments without the need for custom AMIs? This feature provides Elastic Beanstalk users with the ability to utilize:

1.     Ephemeral storage via instance store volumes available on the physical EC2 machine. See Instance Stores Available on Instance Types for details regarding volumes available on different EC2 instance types.

2.     EBS volumes for cases where the EC2 instance doesn’t support ephemeral volumes or the user requires that the data persist independent of the EC2 instance.

Today, we will take a closer look at how the user can accomplish the tasks listed above utilizing ebextensions.

Use ebextensions to attach and mount ephemeral volumes

The AWS Elastic Beanstalk team recently introduced a new option setting called “BlockDeviceMappings”. This option setting allows the user to attach ephemeral volumes to an Elastic Beanstalk environment.

For example, say that you have an Elastic Beanstalk environment that is running on a m1.large EC2 instance and you would like to attach an ephemeral volume to this instance so that you have extra storage available to allow your users to upload large files.

You could do so by adding the following code snippet to a configuration file in the .ebextensions folder in your deployable archive. This will allow you to use both of the 420GB volumes available on the m1.large instance.

commands:
  01mkdir:
    command: "mkdir /media/ephemeral0"
  02mkdir:
    command: "mkdir /media/ephemeral1"
  03mount:
    command: "mount /dev/sdb /media/ephemeral0"
  02mount:
    command: "mount /dev/sdc /media/ephemeral1"

option_settings:
  - namespace: aws:autoscaling:launchconfiguration
    option_name: BlockDeviceMappings
    value: /dev/sdb=ephemeral0,/dev/sdc=ephemeral1

In the above snippet, the “BlockDeviceMappings” option setting maps ephemeral volume 0 to “/dev/sdb” and volume 1 to “/dev/sdc”. The commands which are executed in alphabetical order (01mkdir, 02mkdir, 03mount and 04mount) mount and map the ephemeral volumes to the “/media/ephemeral0” and “/media/ephemeral1” folder respectively.

Use ebextensions to attach and mount EBS volumes

The “BlockDeviceMappings” option setting can also be used to attach EBS volumes to an Elastic Beanstalk environment.

For example, say that you have an Elastic Beanstalk environment to which you would like to attach two EBS volumes, a new 100GB EBS volume and another with a snapshot ID of “snap-51eef269”.

You could do so by adding the following code snippet to a configuration file in the .ebextensions folder in your deployable archive. This will allow you to attach and mount both EBS volumes to your Elastic Beanstalk environment.

commands:
  01mkfs:
    command: "mkfs -t ext3 /dev/sdh"
  02mkdir:
    command: "mkdir /media/volume1"
  03mkdir:
    command: "mkdir /media/volume2"
  04mount:
    command: "mount /dev/sdh /media/volume1"
  05mount:
    command: "mount /dev/sdj /media/volume2"

option_settings:
  - namespace: aws:autoscaling:launchconfiguration
    option_name: BlockDeviceMappings
    value: /dev/sdh=:100,/dev/sdj=snap-51eef269

In the above snippet, the “BlockDeviceMappings” option setting maps an new 100GB EBS volume to “/dev/sdh” and an EBS volume with the snapshot ID “snap-51eef269” to “/dev/sdj”. The commands which are executed in alphabetical order (01mkdir, …, 05mount) format the new EBS volume, mount and map both the EBS volumes to the “/media/volume1” and the “/media/volume2” folders respectively.

Summary

The “BlockDeviceMappings” option setting allows the user to map ephemeral and EBS volumes to Elastic Beanstalk environments. A few use cases where this would be useful are:

•    Additional storage to support large file uploads (EBS or ephemeral)

•    Load some existing dataset (via EBS snapshot)

•    Run a local database (which is a pretty bad idea)

•    More storage for session state