From 048cc65197c96baf831aecb930ab228b1f4f0ddd Mon Sep 17 00:00:00 2001 From: Anita Garg Date: Thu, 12 Sep 2019 17:10:28 -0700 Subject: [PATCH] Add DM Walkthrough tutorial --- README.md | 4 + examples/v2/common/container_manifest.yaml | 3 +- .../create_a_helper_script/README.md | 1 + .../create_a_helper_script/common.py | 6 + .../create_a_helper_script/two-vms.yaml | 9 + .../create_a_helper_script/vm-template.py | 38 ++ .../step2_create_a_configuration/two-vms.yaml | 8 +- .../step4_use_references/two-vms.yaml | 10 +- .../python/two-vms.yaml | 2 + .../python/vm-template-2.py | 2 +- .../python/vm-template.py | 2 +- .../python/compute-engine-template.py | 2 +- .../python/firewall-template.py | 2 +- .../python/network-template.py | 2 +- .../python/vm-template-2.py | 4 +- .../python/vm-template.py | 4 +- .../python/vm-template.py | 8 + .../python/vm-template.py | 1 + walkthroughtutorial.md | 624 ++++++++++++++++++ 19 files changed, 715 insertions(+), 17 deletions(-) create mode 100644 examples/v2/step_by_step_guide/create_a_helper_script/README.md create mode 100644 examples/v2/step_by_step_guide/create_a_helper_script/common.py create mode 100644 examples/v2/step_by_step_guide/create_a_helper_script/two-vms.yaml create mode 100644 examples/v2/step_by_step_guide/create_a_helper_script/vm-template.py create mode 100644 walkthroughtutorial.md diff --git a/README.md b/README.md index d2fd5cfe2..dec248f15 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,10 @@ This repository contains example templates for use with Deployment Manager. For an overview of Deployment Manager, see https://cloud.google.com/deployment-manager/docs. +## Run the walkthrough tutorial + +[![Open in Cloud Shell](http://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fdeploymentmanager-samples&cloudshell_tutorial=walkthroughtutorial.md) + ## Try the samples Clone this repostory into [Cloud Shell](https://cloud.google.com/shell/), edit the samples, and deploy them from within Cloud Shell, without installing anything. diff --git a/examples/v2/common/container_manifest.yaml b/examples/v2/common/container_manifest.yaml index 62a72ec47..864f4e674 100644 --- a/examples/v2/common/container_manifest.yaml +++ b/examples/v2/common/container_manifest.yaml @@ -21,8 +21,7 @@ metadata: spec: containers: - name: simple-echo - image: gcr.io/google-containers/busybox - command: ['nc', '-p', '8080', '-l', '-l', '-e', 'echo', 'hello world!'] + image: gcr.io/google-samples/hello-app:2.0 imagePullPolicy: Always ports: - containerPort: 8080 diff --git a/examples/v2/step_by_step_guide/create_a_helper_script/README.md b/examples/v2/step_by_step_guide/create_a_helper_script/README.md new file mode 100644 index 000000000..cccfb17db --- /dev/null +++ b/examples/v2/step_by_step_guide/create_a_helper_script/README.md @@ -0,0 +1 @@ +This directory contains files that allow users to create helper scripts in the interactive tutorial. diff --git a/examples/v2/step_by_step_guide/create_a_helper_script/common.py b/examples/v2/step_by_step_guide/create_a_helper_script/common.py new file mode 100644 index 000000000..20b632fbd --- /dev/null +++ b/examples/v2/step_by_step_guide/create_a_helper_script/common.py @@ -0,0 +1,6 @@ +"""Generates name of a VM.""" + + +def GenerateMachineName(prefix, suffix): + return prefix + "-" + suffix + diff --git a/examples/v2/step_by_step_guide/create_a_helper_script/two-vms.yaml b/examples/v2/step_by_step_guide/create_a_helper_script/two-vms.yaml new file mode 100644 index 000000000..482792f0c --- /dev/null +++ b/examples/v2/step_by_step_guide/create_a_helper_script/two-vms.yaml @@ -0,0 +1,9 @@ +imports: +# common.py is imported below. +- path: common.py +- path: vm-template.py + +resources: +- name: vm-1 + type: vm-template.py + diff --git a/examples/v2/step_by_step_guide/create_a_helper_script/vm-template.py b/examples/v2/step_by_step_guide/create_a_helper_script/vm-template.py new file mode 100644 index 000000000..4c03273ee --- /dev/null +++ b/examples/v2/step_by_step_guide/create_a_helper_script/vm-template.py @@ -0,0 +1,38 @@ +"""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-9'} + }], + 'networkInterfaces': [{ + 'network': COMPUTE_URL_BASE + 'projects/' + context.env['project'] + + '/global/networks/default', + 'accessConfigs': [{ + 'name': 'External NAT', + 'type': 'ONE_TO_ONE_NAT' + }] + }] + } + }] + return {'resources': resources} + diff --git a/examples/v2/step_by_step_guide/step2_create_a_configuration/two-vms.yaml b/examples/v2/step_by_step_guide/step2_create_a_configuration/two-vms.yaml index 27919f965..28a7cecd7 100644 --- a/examples/v2/step_by_step_guide/step2_create_a_configuration/two-vms.yaml +++ b/examples/v2/step_by_step_guide/step2_create_a_configuration/two-vms.yaml @@ -17,7 +17,7 @@ resources: type: compute.v1.instance properties: zone: us-central1-f - machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro + machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/f1-micro disks: - deviceName: boot type: PERSISTENT @@ -26,7 +26,7 @@ resources: initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/global/networks/default + - network: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/global/networks/default accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT @@ -34,7 +34,7 @@ resources: type: compute.v1.instance properties: zone: us-central1-f - machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/g1-small + machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/g1-small disks: - deviceName: boot type: PERSISTENT @@ -43,7 +43,7 @@ resources: initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: - - network: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/global/networks/default + - network: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/global/networks/default accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT diff --git a/examples/v2/step_by_step_guide/step4_use_references/two-vms.yaml b/examples/v2/step_by_step_guide/step4_use_references/two-vms.yaml index 64c05ffc4..6baa91094 100644 --- a/examples/v2/step_by_step_guide/step4_use_references/two-vms.yaml +++ b/examples/v2/step_by_step_guide/step4_use_references/two-vms.yaml @@ -17,7 +17,7 @@ resources: type: compute.v1.instance properties: zone: us-central1-f - machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/f1-micro + machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/f1-micro disks: - deviceName: boot type: PERSISTENT @@ -26,6 +26,9 @@ resources: initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: + # The resource's "network value" has been replaced with a + # reference to the new network's "selfLink" property. The network + # resource has been added to the end of this file. - network: $(ref.a-new-network.selfLink) accessConfigs: - name: External NAT @@ -34,7 +37,7 @@ resources: type: compute.v1.instance properties: zone: us-central1-f - machineType: https://www.googleapis.com/compute/v1/projects/[MY_PROJECT]/zones/us-central1-f/machineTypes/g1-small + machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/g1-small disks: - deviceName: boot type: PERSISTENT @@ -43,10 +46,13 @@ resources: initializeParams: sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9 networkInterfaces: + # As in the resource above, the "network" value has been replaced with + # the new network's "selfLink" property. - network: $(ref.a-new-network.selfLink) accessConfigs: - name: External NAT type: ONE_TO_ONE_NAT +# The following network is a new resource added to the "two-vms.yaml" file. - name: a-new-network type: compute.v1.network properties: diff --git a/examples/v2/step_by_step_guide/step5_create_a_template/python/two-vms.yaml b/examples/v2/step_by_step_guide/step5_create_a_template/python/two-vms.yaml index 35f6663e4..550811d94 100644 --- a/examples/v2/step_by_step_guide/step5_create_a_template/python/two-vms.yaml +++ b/examples/v2/step_by_step_guide/step5_create_a_template/python/two-vms.yaml @@ -16,6 +16,8 @@ imports: - path: vm-template.py - path: vm-template-2.py +# In the resources section below, the properties of the resources are replaced +# with the names of the templates. resources: - name: vm-1 type: vm-template.py diff --git a/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template-2.py b/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template-2.py index 370917ea8..f938a1a21 100644 --- a/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template-2.py +++ b/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template-2.py @@ -25,7 +25,7 @@ def GenerateConfig(unused_context): 'type': 'compute.v1.instance', 'properties': { 'zone': 'us-central1-f', - 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/[MY_PROJECT]', + 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/MY_PROJECT', '/zones/us-central1-f/', 'machineTypes/g1-small']), 'disks': [{ diff --git a/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template.py b/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template.py index da4bfc6d7..33a324fe6 100644 --- a/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template.py +++ b/examples/v2/step_by_step_guide/step5_create_a_template/python/vm-template.py @@ -25,7 +25,7 @@ def GenerateConfig(unused_context): 'type': 'compute.v1.instance', 'properties': { 'zone': 'us-central1-f', - 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/[MY_PROJECT]', + 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/MY_PROJECT', '/zones/us-central1-f/', 'machineTypes/f1-micro']), 'disks': [{ diff --git a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/compute-engine-template.py b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/compute-engine-template.py index 66ed5e1bb..ebd9cd99f 100644 --- a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/compute-engine-template.py +++ b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/compute-engine-template.py @@ -15,7 +15,7 @@ """Creates the Compute Engine.""" -def GenerateConfig(unused_context): +def GenerateConfig(context): """Creates the Compute Engine with network and firewall.""" resources = [{ diff --git a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/firewall-template.py b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/firewall-template.py index a2595c246..47c03785e 100644 --- a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/firewall-template.py +++ b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/firewall-template.py @@ -15,7 +15,7 @@ """Creates the firewall.""" -def GenerateConfig(unused_context): +def GenerateConfig(context): """Creates the firewall.""" resources = [{ diff --git a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/network-template.py b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/network-template.py index 6415eb2b3..a4eb34835 100644 --- a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/network-template.py +++ b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/network-template.py @@ -15,7 +15,7 @@ """Creates the network.""" -def GenerateConfig(unused_context): +def GenerateConfig(context): """Creates the network.""" resources = [{ diff --git a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template-2.py b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template-2.py index 370917ea8..5e7cc22bb 100644 --- a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template-2.py +++ b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template-2.py @@ -17,7 +17,7 @@ COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' -def GenerateConfig(unused_context): +def GenerateConfig(context): """Creates the second virtual machine.""" resources = [{ @@ -25,7 +25,7 @@ def GenerateConfig(unused_context): 'type': 'compute.v1.instance', 'properties': { 'zone': 'us-central1-f', - 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/[MY_PROJECT]', + 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',context.env['project'], '/zones/us-central1-f/', 'machineTypes/g1-small']), 'disks': [{ diff --git a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template.py b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template.py index da4bfc6d7..fd7c5a8c7 100644 --- a/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template.py +++ b/examples/v2/step_by_step_guide/step6_use_multiple_templates/python/vm-template.py @@ -17,7 +17,7 @@ COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' -def GenerateConfig(unused_context): +def GenerateConfig(context): """Creates the first virtual machine.""" resources = [{ @@ -25,7 +25,7 @@ def GenerateConfig(unused_context): 'type': 'compute.v1.instance', 'properties': { 'zone': 'us-central1-f', - 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/[MY_PROJECT]', + 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',context.env['project'], '/zones/us-central1-f/', 'machineTypes/f1-micro']), 'disks': [{ diff --git a/examples/v2/step_by_step_guide/step7_use_environment_variables/python/vm-template.py b/examples/v2/step_by_step_guide/step7_use_environment_variables/python/vm-template.py index 4f433cd53..6f4436194 100644 --- a/examples/v2/step_by_step_guide/step7_use_environment_variables/python/vm-template.py +++ b/examples/v2/step_by_step_guide/step7_use_environment_variables/python/vm-template.py @@ -21,9 +21,15 @@ def GenerateConfig(context): """Creates the virtual machine with environment variables.""" resources = [{ + # `the-first-vm` is replaced with `context.env[`name`]` 'name': context.env['name'], 'type': 'compute.v1.instance', 'properties': { + # All occurrences of `us-central1-f` are replaced with + # `context.properties[`zone`]. + # All occurrences of the project ID are replaced with + # `context.env[`project`]`. + # `f1-micro` is replaced with `context.properties["machineType"]. 'zone': context.properties['zone'], 'machineType': ''.join([COMPUTE_URL_BASE, 'projects/', context.env['project'], '/zones/', @@ -40,6 +46,8 @@ def GenerateConfig(context): 'images/family/debian-9']) } }], + # `$(ref.a-new-network.selfLink)` is replaced with + # `$(ref.` + context.properties[`network`] + `selfLink)`. 'networkInterfaces': [{ 'network': '$(ref.' + context.properties['network'] + '.selfLink)', diff --git a/examples/v2/step_by_step_guide/step9_update_a_deployment/python/vm-template.py b/examples/v2/step_by_step_guide/step9_update_a_deployment/python/vm-template.py index 74df79f40..1878889e8 100644 --- a/examples/v2/step_by_step_guide/step9_update_a_deployment/python/vm-template.py +++ b/examples/v2/step_by_step_guide/step9_update_a_deployment/python/vm-template.py @@ -14,6 +14,7 @@ """Creates the virtual machine with environment variables and startup script.""" + COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' diff --git a/walkthroughtutorial.md b/walkthroughtutorial.md new file mode 100644 index 000000000..720c7b3d0 --- /dev/null +++ b/walkthroughtutorial.md @@ -0,0 +1,624 @@ +# Explore Deployment Manager + +## Overview + +This tutorial is a walkthrough of Deployment Manager best practices. As you complete this guide, you will learn techniques to build complex deployments: + ++ Creating a deployment configuration ++ Deploying resources ++ Using templates ++ Using helper scripts + +After completing this tutorial, you can apply these techniques to carry out tasks such as: + ++ Creating and managing Google Cloud Platform (GCP) resources using templates ++ Harnessing templates to reuse deployment paradigms ++ Deploying multiple resources at once ++ Configuring GCP resources such as Cloud Storage, Compute Engine, and Cloud SQL to work together + +This tutorial assumes that you are familiar with YAML syntax and are comfortable running commands in a Linux terminal. + +### Select a project + +Select a GCP Console project to use for this tutorial. + + + +## Setup + +Every command requires a project ID. Set a default project ID so you do not need to provide it every time. + +```sh +gcloud config set project {{project-id}} +``` + +Enable the Compute Engine and Deployment Manager APIs, which you will need for this tutorial. + +```sh +gcloud services enable compute.googleapis.com deploymentmanager.googleapis.com +``` + +## Understanding configurations + +A **configuration** defines the structure of a deployment. You must specify a configuration to create a deployment. + +In this step, you will access a configuration that creates a deployment with two instances. An **instance** is one of several kinds of resources you can deploy with Deployment Manager. + +## Access a configuration + +To access the configuration, first use the following command: + +```sh +cd examples/v2/step_by_step_guide/step2_create_a_configuration +``` + +Next, open the file `two-vms.yaml`: + +```sh +cloudshell edit two-vms.yaml +``` + +You will now be able to view your configuration file, which has two resources: `the-first-vm` and `the-second-vm`. Note that each resource has a `name`, `type`, and `properties` field. + +### Update your project ID + +Replace all instances of "MY_PROJECT" in the file with your project ID: + +```sh +sed -i -e 's/MY_PROJECT/{{project-id}}/g' two-vms.yaml +``` + +### Looking ahead: deploying resources + +You can use this configuration file to create a deployment. To learn how to deploy the resources in your configuration, continue to the next step. + +## Deploying your resources + +A **deployment** creates a set of resources defined in a configuration. Since a deployment contains the resources defined in the configuration, your deployment in this tutorial will have two virtual machine instances. + +## Deploy the configuration + +Run this command to deploy your configuration: + +```sh +gcloud deployment-manager deployments create deployment-with-2-vms --config two-vms.yaml +``` + +Wait for the indication that you successfully created the deployment (note that your actual operation IDs will differ): + +``` +Waiting for create operation-1432319707382-516afeb5d00f1-b864f0e7-b7103978...done. +Create operation operation-1432319707382-516afeb5d00f1-b864f0e7-b7103978 completed successfully. +NAME TYPE STATE ERRORS INTENT +the-first-vm compute.v1.instance COMPLETED [] +the-second-vm compute.v1.instance COMPLETED [] +``` + +## View the deployment + +Run the following command to view the deployment. You will see a list of each resource in the deployment, along with the resource type, unique resource ID, resource name, and resource creation status: + +```sh +gcloud deployment-manager deployments describe deployment-with-2-vms +``` + +### View deployment resources + +You can view a list of resources to quickly see which resource might be causing the issue. + +To get a list of deployment resources, run: + +```sh +gcloud deployment-manager resources list --deployment deployment-with-2-vms +``` + +## Delete your deployment + +You won't use this deployment for the remainder of the tutorial. Since Compute Engine resources incur charges, you should delete this deployment. Deleting a deployment also deletes all the resources in a deployment. + +**If you don't delete the deployment, you will run into conflicts with future examples.** + +To delete this deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-2-vms +``` + +### Looking ahead: references + +Next, use references to improve troubleshooting and to access properties of other resources in your deployment. + +## Understanding the benefits of references + +You can use **references** to define the properties of your configuration or templates instead of directly providing values. + +With references, you can access properties that are not defined until the resource is created. For example, when you define a virtual machine in your configuration, you do not know its IP address. However, you can create a reference to the IP address. + +Next, you will examine an updated `two-vms.yaml` that contains a network, as well as virtual machine instances that reference the network. + +## Exploring the new `two-vms.yaml` + +First, run the following command to open your home directory: + +```sh +cd +``` + +To open the new `two-vms.yaml` file with the network and references, first use the following command: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step4_use_references +``` + +Then, open the `two-vms.yaml` file: + +```sh +cloudshell edit two-vms.yaml +``` + +### Viewing the reference to a network + +In the properties section of both of your virtual machine instances, the value of `network` is replaced with a reference to the new network's `selfLink` property. + +## Deploy your new configuration + +### Update your project ID + +Replace all instances of "MY_PROJECT" in `two-vms.yaml` with your project ID: + +```sh +sed -i -e 's/MY_PROJECT/{{project-id}}/g' two-vms.yaml +``` + +### Deploy your configuration + +Deploy your configuration with the following command: + +```sh +gcloud deployment-manager deployments create deployment-with-references --config two-vms.yaml +``` + +To view your deployment, use the following command: + +```sh +gcloud deployment-manager deployments describe deployment-with-references +``` + +## Delete Your Deployment + +Once again, you will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-references +``` + +### Looking ahead + +To maximize efficiency while building large configurations, follow best practices such as using variables and templates. + +In the next step, you will learn about templates and how they enable flexible, dynamic configurations. + +## Exploring the power of templates + +While developing an application, you will most likely require complex architectures. Therefore, we recommend that you break your configuration into templates. + +A **template** is a separate file that defines a set of resources. You can reuse templates across different deployments, which creates consistency across complex deployments. + +You can use Python or Jinja2 to create templates for Deployment Manager. We recommend that you use Python templates, because Python allows for greater flexibility and more features as you scale your application. + +Your next task is to create a Python template using the contents of the configuration you created earlier in this tutorial. + +## Examining a template + +First, run the following command to open your home directory: + +```sh +cd +``` + +Next, change directories with the following command: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step5_create_a_template/python +``` + +Then, open the `vm-template.py` file: + +```sh +cloudshell edit vm-template.py +``` + +Replace all instances of "MY_PROJECT" in the file with your project ID: + +```sh +sed -i -e 's/MY_PROJECT/{{project-id}}/g' vm-template.py +``` + +## Editing a second template + +There is a second template in this directory called `vm-template-2.py`. Like `vm-template.py`, `vm-template-2.py` creates a virtual machine. + +To view `vm-template-2.py`, run the following command: + +```sh +cloudshell edit vm-template-2.py +``` + +Replace all instances of "MY_PROJECT" in the file with your project ID: + +```sh +sed -i -e 's/MY_PROJECT/{{project-id}}/g' vm-template-2.py +``` + +## Importing templates + +Open the `two-vms.yaml` file in this directory with the following command: + +```sh +cloudshell edit two-vms.yaml +``` + +In this updated file, the templates are imported at the top of the file. The properties of the resources are replaced with the names of the templates. + +## Naming resources + +When you use a template, your resource names are defined using the `name` field provided in the template, not the name in the configuration file. + +For example, in this case, the virtual machine instances are created using the names in the templates you created, "the-first-vm" and "the-second-vm." The values "vm-1" and "vm-2," defined in the configuration, are used to name an instantiation of the template, but are not resource names, unless you decide to use environment variables to set both names to be the same. + +## View the deployment + +Save your configuration and deploy it: + +```sh + gcloud deployment-manager deployments create deployment-with-templates --config two-vms.yaml +``` + +To view your deployment, run the following command: + +```sh + gcloud deployment-manager deployments describe deployment-with-templates +``` + +## Delete the deployment + +Once again, you will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-templates +``` + +### Looking ahead: using multiple templates + +Next, combine templates so that your configuration only calls one template to deploy all your resources. + +## Using multiple templates + +Next, you will explore a template that imports other templates. + +After incorporating these templates, your configuration only needs to call a single template to create a deployment with all of these resources. + +## Exploring a template that uses multiple templates + +The template in this example creates a Compute Engine with a network and a firewall. + +### Access the template + +First, use the following command to open your home directory: + +```sh +cd +``` + +Next, use the following command: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step6_use_multiple_templates/python +``` + +To view this template, run the following command: + +```sh +cloudshell edit compute-engine-template.py +``` + +### Viewing the template's resources + +The template's resources include `vm-template.py`, `vm-template-2.py`, `network-template.py`, and `firewall-template.py`. You can view these templates using the `cloudshell edit {file-name}` command. + +## Importing many templates into a configuration + +Now, you will explore a configuration that uses the template you previously viewed. + +To see this file, run the following command: + + ```sh +cloudshell edit config-with-many-templates.yaml +``` + +Notice that the configuration did not directly call the other templates. However, the other templates are imported because `compute-engine-template.py` depends on the other templates to be valid. + +## Deploy your configuration + +Save your configuration and deploy it: + +```sh +gcloud deployment-manager deployments create deployment-with-many-templates --config config-with-many-templates.yaml +``` + +To view your deployment, run the following command: + +```sh +gcloud deployment-manager deployments describe deployment-with-many-templates +``` + +## Delete your deployment + +Once again, you will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-many-templates +``` + +### Looking ahead: exploring template potential + +Next, you will learn how to maximize template features such as custom properties to benefit your application development. + +## Learning about template properties + +An advantage of using templates is the ability to create and define custom properties, which enable the reuse of templates across zones, regions, and projects. + +Template properties are arbitrary variables. Any configuration file or template file can provide a value for a template property without modifying the template. Therefore, you can change a property's value for various configurations without changing the template itself. + +To reference an arbitrary value, use this syntax in a template: + +```sh + context.properties["property-name"] +``` + +## Learning about environment variables + +Environment variables are predefined variables that populate particular pieces of information from your deployment. Use environment variables in templates to get unique information about your deployment. + +Reference an environment variable using this syntax: + +```sh + context.env['variable-name'] +``` + +## Using template properties and environment variables in a template + +In this step, `vm-template.py` shows the benefits of template properties and environment variables. + +To view the new `vm-template.py`, first use the following command to open your home directory: + +```sh +cd +``` + +Next, run the following command: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step7_use_environment_variables/python +``` + +Then, open the `vm-template.py` file: + +```sh +cloudshell edit vm-template.py +``` + +### Exploring the changes + +Various parts of the file have been replaced with template properties and environment variables. For example, the project ID is replaced with `context.env[`project`]`. Read the file comments to learn about other changes in the file. + +## Deploy your configuration + +To view the configuration file for this deployment, run the following command: + +```sh +cloudshell edit config-with-many-templates.yaml +``` + +Save your changes and redeploy your configuration to confirm the variables work. + +```sh + gcloud deployment-manager deployments create deployment-with-template-properties --config config-with-many-templates.yaml +``` + +## Delete the deployment + +Once again, you will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-template-properties +``` + +### Looking ahead: helper scripts + +Next, you will learn how to use helper scripts to efficiently perform repeated tasks. + +## Exploring helper scripts + +**Helper scripts** are helper files that make your templates more efficient by performing specific functions. Helper scripts can be used 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. + +## Viewing the helper script + +The helper script in this example generates the name for a virtual machine. To view the helper script, first run the following command to open your home directory: + +```sh +cd +``` + +Next, change to this directory: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/create_a_helper_script +``` + +Then, open `common.py`: + +```sh +cloudshell edit common.py +``` + +## Using helper scripts in templates + +To use `common.py` in `vm-template.py`, several changes must be made to the template. + +To view the changes, open the `vm-template.py` file: + +```sh +cloudshell edit vm-template.py +``` + +The template contains code comments highlighting the changes made. + +### Exploring the changes + +Notice that `common.py` is imported at the top of the file. Also, the `name` listing in the `resources` section is changed to use the script. + +## Changing your configuration + +The configuration must also be changed to import the helper script. To view this change, open `two-vms.yaml`: + +```sh +cloudshell edit two-vms.yaml +``` + +Deploy your configuration to confirm the changes work: + +```sh +gcloud deployment-manager deployments create deployment-with-helper-script --config two-vms.yaml +``` + +## View your deployment + +View your deployment with the following command: + +```sh + gcloud deployment-manager deployments describe deployment-with-helper-script +``` + +## Delete the deployment + +Once again, you will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-with-helper-script +``` + +### Looking forward: updating deployments + +Next, learn to add, delete, and change the properties of resources of a deployment as your application evolves. + +## Updating a deployment + +Once you have created a deployment, you can update it as your application changes. You can use Deployment Manager to update a deployment by: + ++ Adding or removing resources from a deployment ++ Updating the properties of existing resources in a deployment + +You will now update a deployment by changing the metadata in `vm-template.py`. + +## Deploy the original configuration + +In this step, deploy the configuration that you will later update. + +First, run the following command to open your home directory: + +```sh +cd +``` + +Next, change to this directory: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step8_metadata_and_startup_scripts/python +``` + +Deploy your configuration: + +```sh +gcloud deployment-manager deployments create deployment-to-update --config config-with-many-templates.yaml +``` + +## Viewing the updated template + +In this example, the metadata is changed in `vm-template.py`. + +To view these changes, first run the following command to open your home directory: + +```sh +cd +``` + +Next, change to this directory: + +```sh +cd deploymentmanager-samples/examples/v2/step_by_step_guide/step9_update_a_deployment/python +``` + +Then, open `vm-template.py`: + +```sh +cloudshell edit vm-template.py +``` + +Notice that the metadata section is changed in the file. + +## Commit the update + +### Preview the configuration + +To preview your updated configuration before committing changes, run the following command: + +```sh +gcloud deployment-manager deployments update deployment-to-update --config config-with-many-templates.yaml --preview +``` + +### Update the configuration + +To commit the update, run the following command: + +```sh +gcloud deployment-manager deployments update deployment-to-update +``` + +## Delete the deployment + +You will want to delete the deployment to avoid charges. Run the following command to delete the deployment: + +```sh +gcloud deployment-manager deployments delete deployment-to-update +``` + +### Next: Wrapping up + +## Wrapping up + + + +Congratulations! You've completed the Step-by-Step Walkthrough of Deployment Manager! + +You learned skills such as: + ++ Deploying resources ++ Creating templates ++ Setting template properties ++ Setting environment variables ++ Creating helper scripts ++ Updating deployments + +### What's Next + +Here are some areas to explore as you learn more details about specific Deployment Manager functions: + ++ [Explore more complex tutorials](https://cloud.google.com/deployment-manager/docs/tutorials) ++ Page on metadata and startup scripts ++ [Learn about available resource types](https://cloud.google.com/deployment-manager/docs/configuration/supported-resource-types) ++ [Read the environment variables documentation](https://cloud.google.com/deployment-manager/docs/configuration/templates/use-environment-variables) ++ [Read about importing Python libraries](https://cloud.google.com/deployment-manager/docs/configuration/templates/import-python-libraries) ++ [Understand guidelines for preparing updates](https://cloud.google.com/deployment-manager/docs/deployments/updating-deployments)