Using Environment Variables - AWS OpsWorks

Using Environment Variables


AWS OpsWorks Stacks is no longer accepting new customers. Existing customers will be able to use the OpsWorks console, API, CLI, and CloudFormation resources as normal until May 26, 2024, at which time they will be discontinued. To prepare for this transition, we recommend you transition your stacks to AWS Systems Manager as soon as possible. For more information, see AWS OpsWorks Stacks End of Life FAQs and Migrating your AWS OpsWorks Stacks applications to AWS Systems Manager Application Manager.


The recommendations in this topic apply to Chef 11.10 and earlier versions of Chef. To get environment variables in Chef 12 and newer releases, you must use the App Data Bag. For more information, see AWS OpsWorks Data Bag Reference and App Data Bag (aws_opsworks_app).

When you specify environment variables for an app, AWS OpsWorks Stacks adds the variable definitions to the app's deploy attributes.

Custom layers can use a recipe to retrieve a variable's value by using standard node syntax, and store it in a form that is accessible to the layer's apps.

You must implement a custom recipe that obtains the environment variable values from the instance's deploy attributes. The recipe can then store the data on the instance in a form that can be accessed by the application, such as a YAML file. An app's environment variable definitions are stored in the deploy attributes, in the app's environment_variables. The following example shows the location of these attributes for an app named simplephpapp, using JSON to represent the attribute structure.

{ ... "ssh_users": { }, "deploy": { "simplephpapp": { "application": "simplephpapp", "application_type": "php", "environment_variables": { "USER_ID": "168424", "USER_KEY": "somepassword" }, ... } }

A recipe can obtain variable values by using standard node syntax. The following example shows how to obtain the USER_ID value from the preceding JSON and place it in the Chef log."USER_ID: #{node[:deploy]['simplephpapp'][:environment_variables][:USER_ID]}")

For a more detailed description of how to retrieve information from the stack configuration and deployment JSON and store it on the instance, see Passing Data to Applications.