Using the Elastic Beanstalk (EB) CLI to create, manage, and share environment configuration
My colleague Nick Humrich wrote up the guest post below to share a powerful way to use the EB CLI to manage environment configurations — Abhishek
The AWS Elastic Beanstalk command line interface (EB CLI) makes it easier for developers to get started with Elastic Beanstalk by using command line tools. Last November, we released a revamped version of the EB CLI that added a number of new commands and made it even simpler to get started. As part of a recent update (2/17), we’ve added new commands to create, manage, and share environment configurations.
In this post, we will discuss how to create configurations, save them in templates, make a template the default for future deployments, and share templates by checking them in to version control. The remainder of this post will assume that you have EB CLI 3.x installed and have an application that you will be deploying to Elastic Beanstalk. If you haven’t installed the EB CLI, see Install the EB CLI using pip (Windows, Linux, OS X or Unix).
To begin, we will create an Elastic Beanstalk environment and deploy our application to it:
$ git clone https://github.com/awslabs/eb-python-flask.git $ cd eb-python-flask $ eb init -p python2.7 # Configures the current folder for EB $ eb create dev-env # Creates the EB environment and pushes # the contents of the app folder to the # newly created environment $ eb open # Opens the default browser to the # current application’s URL
Creating and applying an environment configuration
Before we start editing our environment configuration, let’s take a snapshot of the current settings. We can then roll back to them should any problems arise.
You can save a configuration of a running environment by using the following command:
$ eb config save dev-env --cfg initial-configuration
The command saves a configuration locally and in Elastic Beanstalk. The output from EB CLI tells you the location of the local copy, the .elasticbeanstalk/saved_configs/ folder.
Now that we have a snapshot, let’s make some changes to our environment so that we can see how to apply a configuration to an environment. Type the following command to set the environment variable “ENABLE_COOL_NEW_FEATURE” to “true”.
$ eb setenv ENABLE_COOL_NEW_FEATURE=true
If you want to revert to your previous configuration, you can do so by applying a saved configuration during an environment update.
$ eb config --cfg initial-configuration
This will work even if you don’t have a local copy because the saved configuration is also stored in Elastic Beanstalk.
Creating an environment template
Now that our cool new feature is working, we are ready to create a production environment. Let’s begin by saving a configuration with the following:
$ eb config save dev-env --cfg prod
Now, open this file in a text editor to modify/remove sections as necessary for your production environment. For this example, we just need the environment variables section. We will also add the line “FLASK_DEBUG: false” to turn debugging text off.
Note: AWSConfigurationTemplateVersion is a required field. Do not remove it from the configuration file.
The following is an example of what the file should now look like:
EnvironmentConfigurationMetadata: Description: Configuration created from the EB CLI using "eb config save". DateModified: '1427752586000' DateCreated: '1427752586000' AWSConfigurationTemplateVersion: 18.104.22.168 OptionSettings: aws:elasticbeanstalk:application:environment: ENABLE_COOL_NEW_FEATURE: true FLASK_DEBUG: false
When you are done revising the configuration, you can upload it to Elastic Beanstalk by running the following command:
$ eb config put prod
This command also validates the saved configuration to make sure it doesn’t contain any errors.
Using an environment template to create an environment
Now that we have an environment template, we can create our new prod environment from the template.
$ eb create prod-env --cfg prod
Updating an existing environment using an environment template
You can also update the currently running environment to use the saved configuration by running the following command:
$ eb config dev-env --cfg prod
Alternatively, you can pipe the configuration into the config command:
$ cat prod.cfg.yml | eb config dev-env
Using default option settings with EB CLI
Specifying a template every time you create an environment can be annoying. When you save a template with the name “default,” the CLI will use it automatically for all new environments.
$ eb config save --cfg default
This will save a configuration called default locally and in Elastic Beanstalk. Open the saved configuration with a text editor and remove all sections that you do not want included as default settings for environments that apply the saved configuration. EB CLI will now use these settings automatically every time you run the eb create command.
Checking configurations into version control
If you want to check in your saved configurations so that anyone with access to your code can use the same settings in their own environments or if you want to track different versions of the saved configurations, move the file to the .elasticbeanstalk/folder directory. Saved configurations are located in the .elasticbeanstalk/saved_configs/ folder. By moving the configuration file up one level into the .elasticbeanstalk/ folder, the file can be checked in and will still work with the EB CLI. After you move the file, you must add and commit it.