Google Deployment Manager template using environment variables

794 views
Skip to first unread message

Nathan Sowatskey

unread,
May 18, 2017, 10:18:34 AM5/18/17
to gce-discussion
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
  
  

Faizan (Google Cloud Support)

unread,
May 18, 2017, 4:13:02 PM5/18/17
to gce-discussion
Hello Nathan,

I believe you can only use the predefined environment variables in your config file based on this article. As such, you can continue defining the project and zone information using template properties.

Faizan

Nathan Sowatskey

unread,
May 19, 2017, 12:57:53 AM5/19/17
to gce-discussion
Faizan

Thank you for that link. It does help what is meant by "Environment Variables" in the context of the limited mechanisms of the templates.

In general, the concept of "Environment Variables" is normally taken to mean variables that are defined in a user environment, for example those set via the .bashrc.

This use of the concept, to mean a few variables defined in the scope of the template system, is misleading, given the general expectation of what environment variables are.

Further, such a limited set of variables is of little or no practical use. For example, the zone is not included.

The use of properties, as shown, does work, but they seem to be of little practical use. The mechanism, as shown, seems to imply that the template needs to be edited each time the zone changes. That would mean that the same template could not, without editing, be used in different zones. That does, at least, provide a single file to edit, but still does not work in a typical CI/CD system where the context is defined by variables.

Basically, this is broken.

Regards

Nathan

Faizan (Google Cloud Support)

unread,
May 19, 2017, 4:14:50 PM5/19/17
to gce-discussion
Hello Nathan,

Can you report this through GCP issue tracker. One done, let me know I'll investigate this further with the backend team.

Faizan

Nathan Sowatskey

unread,
May 20, 2017, 1:34:50 AM5/20/17
to Faizan (Google Cloud Support), gce-discussion
Many thanks. See:

https://issuetracker.google.com/issues/38464927

Regards

Nathan
> --

Nathan Sowatskey

unread,
May 27, 2017, 4:32:48 AM5/27/17
to gce-discussion, fai...@google.com
Reply all
Reply to author
Forward
0 new messages