Using Environment Variables

Environment variables are predefined variables that populate certain pieces of information inferred from your deployment. Use environment variables in your Python or Jinja2 templates to get unique information about your project or deployment.

Before you begin

Valid 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

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Deployment Manager Documentation