AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Custom Platforms

Elastic Beanstalk supports custom platforms that can be based on an Amazon Machine Image (AMI) that you create from one of the supported operating systems, Ubuntu, RHEL, or Amazon Linux (see the flavor entry in Platform.yaml File Format for the exact version numbers) and can include further customizations. Create your own Elastic Beanstalk platform with Packer, which is an open-source tool for creating machine images for many platforms, including AMIs for use with Amazon EC2. An Elastic Beanstalk platform comprises an AMI configured to run a set of software that supports an application, and metadata that can include custom configuration options and default configuration option settings.

You create a platform by providing Elastic Beanstalk with a Packer template, and the scripts and files that the template invokes to build an AMI. These components are packaged with a platform definition file, which specifies the template and metadata, into a ZIP archive called a platform definition archive.

When you create a custom platform, you launch a single instance environment without an EIP that runs Packer, which launches another instance to build an image. You can reuse this environment for multiple platforms and multiple versions of each platform.

Note

Custom platforms are region-specific. If you use Elastic Beanstalk in multiple regions, you must create your platforms separately in each region.

In certain circumstances, instances launched by Packer are not cleaned up and have to be manually terminated. To learn how to manually clean up these instances, see Packer Instance Cleanup.

Users in your account can use your custom platforms by specifying a platform ARN during environment creation. These ARNs are returned by the eb platform create command that you used to create the custom platform.

Each time you build your custom platform, Elastic Beanstalk creates a new platform version. Users can specify a platform by name to get only the latest version of the platform, or include a version number to get a specific version.

For example, to deploy the latest version of the custom platform with the ARN MyCustomPlatformARN, which could be version 3.0, your EB CLI command line would look like:

eb deploy -p MyCustomPlatformARN

To deploy version 2.1 your EB CLI command line would look like:

eb deploy -p MyCustomPlatformARN --version 2.1

Creating a Custom Platform

To create a custom platform, the root of your application must include a platform.yaml file, which defines the type of builder used to create the custom platform. The format of this file is described in the Platform.yaml File Format topic. As an alternative to creating your custom platform from scratch, you can use one of the sample custom platforms.

Using a Sample Custom Platform

One alternative to rolling your own custom platform is to use one of the platform definition archive samples to bootstrap your custom platform.

Note

Do not use a sample custom platform in production. The goal of the samples is to illustrate some of the functionality available for a custom platform, but they have not been hardened for production use.

NodePlatform_Ubuntu.zip

This custom platform is based on Ubuntu 16.04 and supports Node.js 4.4.4. We'll use this custom platform for the examples in this section.

NodePlatform_RHEL.zip

This custom platform is based on RHEL 7.2 and supports Node.js 4.4.4.

NodePlatform_AmazonLinux.zip

This custom platform is based on Amazon Linux 2016.09.1 and supports Node.js 4.4.4.

TomcatPlatform_Ubuntu.zip

This custom platform is based on Ubuntu 16.04 and supports Tomcat 7/Java 8.

CustomPlatform_NodeSampleApp.zip

A Node.js sample that uses express and ejs to display a static web page

CustomPlatform_TomcatSampleApp.zip

A Tomcat sample that displays a static web page when deployed

Download the sample platform definition archive: NodePlatform_Ubuntu.zip. This file contains a platform definition file, Packer template, scripts that Packer runs during image creation, and scripts and configuration files that Packer copies onto the builder instance during platform creation.

Example NodePlatform_Ubuntu.zip

|-- builder                 Contains files used by Packer to create the custom platform
|-- custom_platform.json    Packer template
|-- platform.yaml           Platform definition file
|-- ReadMe.txt              Briefly describes the sample

The platform definition file, platform.yaml, tells Elastic Beanstalk the name of the Packer template, custom_platform.json.

version: "1.0"

provisioner:
  type: packer
  template: custom_platform.json
  flavor: ubuntu1604

The Packer template tells Packer how to build the AMIs for the platform, using an Ubuntu AMI as a base for the platform image for HVM instance types. The provisioners section tells Packer to copy all files in the builder folder within the archive to the instance, and to run the builder.sh script on the instance. When the scripts complete, Packer creates an image from the modified instance.

Elastic Beanstalk creates three environment variables that can be used to tag AMIs in Packer:

AWS_EB_PLATFORM_ARN

The ARN of the custom platform.

AWS_EB_PLATFORM_NAME

The name of the custom platform.

AWS_EB_PLATFORM_VERSION

The version of the custom platform.

The sample custom_platform.json file uses these values to define the following values that it uses in the scripts:

  • platform_name, which is set by platform.yaml

  • platform_version, which is set by platform.yaml

  • platform_arn, which is set by the main build script, builder.sh, which is shown at the end of the sample custom_platform.json file.

Example custom_platform.json

{
  "variables": {
    "platform_name": "{{env `AWS_EB_PLATFORM_NAME`}}",
    "platform_version": "{{env `AWS_EB_PLATFORM_VERSION`}}",
    "platform_arn": "{{env `AWS_EB_PLATFORM_ARN`}}"
  },
  ...,
  "provisioners": [
    {...},
    {
      "type": "shell",
      "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo {{ .Path }}",
      "scripts": [
        "builder/builder.sh"
      ]
    }
  ]
}

The scripts and other files that you include in your platform definition archive will vary greatly depending on the modifications that you want to make to the instance. The sample platform includes the following scripts:

  • 00-sync-apt.sh – Commented out: apt -y update.

  • 01-install-nginx.sh – Installs nginx.

  • 02-setup-platform.sh – Installs wget, tree, and git. Copies hooks and logging configurations to the instance, and creates the following directories:

    • /etc/SampleNodePlatform – Where the container configuration file is uploaded during deployment.

    • /opt/elasticbeanstalk/deploy/appsource/ – Where the 00-unzip.sh script uploads application source code during deployment (see the Platform Scripts section for information about this script).

    • /var/app/staging/ – Where application source code is processed during deployment.

    • /var/app/current/ – Where application source code runs after processing.

    • /var/log/nginx/healthd/ – Where the enhanced health agent writes logs.

    • /var/nodejs – Where the Node.js files are uploaded during deployment.

Use the EB CLI to create your first custom platform with the sample platform definition archive.

To create a custom platform

  1. Install the EB CLI.

  2. Create a directory in which you will extract the sample custom platform.

    ~$ mkdir ~/custom-platform
  3. Extract NodePlatform_Ubuntu.zip to the directory.

    ~$ cd ~/custom-platform
    ~/custom-platform$ unzip ~/NodePlatform_Ubuntu.zip
  4. Run eb platform init and follow the prompts to initialize a platform repository.

    Note

    You can shorten eb platform to ebp.

    ~/custom-platform$ ebp init

    This command also creates the directory .elasticbeanstalk in the current directory and adds the configuration file config.yml to the directory. Don't change or delete this file, because Elastic Beanstalk relies on it when creating the custom platform.

    By default, ebp init uses the name of the current folder as the name of the custom platform, which would be custom-platform in this example.

  5. Run eb platform create to launch a Packer environment and get the ARN of the custom platform. You'll need this value later when you create an environment from the custom platform.

    ~/custom-platform$ ebp create
    ...

    By default, Elastic Beanstalk creates the instance profile aws-elasticbeanstalk-custom-platform-ec2-role for custom platforms. If, instead, you want to use an existing instance profile, add the option -ip INSTANCE_PROFILE to the eb platform create command.

    Note

    Packer will fail to create a custom platform if you use the Elastic Beanstalk default instance profile aws-elasticbeanstalk-ec2-role.

    The EB CLI shows event output of the Packer environment until the build is complete. You can exit the event view by pressing Ctrl-C.

  6. You can check the logs for errors using the eb platform logs command.

    ~/custom-platform$ ebp logs
    ...
  7. You can check on the process later with eb platform events.

    ~/custom-platform$ ebp events
    ...
  8. Check the status of your platform with eb platform status.

    ~/custom-platform$ ebp status
    ...

When the operation completes, you have a platform that you can use to launch an Elastic Beanstalk environment.

You can use the custom platform when creating a new environment from the console. Learn more at The Create New Environment Wizard.

To launch an environment on your custom platform

  1. Create a new directory for your application.

    ~$ mkdir custom-platform-app
    ~$ cd ~/custom-platform-app
  2. Initialize an application repository.

    ~/custom-platform-app$ eb init
    ...
  3. Download the sample application NodeSampleApp.zip.

  4. Unzip the sample application.

    ~/custom-platform-app$ unzip ~/NodeSampleApp.zip
  5. Run eb create -p CUSTOM-PLATFORM-ARN, where CUSTOM-PLATFORM-ARN is the ARN returned by an eb platform create command, to launch an environment running your custom platform.

    ~/custom-platform-app$ eb create -p CUSTOM-PLATFORM-ARN
    ...