Updating a Deployment

After you have a deployment, you can update or change the deployment as your application evolves, by making an update request to Deployment Manager. Deployment Manager allows you to change a deployment by:

  • Adding or removing resources to the deployment
  • Updating some properties of existing resources in your deployment

Remember that Deployment Manager uses the underlying APIs of Google Cloud Platform services to manage resources in your deployment. If an API doesn't support a certain action, Deployment Manager cannot perform that action either. For example, Deployment Manager can only update an existing resource if there is an update method in the corresponding API.

In addition to standard update methods, in certain APIs, there are custom methods that can change the properties of a resource. For example, in Compute Engine, there is a method for changing the metadata of a running instance. Deployment Manager can also take advantage of these custom methods to perform updates to your deployment.

To check what kind of methods are supported, read the respective product documentation.

Selecting update policies

There are many update policies you can choose from when you perform an update but for most cases, the default settings are enough. As such, you might not need to explicitly provide any policies and Deployment Manager will use the default policies. The default update policies are:

  • Default policy for adding resources is to CREATE_OR_ACQUIRE, which acquires a resource if it already exists and adds it to the deployment, or creates the resource if it doesn't exist.
  • Default policy for removing resources is to DELETE, which removes the resource from deployment and deletes the resource from your project.
  • Default policy for updating a resource is to use the UPDATE method, or a custom method if the method exists.

For detailed information on update policies, see Updating a Deployment.

Previewing an update

Before you commit to any changes in your deployment, you can preview an update, which gives you a glimpse into how your updates are applied before you make the decision to change your deployment. Previewing an update causes Deployment Manager to start updating your deployment but then stops short of actually changing any resources. Instead, Deployment Manager creates "shell" resources that have the desired changes but that only exist in Deployment Manager and not in any of the respective Cloud Platform services. From that point, you can decide to either cancel the preview or move forward with the update.

We recommend generally previewing updates so that you can check your changes before committing to them.

How to update a deployment

Updating a deployment is done in three steps:

  1. Create an updated configuration with the changes you want.
  2. Select the desired update policies or use the default policies.
  3. (Optional) Preview your update.
  4. Perform the update.

Let's take the last example, and update the startup script in vm-template.py or vm-template.jinja.

Edit the vm-template file

Change the metadata section so that it now looks like this:

Jinja

{#
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#}

resources:
- name: {{ env["name"] }}
  type: compute.v1.instance
  properties:
    zone: {{ properties["zone"] }}
    machineType: https://www.googleapis.com/compute/v1/projects/{{ env["project"] }}/zones/{{ properties["zone"] }}/machineTypes/{{ properties["machineType"] }}
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9
    networkInterfaces:
    - network: $(ref.{{ properties["network"] }}.selfLink)
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          INSTANCE=$(curl http://metadata.google.internal/computeMetadata/v1/instance/hostname -H "Metadata-Flavor: Google")
          echo "<html><header><title>Hello from Deployment Manager!</title></header>
          <body><h2>Hello from $INSTANCE</h2>
          <p>Deployment Manager bids you good day!</p>
          </body>
          </html>" > index.html
          python -m SimpleHTTPServer 80

Python

# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Creates the virtual machine with environment variables and startup script."""

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


def GenerateConfig(context):
  """Creates the virtual machine."""

  resources = [{
      'name': context.env['name'],
      'type': 'compute.v1.instance',
      'properties': {
          'zone': context.properties['zone'],
          'machineType': ''.join([COMPUTE_URL_BASE, 'projects/',
                                  context.env['project'], '/zones/',
                                  context.properties['zone'], '/machineTypes/',
                                  context.properties['machineType']]),
          'disks': [{
              'deviceName': 'boot',
              'type': 'PERSISTENT',
              'boot': True,
              'autoDelete': True,
              'initializeParams': {
                  'sourceImage': ''.join([COMPUTE_URL_BASE, 'projects/',
                                          'debian-cloud/global/',
                                          'images/family/debian-9'])
              }
          }],
          'networkInterfaces': [{
              'network': '$(ref.' + context.properties['network']
                         + '.selfLink)',
              'accessConfigs': [{
                  'name': 'External NAT',
                  'type': 'ONE_TO_ONE_NAT'
              }]
          }],
          'metadata': {
              'items': [{
                  'key': 'startup-script',
                  'value': ''.join(['#!/bin/bash\n',
                                    'INSTANCE=$(curl http://metadata.google.',
                                    'internal/computeMetadata/v1/instance/',
                                    'hostname -H "Metadata-Flavor: Google")\n',
                                    'echo "<html><header><title>Hello from ',
                                    'Deployment Manager!</title></header>',
                                    '<body><h2>Hello from $INSTANCE</h2><p>',
                                    'Deployment Manager bids you good day!</p>',
                                    '</body></html>" > index.html\n',
                                    'python -m SimpleHTTPServer 80\n'])
              }]
          }
      }
  }]
  return {'resources': resources}

Save your changes. You are using the default policies for the updates so you do not have to make additional changes.

Preview your configuration

gcloud deployment-manager deployments update deployment-with-startup-script  \
  --config config-with-many-templates.yaml --preview

You can explore your proposed changes by viewing your new resources, describing the manifest, and so on. For example, you can get a description of the proposed changes to the-first-vm by describing the resource:

gcloud deployment-manager resources describe the-first-vm --deployment deployment-with-startup-script

Commit the update

gcloud deployment-manager deployments update deployment-with-startup-script

Restart your instance

To check if the update worked, we need to restart our instances to pick up on the new startup script. Restart the-first-vm:

gcloud compute instances reset the-first-vm

The instance might take some time to start back up. Wait a couple minutes before confirming the change.

Confirm the change

  1. Get the external IP of the-first-vm:

    gcloud compute instances describe the-first-vm | grep "natIP"
    
  2. Copy the value.

  3. Open a browser and paste the IP address into the address bar to visit your instance. The new webpage should now show a different welcome message.

You can also repeat these steps with the-second-vm and see a slightly different message.

Delete your deployment

Compute Engine resources incur charges, so you should delete this deployment, as it is no longer necessary for future steps. Deleting this deployment deletes all the resources in this deployment as well. If you don’t delete the deployment, you will run into conflicts with future examples.

To delete this deployment:

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

Related topics

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Deployment Manager Documentation