Terraform GCP Resource Automation
Last Updated :
16 Apr, 2023
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.
-(1).jpg)
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).
.png)
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.
.jpeg)
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.
Similar Reads
Terraform Data Sources Terraform is a powerful tool used for Infrastructure as Code (IaC), which helps in management and deployment of Infrastructure. The important feature that make Terraform powerful is its ability to interact with data sources. Terraform data sources are used to get data from external systems or servic
7 min read
Terraform Resources A Terraform resource is like a building block in a blueprint for your infrastructure. Think of it as a specific piece of the infrastructure that you want to create, manage, or update. For example, it could be a virtual machine, a database, a storage bucket or a load balancer.When using Terraform, yo
13 min read
How To Create Azure Resource Group Using Terraform ? As more organizations adopt multi-cloud strategies and deploy applications in diverse regions and instances, managing this stack has grown much more intricate. By way of solving problems manually, the provisioning process might take a lot of time, may be incorrect sometimes, or pave the way to incon
11 min read
Terraform Configuration File Terraform is one of the important tools used for managing Infrastructure as code (IaC). It eases defining, provisioning, and automating of infrastructures from multiple clouds AWS, Azure, and Google Cloud. A configuration file serves as the heart of Terraform which defines the infrastructure that ha
8 min read
Modules Block in Terraform Pre-requisite: Terraform Users can define and provision infrastructure resources declaratively using the open-source infrastructure as code (IaC) tool known as Terraform. It enables teams to effectively manage their infrastructure across a range of cloud providers and on-premises settings. The capab
6 min read
What is Terraform Console Command ? The terraform console command in Terraform opens an interactive console where you can evaluate and explore Terraform expressions and resource state in real-time.Why We Should use Terraform Console ?The terraform console command is a feature of the Terraform CLI that launches an interactive environme
5 min read