Google Cloud: Using Terraform to Create a New VPC and Public Subnet in GCP

Share At:

Learn The 6 Fundamentals Of Terraform — In Less Than 20 Minutes

Scenario

Your company wants to provision a new network with a public subnet so they can test any new instances within that public subnet. They have tasked you with creating this VPC and subnet through Infrastructure as Code so they can test and launch resources as necessary.

You’ll need to accomplish the following steps to complete your task:

1. Create a service account.

2. Create a service account key to be used with the host instance.

3. Ensure Terraform is installed.

4. Create a main.tf file to create the configuration for the VPC and subnet.

5. Use terraform apply to execute the plan.

Create a Service Account

  1. From Google Cloud console’s main navigation, choose IAM & Admin > Service Accounts.
  2. Click Create service account.
  3. Give your service account a name.
  4. Click Create.
  5. In the roles dropdown, select Project > Owner.
  6. Click Continue and then Done.

Log in to the Host Instance and Ensure Terraform Is Installed

  1. From Google Cloud navigation, choose Compute Engine > VM instances.
  1. Click SSH next to terraform-instance.
  2. Use root privileges: sudo -i
  3. Change into the root directory: cd /
  4. Input the path to communicate with Terraform into the /etc/profile file: echo "PATH='$PATH:/downloads/'" >> /etc/profile
  5. Run the following in order to be able to call Terraform: source /etc/profile
  1. Call Terraform: terraform

Create a Service Account Key within the Instance

  1. Allow the SDK to communicate with GCP: gcloud auth login
  1. Click on the link given, allow the cloud_user email to retrieve the key, and copy and paste the key into your terminal.
  1. Create the service account key: gcloud iam service-accounts keys create /downloads/instance.json --iam-account <SERVICE_ACCOUNT>

gcloud iam service-accounts keys create /downloads/instance.json –iam-account terraform@using-terraf-156-16d1549b.iam.gserviceaccount.com

Create and Deploy the Configuration File

  1. Create a main.tf file: vi main.tf
  2. Paste the following configuration:

provider “google” {

  version = “3.5.0”

  credentials = file(“/downloads/instance.json”)

  project = “”

  region  = “us-central1”

  zone    = “us-central1-c”

}

resource “google_compute_network” “vpc_network” {

  name = “terraform-network”

}

resource “google_compute_subnetwork” “public-subnetwork” {

  name          = “terraform-subnetwork”

  ip_cidr_range = “10.2.0.0/16”

  region        = “us-central1”

  network       = google_compute_network.vpc_network.name

  }

  1. Save and exit the file by pressing Escape followed by :wq.
  1. Use terraform init , terraform validate , terraform plan, and then terraform apply.

5. Now go to VPC network and you will see that the sub network called “terraform-subnetwork” has been created.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Create Account
3 days ago

The point of view of your article has taught me a lot, and I already know how to improve the paper on gate.oi, thank you. https://www.gate.io/pt-br/signup/XwNAU

binance sign up
5 days ago

Can you be more specific about the content of your enticle? After reading it, I still have some doubts. Hope you can help me. https://accounts.binance.com/en/register?ref=P9L9FQKY

gate io nedir
24 days ago

After reading your article, I have some doubts about gate.io. I don’t know if you’re free? I would like to consult with you. thank you.

Back To Top

Contact Us