AWS DevOps & Developer Productivity Blog
AWS OpsWorks supports application environment variables
AWS OpsWorks now allows users to define environment variables per application. Instead of creating a custom recipe and managing environment variables as Chef attributes, you define the environment variables on each app and OpsWorks takes care of securely storing and sending the environment variables from your OpsWorks app definition to your instances and adding them to the application server context. All you have to do is reference the environment variables in your application’s code using the methods defined in the Java, Ruby, PHP, and Node.js application servers. The environment variables are passed to the application server during instance setup and can be updated on each application deployment. Environment variables can also be defined as protected values, so that they cannot be read by OpsWorks users. For example, a user can set separate environment variables for database endpoint, username, and password. The password environment variable can be defined as a protected value, so it cannot be viewed in the console, SDK or CLI and is only made available to the defined application.
1. To get started, create a stack with an application server layer as described in steps 1-3 of the
OpsWorks walk-through.
2. Create the following sample PHP application as index.php in a source repository to display the environment variable
database_endpoint that is set in step 3.
<?php
$d = array(‘environment’ => getenv(“database_endpoint”));
echo $d
?>
|
3. Once you have a stack, PHP layer and instance created, start the instance and navigate to Apps to add an app. Point to the repository where your app’s code is stored. Scroll down to the app’s Environment Variables section, then enter the environment variables as keys and values making sure to include an environment variable database_endpoint with a value of your choosing.
Check the “protected value” box next to values such as passwords that you do not want to be displayed. Once you have finished entering the environment variables, click
Add App.
4. When you edit an app you can add, update, and delete environment variables that are set in OpsWorks. Notice that protected values are not displayed in the console, CLI or SDK. You can update the value by selecting the Update value link. Use
OpsWorks permissions to choose which users can edit these values.
5. Finally, deploy your app. When the app is deployed, select the instance’s IP address to view the values that were passed to the app. You will see:
{“environment”:”mydbinstance.us-east-1.rds.amazonaws.com”}
|
The environment variables were sent to your instance as attributes that you can also use in custom recipes:
“deploy”: {
“myapp”: {
“application”: “myapp”,
“application_type”: “php”,
“environment”: {
“database_endpoint”: “mydbinstance.us-east-1.rds.amazonaws.com”,
…
|
For more information including examples for other application servers, see the documentation.