Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Terraform + GCP + Git Example (2024) | TechGeekNxt>>


Terraform + GCP + Git Example (2024)

The majority of businesses are migrating to the public cloud. Managing infrastructure in a secure and controlled manner is a critical step for businesses.

Terraform is an open source provisioning tool. Terraform is a cross-platform application that works on Linux, Windows, and MacOS.

In this example, we'll look at how we can use Terraform to provision infrastructure on gcp while keeping the infrastructure code in a github repository.

Setup Github account

  1. You can use your existing Github account or create a new free account
  2. Then Click on "Create new repository" as "terraform-getting-started" as private repository
  3. Select "Add a README file" from the Initialize section, then click "Create Repository."
  4. After the repository has been created, click the "Add file" button and select "Create new file" from the dropdown menu.
  5. Add the following code to the new file and save it with name as main.ts.
    provider "google" {
    project = "qwiklabs-gcp-04-576cc70c1ddb"
    region = "us-west1"
    }
    
    resource "google_compute_instance" "myvm" {
    name = "myvm-dev"
    zone = "us-west1-c"
    boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
      size = "10"
    }
    }
    machine_type = "f1-micro" 
    network_interface {
    network = "default"
    }
    }

Now our Git Accounts are ready with our sample terraform repository.

Setup Terraform Cloud Account :

Terraform Cloud delivers features such as remote state management, API-driven runs, policy administration and much more. A cloud-based SaaS solution is preferred by most consumers since they do not want to maintain the infrastructure to run it.

  1. Let's begin by signup for a free Terraform cloud account at: https://app.terraform.io/signup/account
  2. After logging in, select "create new organization" and give it the name as "techgeeknext."

Configure Terraform VCS

  1. Let's "Create New Workspace" with "Version control workflow" type.
  2. Select provider as "GitHub" from the "Connect to VCS" tab. Make sure your pop-up blocker is turned on so you can enter into your github account and provide terraform access.
  3. Choose a git repository
  4. Finally provide workspace name and save the Configuration

Setup GCP Free Trial Account

After that, we'll set up a Google Cloud Platform account. GCP is giving new customers a 90-day free trial account with $300 in credit to try out all of Google's cloud services.

  1. Go to https://cloud.google.com/free link and click the "Get Started for Free" icon in the top right corner to get started for free.
  2. It will take you to the Sign-In page, where you can sign in using your Gmail ID.
  3. It will take you to the GCP Free-trial page after you sign in. After selecting your country from the drop-down menu and agreeing to the Terms of Service, click Continue.
  4. In next step, fill in your personal information.
  5. Select the payment option, give your card details and click on Start my free trial button.
  6. It will next ask you to enter your security code and confirm your credit or debit card. Then press the button that says "Continue."
  7. This will take you to the payment gateway to verify your payment information, and Google will charge you the lowest fee for credit card verification based on your country.
    Note: The nominal amount deducted will be refunded at a later date.
  8. You'll be taken to the Google Cloud Platform (Console) page after successful authentication, where you can start building projects and get hands-on experience.

Setup GCP Service Account & Key

  1. Visit Google Cloud Platform page (https://console.cloud.google.com/)
  2. Go to "IAM & Admin > Service Accounts" from the Navigation menu and click the "Create service account" button on the top tool bar.
  3. Enter Server Account name : (e.g. terraform gcp demo)

    Next, grant service account access to project (e.g. Role - > Basic - > Owner) and click Done.

  4. Then select the newly created service account and go to Manage Keys
  5. Create Key with JSON Key type

  6. The key will be downloaded to your browser when you click "CREATE."
  7. In the coming step, we'll use this json key to connect to our Terraform account.

Update project name in github terraform script main.ts

  1. Copy the project id from your GCP console and replace it in the github repository's main.ts file.

Add Terraform Variable to connect with GCP account

  1. Navigate to the "Variable" tabs in the Terraform workspace (terraform-getting-started).
  2. Now, create a new Environment Variable with the key "GOOGLE CREDENTIALS" then in the Value box, paste the service account key in json format that we downloaded in the previous step, also select the checkbox "Sensitive" to keep the key confidential.

  3. Note: Here's how to store an environment variable if you're experiencing trouble. Then there's a workaround:
    • You can create new "temp" Environment variable in Terraform and set json key as it's value. Then save it without sensitivity.
    • This will save the key in required format for "temp" variable that you can use to copy it to "GOOGLE CREDENTIALS" variable value.
    • You can finally delete the temp key.

Run Terraform Plan

  1. Now that we've completed our setup, let's trigger a new plan by selecting "Queue plan manually" for the first time.
  2. Initiate the plan: This will pull the code from the Github repository, run it, and display the plan output after it's finished.
  3. Apply Plan : After you've reviewed the plan, click "Apply Plan" to have the infrastructure provisioned on GCP.
  4. Verify resource on GCP

Conclusion: Now, Terraform will plan and provision resources on GCP automatically if you commit anything to your git repository hereafter.
















Recommendation for Top Popular Post :