Create VPC in Google Cloud and deploy with Terraform
How to start from scratch and deploy a vpc to terraform.
Access the Google Cloud Management Console at https://console.cloud.google.com.
Create a project or select an existing project.
In the navigation pane, click “VPC Network” and then “VPC Networks”.
Click the “Create VPC Network” button.
Fill in the VPC details such as name and region.
Choose subnet mode: automatic or custom. In automatic mode, Google Cloud automatically manages the allocation of IP ranges to your subnets. In custom mode, you manually define the IP ranges for the subnets.
Configure advanced options if needed, such as custom routing, private Google forwarding, or shared segment.
Click “Create” to create the VPC.
After creating the VPC, you can add subnets, create firewall rules, connect virtual local area networks (VPN), and other network-related settings. Be sure to review and adjust your security and connectivity settings as needed to meet your specific needs.
provider "google" {
credentials = file("path/to/your/credentials.json")
project = "your-project-id"
region = "your-region"
}
resource "google_compute_network" "vpc" {
name = "your-vpc-name"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet" {
name = "your-subnet-name"
region = "your-region"
network = google_compute_network.vpc.name
ip_cidr_range = "10.0.0.0/24"
}
Be sure to replace the following values with your own:
“path/to/your/credentials.json”: The path to the Google Cloud JSON credentials file.
“your-project-id”: Your Google Cloud project ID.
“your-region”: The region where you want to create the VPC and subnet.
“your-vpc-name”: The name you want to give the VPC.
“your-subnet-name”: The name you want to give the subnet.
“10.0.0.0/24”: The IP range you want to assign to the subnet.
After saving the above code to a file with a .tf extension (for example, vpc.tf), run the following Terraform commands in the directory where the file is located:
terraform init
terraform plan
terraform apply
Have nice day!