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

Using Terraform to Create a New VPC and Public Subnet in GCP
Techiio-author
Written by Abhishek JalanApril 18, 2020
3 min read
Terraform
6 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 6 VIEWS SHARE
Techiio-author
Abhishek Jalan

DevSecOps Engineer

Creating a virtual private network and subnetworks is the foundation of using resources or any infrastructure within GCP

In this hands-on lab, we will learn how to use Terraform to create a VPC and public subnet.

Create a Service Account

  • From Google Cloud console's main navigation, choose IAM & Admin > Service Accounts.
  • Click Create service account.
  • Give your service account a name.
  • Click Create.
  • In the roles dropdown, select Project > Owner.
  • Click Continue and then Done.

Log in to the Host Instance and Ensure Terraform Is Installed

From Google Cloud navigation, choose Compute Engine > VM instances.

Click SSH next to terraform-instance.

Use root privileges:

sudo -i

Change into the root directory:

cd /

Input the path to communicate with Terraform into the /etc/profile file:

echo "PATH='$PATH:/downloads/'" >> /etc/profile

Run the following in order to be able to call Terraform:

source /etc/profile

Call Terraform:

terraform

Create a Service Account Key within the Instance

Allow the SDK to communicate with GCP:

gcloud auth login

Click on the link given, allow the cloud_user email to retrieve the key, and copy and paste the key into your terminal.

Create the service account key:

gcloud iam service-accounts keys create /downloads/instance.json --iam-account <SERVICE_ACCOUNT>

Create and Deploy the Configuration File

Create a main.tf file:

vim main.tf

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
  }

Save and exit the file by pressing Escape followed by :wq.

Use terraform init , terraform validate , terraform plan, and then terraform apply.

GCP
Google
Terraform
vpc
networking
6 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 6 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Abhishek Jalan
DevSecOps Engineer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+60 more
19 Blog Posts
9 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance