Using deployment-specific environment variables

For each of your deployments, Deployment Manager creates pre-defined environment variables that contain information inferred from your deployment. Use these environment variables in your Python or Jinja2 templates to get information about your project or deployment.

Before you begin

Available environment variables

The following environment variables are automatically set by Deployment Manager. They are replaced everywhere you use them in your templates. For example, use the project_number variable to add the project number to the name of a service account.

Environment variable Value
deployment The name of the deployment.
name The name declared in the configuration that is using the template. This can be useful if you want the name you declare in the configuration to be the name of the resource in the underlying templates.
project The project ID for this deployment.
project_number The project number for this deployment.
current_time The UTC timestamp when expansion started for the deployment.
type The resource type declared in the top-level configuration.
username The current Deployment Manager user.

Using an environment variable

Use the following syntax to add an environment variable to your templates:

{{ env["deployment"] }} # Jinja

context.env["deployment"] # Python

In your template, use the variables as in these examples:

Jinja

- type: compute.v1.instance
  name: vm-{{ env["deployment"] }}
  properties:
    machineType: zones/us-central1-a/machineTypes/f1-micro
    serviceAccounts:
    - email: {{ env['project_number'] }}-compute@developer.gserviceaccount.com
      scopes:
      - ...

Python

def GenerateConfig(context):
  resources = []
  resources.append ({
    'name': 'vm-' + context.env["deployment"],
    'type': 'compute.v1.instance',
    'properties': {
       'serviceAccounts': [{
         'email': context.env['project_number'] + '-compute@developer.gserviceaccount.com',
         'scopes': [...]
       }]
    }
    ...}]
  return {'resources': resources}

What's next