top of page

Automating Infrastructure on GCP with Deployment Manager: A Hands-On Guide

Updated: Oct 21, 2024

In today’s fast-paced digital world, managing cloud infrastructure manually can be time-consuming and prone to error. With Google Cloud Platform (GCP), automating infrastructure management becomes easier, allowing you to deploy, configure, and manage your resources with just a few lines of code. This is where Google Cloud Deployment Manager steps in. In this guide, we’ll explore how to automate infrastructure on GCP using Deployment Manager, with practical examples, architecture diagrams, and real-world use cases.



Why Automate Infrastructure?

Automation is a crucial element in the world of cloud computing. It offers several benefits such as:

●     Consistency: Automated processes eliminate human error, ensuring the infrastructure is set up consistently.

●     Scalability: It allows you to scale resources up or down without manual intervention.

●     Efficiency: Deploy infrastructure in minutes, not days.

●     Cost Management: Automating infrastructure can help optimize the use of cloud resources, reducing costs.

Now, let’s dive into GCP’s Deployment Manager and understand how you can automate your infrastructure effortlessly.


What is Google Cloud Deployment Manager?

Google Cloud Deployment Manager is an infrastructure-as-code (IaC) tool that allows you to define and manage Google Cloud resources in declarative templates. These templates are written in YAML, Jinja, or Python, making it easier to provision resources like Compute Engine instances, Cloud Storage buckets, Firewalls, and more, without manual clicks in the Google Cloud Console.


Key Features of Deployment Manager:

●     Declarative Syntax: You define what you want, and Deployment Manager figures out how to do it.

●     Templates and Modular Configurations: It allows you to reuse and modularize configurations.

●     Preview Changes: You can preview changes before deploying them.

●     Integrated with GCP Tools: Seamlessly integrates with GCP’s other tools, ensuring streamlined infrastructure management.


Getting Started: Setting Up Deployment Manager

Step 1: Enable Required APIs

Before you can start using Deployment Manager, ensure that the Google Cloud Deployment Manager API is enabled for your project. You can do this from the GCP Console:

  1. Navigate to the APIs & Services dashboard.

  2. Search for Deployment Manager API and enable it.

Alternatively, you can enable it via the terminal:

gcloud services enable deploymentmanager.googleapis.com


Step 2: Set Up a GCP Project

Ensure that you have a Google Cloud project. If you don’t, you can create one by navigating to the GCP console and selecting Create Project.


Hands-On: Deploying a Simple Compute Engine Instance

Let’s walk through deploying a simple Compute Engine instance using Deployment Manager. Follow these steps:

Step 3: Create a YAML Configuration File

Create a configuration file that describes the infrastructure you want to deploy. In this example, we’ll create a Compute Engine virtual machine instance.

Create a file named vm-config.yaml with the following content:

resources:

- name: my-vm-instance

  type: compute.v1.instance

  properties:

    zone: us-central1-a

    machineType: zones/us-central1-a/machineTypes/n1-standard-1

    disks:

    - deviceName: boot

      type: PERSISTENT

      boot: true

      initializeParams:

        sourceImage: projects/debian-cloud/global/images/family/debian-10

    networkInterfaces:

    - network: global/networks/default

      accessConfigs:

      - name: External NAT

        type: ONE_TO_ONE_NAT


In this YAML file:

●     We define a Compute Engine instance named my-vm-instance.

●     It will use the n1-standard-1 machine type.

●     The instance will be booted from a Debian 10 image.


Step 4: Deploy the Configuration

Deploy the instance using the following command in Cloud Shell or your local terminal:

bash

Copy code

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


This command will:

●     Create a deployment called my-first-deployment.

●     Deploy the VM as described in the vm-config.yaml configuration file.


Step 5: Verify the Deployment

Once the deployment is complete, you can verify it by listing all deployments:

gcloud deployment-manager deployments list


To check the status of individual resources, use:

gcloud compute instances list


Modular Templates: Reusability and Scalability

While our first example was straightforward, real-world deployments often require deploying complex architectures. GCP’s Deployment Manager supports modular templates, allowing you to break down configurations into smaller, reusable components.

Step 6: Create a Template

Let’s create a reusable template for a VM. Create a new directory and add a Jinja2 template for the VM, named vm-template.jinja.

resources:

- name: {{ properties['name'] }}

  type: compute.v1.instance

  properties:

    zone: {{ properties['zone'] }}

    machineType: zones/{{ properties['zone'] }}/machineTypes/{{ properties['machineType'] }}

    disks:

    - deviceName: boot

      type: PERSISTENT

      boot: true

      initializeParams:

        sourceImage: projects/debian-cloud/global/images/family/debian-10

    networkInterfaces:

    - network: global/networks/default

      accessConfigs:

      - name: External NAT

        type: ONE_TO_ONE_NAT


Step 7: Create a New Configuration

Now, create a configuration file (main-config.yaml) that references this template.

imports:

- path: vm-template.jinja

 

resources:

- name: instance-from-template

  type: vm-template.jinja

  properties:

    name: my-template-instance

    zone: us-central1-a

    machineType: n1-standard-1


Step 8: Deploy Using the Template

Deploy the new configuration:

gcloud deployment-manager deployments create template-deployment --config main-config.yaml


This command deploys a new Compute Engine instance using the template.

Real-Time Case: Automating Multi-Tier Architectures

In a real-world scenario, let’s consider deploying a multi-tier application architecture on GCP, consisting of a web server, an application server, and a database.


Step 9: Define Each Layer

Create YAML or Jinja templates for each layer, and manage them under a single configuration file. For example:

●     Web Server Configuration: A Compute Engine instance running NGINX.

●     Application Server Configuration: A Compute Engine instance with Java or Python backend.

●     Database Configuration: Cloud SQL instance running MySQL.

You can modularize the deployment to ensure the flexibility and scalability of your infrastructure. Deployment Manager allows you to preview, update, and rollback changes, making it ideal for managing complex architectures.

Previewing and Updating Deployments

Before applying changes, you can preview them to avoid disruptions:

gcloud deployment-manager deployments update my-deployment --config new-config.yaml --preview


Once you are satisfied, execute the update:

gcloud deployment-manager deployments update my-deployment


Rollback to Previous State

In case of any issues, you can effortlessly revert to a previous state:

gcloud deployment-manager deployments cancel-preview my-deployment


Best Practices for Using Deployment Manager

  1. Use Modular Templates: Break down large configurations into reusable templates for better maintainability.

  2. Version Control: Use a version control system such as Git to save your configuration files and monitor changes.

  3. Monitoring: Integrate Stackdriver Monitoring to keep an eye on resource usage and health.

  4. Security: Implement appropriate IAM roles to restrict access to sensitive resources.


Conclusion

By leveraging Google Cloud Deployment Manager, you can automate the deployment and management of your cloud infrastructure with ease. The declarative syntax allows you to focus on what you want to achieve, while GCP takes care of the how. Whether you’re deploying a simple VM or a complex multi-tier architecture, Deployment Manager ensures consistency, scalability, and efficiency in your cloud environment


References:

●     Google Cloud APIs


Disclaimer:

The content in this blog is for informational purposes only. While every effort has been made to ensure the accuracy of the information, users should independently verify its applicability to their environment before implementing.

コメント


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2035 by Train of Thoughts. Powered and secured by Wix

bottom of page