Open In App

Terraform GCP Resource Automation

Terraform is a popular tool for framework mechanization that permits you to describe your framework in an explanatory way using straightforward language structure or syntax. It gives a way to automate the creation, adjustment, modification, and erasure of cloud assets across various cloud providers, counting GCP (Google Cloud Platform)

Pre-requisite: GCP,Terraform 



Steps To Automate GCP Cloud Resource

Example 1:

Step 1: Service account with appropriate permissions to create resources in the GCP project. Service account’s JSON key file.



 

Step 2: Terraform script for creating a VPC network in GCP (Given script assumes that you have already set up the authentication for terraform to access your GCP account).

 

 

Step 3: Terraform script for creating Private Subnet in GCP a private subnet is a subnet that does not have a direct connection to the internet. Resources within a private subnet can only communicate with other resources within the same subnet, or with resources in other subnets within the same VPC network if allowed through firewall rules or VPN connections. This makes private subnets useful for hosting resources that should not be directly exposed to the internet, such as database servers, internal APIs, or backend services. To create a private subnet in GCP using Terraform, you need to create a subnet with an IP address range that does not overlap with any other subnet in the same VPC network, and configure the subnet to disallow traffic from the internet. You can also configure the subnet to allow traffic to and from other subnets or networks within GCP, if necessary.

 

Step 4: Terraform script for creating Public Subnet in GCP a public subnet is a subnet that has a direct connection to the internet, meaning that resources within the subnet can send and receive traffic from the internet. This makes public subnets useful for hosting resources that need to be directly accessible from the internet, such as web servers or load balancers. To create a public subnet in GCP using Terraform, you need to create a subnet with an IP address range that does not overlap with any other subnet in the same VPC network, and configure the subnet to allow traffic from the internet. You can also configure the subnet to allow traffic to and from other subnets or networks within GCP, if necessary.

 

Example 2:       

Step 1: Create a new terraform project and add the required provider.

 

Step 2: Define networking configuration for VM.

 

Step 3: Create a firewall rule to allow SSH access to the VM.

 

Step 4: Define VM configuration.

 

Step 5: Terraform script to create jump server/Bastion Host in GCP to ssh server which is in the private subnet.

 

Authentication to GCP from terraform: To authenticate Google Cloud Platform from terraform, Google Cloud provider plugin for Terraform. To begin with, we’ll get to make a service account in GCP with the fitting parts and authorizations. At that point, we’ll download the JSON key record related to the benefit account, which contains the authentication information.

 

Next, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of the JSON key file on your local machine. This will allow Terraform to use the credentials to authenticate to GCP.

 


Article Tags :