DEV Community

Cover image for Managing Google Cloud using Infrastructure as Code (IaC)
Adil Shahzad for GDG Cloud Lahore

Posted on

Managing Google Cloud using Infrastructure as Code (IaC)

Infrastructure as Code

Infrastructure as Code (IaC) is a method used to manage and set up computer networks, servers, and other IT infrastructure through machine-readable files, rather than physical hardware configuration or interactive configuration tools. To understand it easily, think of it like building a model house using LEGO blocks. In traditional methods (like building a house by hand), you would manually place each brick. But with IaC, you create a blueprint or a plan that tells an automated system exactly how to build your model house. Whenever you want to build the house, you just use this plan, and the system automatically assembles the LEGO blocks as per your design.

This approach makes the entire process of managing IT infrastructure more efficient, consistent, and error-free. You can easily modify the plan if you want to change something (like adding a room to your model house), and the system will adjust everything accordingly. It's like updating your LEGO blueprint, and the model updates itself.

Understanding API

API stands for Application Programming Interface. Imagine an API as a menu in a restaurant. The menu provides a list of dishes you can order, along with a description of each dish. When you specify which dish you want, the kitchen (the system) prepares the dish and serves it to you (the user). In this analogy, the menu is the API, the order is the request, and the dish that is served to you is the response.

Now, when it comes to Google Cloud API, think of it as a specialized menu offered by a very advanced restaurant (Google Cloud). This restaurant offers a variety of dishes (services) like storing your data, running your website, or analyzing large amounts of information. Each dish/service is accessed through its part of the menu/API. So, when you ask for a specific service, like data storage, the Google Cloud kitchen works behind the scenes to provide you with that service.

In simpler terms, APIs allow different software programs to communicate with each other. Google Cloud API, in particular, lets your software access the various services offered by Google Cloud, like storing files, processing data, or even running complex algorithms.

Exploring Cloud Shell

Google Cloud Shell provides a convenient, ready-to-use command-line environment for interacting with Google Cloud services without the hassle of setting up and maintaining a separate computer or server for these tasks.

Google Cloud Shell

Managing Version Controlling using Git

Managing version control using Git is crucial for efficient collaboration in software development. Git allows multiple individuals to work on the same project without interfering with each other's changes. Here are the top git commands you can use , for more commands checkout here : Git Cheatsheet

1. git init

  • Usage: Initializes a new Git repository in your project directory.
  • Command: git init

2. git status

  • Usage: Shows the status of changes as untracked, modified, or staged.
  • Command: git status

3. git add

  • Usage: Starts tracking new files and stages any changes to existing files.
  • Command: git add <filename> or git add . (to add all files)

4. git commit

  • Usage: Records your changes as a commit with a descriptive message.
  • Command: git commit -m "Commit message"

5. git push

  • Usage: Pushes your commits to the remote repository.
  • Command: git push -u origin master

6. git pull

  • Usage: Fetches and merges changes from the remote server to your local repository.
  • Command: git pull

GCP Deployment Manager

Google Cloud Deployment Manager is a tool that lets you manage your Google Cloud resources using a declarative format. Here is the overview of how to write a Deployment Manager template file.

  1. Specify the Template Version: At the top of the file, you define the version of the template that you are using. This helps the Deployment Manager understand how to process the file.

  2. Resource Definitions: Each resource you want to create, like a virtual machine or a storage bucket, is defined in a section called 'resources'. For each resource, you provide details such as:

    Name: A unique identifier for the resource.
    Type: The kind of resource you want to create, like a
    compute engine instance or a Cloud SQL database.
    Properties: The specific settings for the resource. For
    a compute instance, this could include the
    machine type, the image to use, the network
    settings, and more.

  3. Configuration Parameters (optional): Sometimes, you might want to pass in parameters that can change, like the machine type or the zone you want to deploy to. You can define these parameters at the beginning of your file and reference them within your resources.

  4. Outputs (optional): After your resources are created, you might want to output certain information, like the IP address of a virtual machine. You can define outputs to retrieve this information after deployment.

Here is the sample YAML template for Deployment Manager to create a virtual machine.

resources:
- name: my-vm
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/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

Enter fullscreen mode Exit fullscreen mode

Lab

First, access the Cloud Shell and ensure you have access to the project

Image description

Then, from the terminal, type the following command to clone the repository

git clone https://github.com/GDGCloudLahore/GCP-Deployment-Manager-IaC.git
Enter fullscreen mode Exit fullscreen mode

Next, navigate to the basic01 lab.

Image description

Now, add the following command. The Google Cloud Shell will ask you for authorization

gcloud deployment-manager deployments create my-vm-deployment --config vm-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

"In case you face any issue or error, please open an issue on our GitHub repository."

Image description

After receiving a successful creation message, you are now able to view the compute resource.

Image description

I hope you like this blog. We have many blogs in the pipeline that we are working on. You are welcome to contribute to our GitHub Repository if you have experience working with Deployment Manager. If you have any questions, please feel free to ask me on LinkedIn.

This blog is written and managed by Adil Shahzad

Join our discussion room to ask questions from cloud experts : https://www.googlecloudlahore.com/

Top comments (1)

Collapse
 
usamazaheer profile image
Usama Zaheer

Insightful