Nathan Sowatskey
Nathan Sowatskey

Reputation: 169

Google Deployment Manager template using environment variables

I have a template called dev.yaml that looks like this:

imports:
- path: generate_config.py

resources:
- name: application_vm
  type: generate_config.py
  properties:
    zone: europe-west1-d
    project: cpb1234567

Note that I am hardcoding the zone and project. I want those to come from these environment variables.

CLOUDSDK_COMPUTE_ZONE=europe-west1-d
GCP_PROJ_ID=cpb1234567

The generate_config.py looks like this:

#!/usr/bin/env python

COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'

def generate_config(context):

  resources = [{
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 
                                  'projects/', 
                                  context.properties['project'],
                                  '/zones/',
                                  context.properties['zone'],
                                  '/machineTypes/n1-standard-1']),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 
                                          'projects/',
                                          context.properties['project'],
                                          '/global/images/jre-10gb-debian-jessie'])
              }
          }],
          'networkInterfaces': [{
              'network': ''.join([COMPUTE_URL_BASE, 
                                  'projects/',
                                  context.properties['project'],
                                  '/global/networks/default']),
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }]
      }
  }]
  return {'resources': resources}

I am getting the zone and project from the properties, so that works.

BUT, how do I get the zone and project from the environment variables?

I can't seem to do this in the Python template:

import os
...
os.environ['GCP_PROJ_ID']

As the import fails.

In the YAML, it is not clear what syntax would work.

Many thanks

Nathan

Upvotes: 2

Views: 956

Answers (1)

Nathan Sowatskey
Nathan Sowatskey

Reputation: 169

Whilst not an answer as such, more information is here:

https://groups.google.com/forum/#!topic/gce-discussion/Md7rCo1ZMJY

Upvotes: 2

Related Questions