Managing Google Cloud using Infrastructure as Code (IaC)
Tags: googlecloud, iac, infrastructureascode, cloud
Published: 2024-01-14 17:00:19

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cg21vi7kpm5b21qapia.png)

## 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](https://education.github.com/git-cheat-sheet-education.pdf)

### 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. 

```yaml
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

```

## Lab 

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9oj58qwrx3ogd4e3mxv.png)



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

```git 
git clone https://github.com/GDGCloudLahore/GCP-Deployment-Manager-IaC.git
```


Next, navigate to the basic01 lab.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uj76utafnzu5pve7qhk.png)

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

```sh
gcloud deployment-manager deployments create my-vm-deployment --config vm-deployment.yaml
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhe08yay2yjg1f6o003z.png)


_"In case you face any issue or error, please open an issue on our [GitHub repository](https://github.com/GDGCloudLahore/GCP-Deployment-Manager-IaC/issues)."_ 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/clmuh2o1f5848xwwl54c.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ta98lo5g7d58rzeoxvsb.png)



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](https://github.com/GDGCloudLahore/GCP-Deployment-Manager-IaC.git) 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](https://www.linkedin.com/in/adilshehzad7/)

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