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

- If you want to use the command-line examples in this guide, install the [\`gcloud\` command-line tool](https://docs.cloud.google.com/sdk).
- If you want to use the API examples in this guide, set up [API access](https://docs.cloud.google.com/deployment-manager/docs/reference/latest).
- Understand how to [create a basic template](https://docs.cloud.google.com/deployment-manager/docs/configuration/templates/create-basic-template).
- Understand how to [create a configuration](https://docs.cloud.google.com/deployment-manager/docs/configuration/create-basic-configuration)

## 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](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects) for this deployment. |
| `project_number` | The [project number](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects) 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

```django/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

```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

- Add a template permanently to your project as a [composite type](https://docs.cloud.google.com/deployment-manager/docs/configuration/templates/create-composite-types).
- [Host templates externally](https://docs.cloud.google.com/deployment-manager/docs/configuration/templates/hosting-templates-externally) to share with others.
- Add [schemas](https://docs.cloud.google.com/deployment-manager/docs/configuration/templates/using-schemas) to ensure users interact with your templates correctly.