Exploring helper scripts

Helper scripts, or template modules, are helper files that can make your templates more efficient by performing specific functions. For example, you can use helper scripts to interpret resource metadata, create files, and launch services.

You will now explore a Python helper script that names a virtual machine, given a prefix and a suffix.

Before you begin

Opening the helper script

The basic helper script in this example generates the name for a virtual machine (VM). To view the script, run these commands:

cd deploymentmanager-samples/examples/v2/step_by_step_guide/create_a_helper_script

nano common.py  # use your preferred text editor

The GenerateMachineName() function takes a prefix and suffix, and generates a name in the format prefix-suffix:

"""Generates name of a VM."""


def GenerateMachineName(prefix, suffix):
  return prefix + "-" + suffix

Using the helper script in the template

To use common.py in vm-template.py, several changes must be made to the template.

To view the changes, open vm-template.py:

nano vm-template.py

The template contains code comments that highlight the changes.

Note that the template imports common.py at the top of the file. In the resources section, the name fields for the VMs now call GenerateMachineName().

"""Creates the virtual machine."""

# `common.py` is imported below.
import common

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


def GenerateConfig(context):
  """Generates configuration of a VM."""
  resources = [{
      'name': common.GenerateMachineName('myfrontend', 'prod'),
      'type': 'compute.v1.instance',
      'properties': {
          'zone': 'us-central1-f',
          'machineType': COMPUTE_URL_BASE + 'projects/' + context.env['project']
                         + '/zones/us-central1-f/machineTypes/f1-micro',
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': COMPUTE_URL_BASE + 'projects/'
                                 'debian-cloud/global/images/family/debian-11'}
          }],
          'networkInterfaces': [{
              'network': COMPUTE_URL_BASE + 'projects/' + context.env['project']
                         + '/global/networks/default',
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }]
      }
  }]
  return {'resources': resources}

Viewing the changes to the configuration

To view the updated configuration, open two-vms.yaml:

nano two-vms.yaml

Note that the helper script common.py must be imported in the configuration as well.

Deploy your configuration:

gcloud deployment-manager deployments create deployment-with-helper-script --config two-vms.yaml

To view the deployment, including the resources with the generated names, run:

 gcloud deployment-manager deployments describe deployment-with-helper-script

Deleting your deployment

We recommend that you delete the deployment to avoid charges. You don't need this deployment for the next step. Run the following command to delete the deployment:

gcloud deployment-manager deployments delete deployment-with-helper-script

Looking ahead: updating deployments

Next, learn to add, delete, and change the properties of resources in a deployment as your app evolves.