Course contentsShow
← Cloud and Infrastructure
Lesson 951 of 2,98918. Google Cloud Deployment ManagerFree lesson

Python Templates for Dynamic Logic

Create Python-based templates that generate configurations programmatically with logic.

Python Templates for Dynamic Logic

Important Notice: Google Cloud Deployment Manager reached end of support on March 31, 2026. Direct all new infrastructure provisioning to Google Cloud Infrastructure Manager or Terraform. The following content is provided strictly as a legacy reference for understanding and maintaining existing Deployment Manager environments.

What you'll learn: How Python-based Deployment Manager templates were used to generate configurations programmatically using logic, loops, and calculations instead of static YAML.


Why Python Templates Were Used

In legacy Deployment Manager setups, YAML configurations declared resources in a straightforward way. However, when developers needed to create identical infrastructure at scale (e.g., 10 identical VMs), conditionally add resources based on environment type, or calculate subnet CIDR ranges, static YAML became repetitive and error-prone.

Python templates solved this by allowing administrators to write code that generated YAML. Instead of hand-crafting each resource declaration, a Python script acted as an automated generator that produced configurations with specific logic and programmatic variations.


How Legacy Python Templates Work

A Python template is a .py file executed by the Deployment Manager engine during the evaluation phase. If you are reviewing an older repository, you will notice these scripts must follow specific structural rules:

  1. Define a GenerateConfig(context) function as the main entry point.
  2. Return a dictionary containing a resources key, which maps to a list of generated resource definitions.
  3. Use the context.properties object to read parameters passed from the main YAML configuration.

Legacy Reference Example:

def GenerateConfig(context):
    resources = []
    
    # Use logic to build resource definitions dynamically
    for i in range(context.properties['vmCount']):
        resources.append({
            'name': 'vm-' + str(i),
            'type': 'compute.v1.instance',
            'properties': {
                'zone': context.properties['zone'],
                'machineType': 'zones/{}/machineTypes/n1-standard-1'.format(context.properties['zone'])
            }
        })
    
    return {'resources': resources}

A main configuration YAML would import this template and pass properties into it, enabling dynamic generation. When maintaining these legacy scripts, remember that the template itself never provisions resources directly; it solely outputs a data structure that the Deployment Manager engine then interprets to deploy the infrastructure.


Key Takeaway:
Python templates transformed legacy Deployment Manager setups from static YAML files into programmatic infrastructure generators. They relied on a required GenerateConfig() function to dynamically output resource configurations using standard code logic. For modern implementations, translate these dynamic patterns into Terraform or Google Cloud Infrastructure Manager architectures.