This content originally appeared on DEV Community and was authored by Daberechi
I’m exited to share with you a comprehensive guide on deploying compute engine, network and Subnet on GCP using terraform. This guide will walk you through the process of setting up robust infrastructure on GCP using terraform’s infrastructure-as-a-code (Iac) approach.
Prerequisite
Before we dive in, make sure you have:
- A GCP account with billing enabled
- Terraform installed on your machine
Step 1: Create a file
- Create a file on your laptop, name it terraform project
- Right click on the file and open it with VS code.
- Create a provider.tf file once you open your VS code
Step 2: Deployment
- Go to your browser and search for terraform provider
- Click on GCP (because we are deploying the resources to GCP)
- Click on use provider
- Then copy provider command
- Go to your VS code, click on the provider.tf file and paste the provider command you copied
Step 3: Creating a system user for terraform on GCP
- Go to your GCP dashboard
- Click on the 3 lines by your left
- Put your cursor on IAM & admin, and click on service account
- Click on create service account at the top
- Let’s name it terraform automation
- Scroll down and click create
- Click select a role under grant this service account
- Put your cursor on basic and select owner
- Click on continue
- Click done
- After creating it, click on it
- Click on key at your top
- Click add key, and click create new key
- Choose Json and click create The private key will be downloaded to your laptop
- Go to your downloads in your laptop
- Locate the private key and right click on it, and copy it
- Open that terraform project file you created on your laptop and paste the private key you copied
- Go to your VS code, you will see the key
- Right click on the key and rename it to something short (e.g gcp-key)
- Click on your provider.tf file
- In line 10, type provider “google”{
- In line 11, type #configuration options
- Line 12, type project =”go to your google dashboard, click on project at the top, copy the project id and paste it here”
- Line 13, type region =”paste your region”
- Line 14, type credentials =file(“gcp-key.json”) -Line 15, put}
save
Go to your terraform provider on your browser
Click on documentation
-Search google compute networkCopy the code under network custom MTU
- Go to your VS code, create a vpc.tf file and paste the command you copied
- Change my-project name to anything you want (e.g gcp project)
- Where you see vpc-network ( front of name) name it to whatever you want or leave it
- Change true to false (because this is a customized VPC)
- save
Step 4: Subnet Creation
- Create a file in your VS code and name it subnet.tf
- Go to your terraform provider documentation
- Search google compute subnet
- Copy the command and paste it in the subnet.tf
- Delete from resource “google_compute_network” to false}
- Where you see “test-subnetwork” name it anything or leave it default
- Change the region to your region
- Change custom-test to vpc_network.id or anything
- save
Step 5: Firewall Creation
- Create a file in your VS code and name it firewall.tf
- Go to your terraform provider documentation
- Search for google compute firewall
- Copy the command and paste it in firewall.tf
- Where you see default, in front of network=, change it to vpc_network.name
- Where you see 8080, change it to 22, leave 80, remove 1000-2000 and replace it with 443
- Where you see test-firewall, change it to frontend firewall or anything
- Where you see “web”, change it to Frontend
- Save
Step 6: Create Virtual Machine
- Create a file, name it instance.tf
- Go to your terraform provider documentation
- Search google compute instance
- Copy the command and paste it in instance.tf
- Where you see “default” in the first line, change it to “web-app” or anything you want
- Where you see “my-custom-sa” in front of account_id, change it to “web-sa”
- Where you see “my-instance” in line 7, change it “frontend-web-app” or anything you want
- Zone in line 9, put your zone
- Line 11, (tags), change those things to (“frontend”)
- Line 28 (network), remove default and put google-compute-network.vpc_network
- In line 36 (foo), remove “bar” and replace it with “web”
- Line 39, you can remove the “echo hi>/test txt” and put “sudo apt install nginx” or leave it
- Line 43, remove default, and replace it with the terraform system account id/
save
Type terraform validate (to check for any possible error)
Type terraform plan
Type apply –auto-approve or terraform apply
When you are done with your deployment, type terraform destroy
Conclusion
In this guide, we have successfully deployed a Compute Engine Instance, Network and Subnet on GCP using terraform. You can now use this infrastructure as a starting point for your project and scale it as needed.
See you soon…
This content originally appeared on DEV Community and was authored by Daberechi