Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Cloud Deployment Manager Tutorial

Introduction

Google Cloud Deployment Manager is an infrastructure deployment service that automates the creation and management of Google Cloud resources. It allows you to specify all the resources needed for your application in a declarative format using YAML, Python, or Jinja2 templates.

Prerequisites

Before you start using Cloud Deployment Manager, ensure you have the following:

  • A Google Cloud Platform (GCP) account.
  • gcloud command-line tool installed and configured.
  • Basic knowledge of YAML or Python.

Setting Up

To start using Cloud Deployment Manager, follow these steps:

  1. Open the Cloud Shell or your local terminal.
  2. Authenticate with your Google Cloud account using the following command:
  3. gcloud auth login
  4. Set your desired project:
  5. gcloud config set project [YOUR_PROJECT_ID]

Creating a Simple Deployment

Let's create a simple deployment that provisions a Compute Engine instance. Create a YAML file named simple-vm.yaml with the following content:

resources:
- name: my-vm
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    machineType: zones/us-central1-f/machineTypes/f1-micro
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-9
    networkInterfaces:
    - network: global/networks/default
                

To deploy this configuration, run the following command:

gcloud deployment-manager deployments create my-first-deployment --config simple-vm.yaml

After deployment, you can check the status with:

gcloud deployment-manager deployments describe my-first-deployment

Updating a Deployment

To update an existing deployment, modify your YAML file and use the following command:

gcloud deployment-manager deployments update my-first-deployment --config simple-vm.yaml

For example, to change the machine type, update the machineType property in your YAML file and run the command above.

Deleting a Deployment

To delete a deployment and all associated resources, use this command:

gcloud deployment-manager deployments delete my-first-deployment

Advanced Configuration

Cloud Deployment Manager supports advanced configurations using Python or Jinja2 templates. Here is an example using a Jinja2 template:

{% raw %}
resources:
- name: my-vm
  type: compute.v1.instance
  properties:
    zone: {{ properties['zone'] }}
    machineType: zones/{{ properties['zone'] }}/machineTypes/{{ properties['machineType'] }}
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-9
    networkInterfaces:
    - network: global/networks/default
{% endraw %}
                

Save this template as vm-template.jinja and create a configuration file vm-config.yaml:

imports:
- path: vm-template.jinja

resources:
- name: my-vm
  type: vm-template.jinja
  properties:
    zone: us-central1-f
    machineType: f1-micro
                

Deploy using:

gcloud deployment-manager deployments create my-template-deployment --config vm-config.yaml

Conclusion

Google Cloud Deployment Manager is a powerful tool for managing your cloud infrastructure through code. It allows you to create, update, and delete resources in a repeatable and automated manner. With this tutorial, you should now have a basic understanding of how to use Deployment Manager to manage your Google Cloud resources.