
In this hands-on lab, we learn how and when to use init
, plan
, and apply
.
To do so, we will set the keys and configurations to use a Terraform instance. To do this, we must:
- Make sure the keys are set up to communicate with Terraform.
- Make sure the configuration files are initiated.
- Execute the config files to test our Terraform deployment.
Before We Begin
To get started, we need to log in to the GCP console using your credentials.
Create a Service Account
To get started, we need to create a service account:
- From Google Cloud console’s main navigation, choose IAM & Admin then Service Accounts.
- Click Create service account.
- Give your service account the name
terraform
. - Click Create.
- In the roles dropdown, select Project then Owner.
- Click Continue and then Done.
- On the Service accounts page, save the email provided for the one we just created.
terraform@terraform-ba-154-f56f96b1.iam.gserviceaccount.com

Ensure Terraform Is Installed on the Host Instance
With our service account created, we can continue:
- From Google Cloud navigation, choose Compute Engine then VM instances.

- Click SSH next to
terraform-test-instance
. - Use
root
privileges:sudo -i
- Change into the
root
directory:cd /
- List its contents:
ll
- Make sure you see both a
downloads
folder and aTerraform
file.

- 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
- Enter Y when prompted.
- Select the link that appears.
- Select the account provided for this lab.
- Select Allow.

- Copy the key that appears and then enter it into our terminal and hit Enter.

- Now, we need to create our key using:
gcloud iam service-accounts keys create /downloads/terraform.json --iam-account <SERVICE_ACCOUNT_EMAIL>

- Create a
main.tf
file:vi main.tf
- Paste the following configuration, replacing
<PROJECT_NAME>
with your project name (it can be found in the top navigation bar in Google Cloud console):
provider “google” {
version = “3.5.0”
credentials = file(“/downloads/terraform.json”)
project = “<PROJECT_NAME>”
region = “us-central1”
zone = “us-central1-c”
}
resource “google_compute_network” “vpc_network” {
name = “new-terraform-network”
}
- Save and exit the file by pressing Escape followed by
:
wq
.

Use init
, plan
, and apply
to Finish the Configuration
With everything set, it is time to finish our configuration:
- Initialize the configuration file:
terraform init

- Validate the configuration file:
terraform validate

- Create the execution plan:
terraform plan

- Apply the changes:
terraform apply
- When prompted, enter yes.

- Run terraform show command to get human readable output from a state or plan file.

- Go to the Google Cloud Platform console, and from the main navigation bar, under Networking, select VPC network.
- Find new-terraform-network to confirm that we have a new vpc network created.

Conclusion
Congratulations! You’ve complete this hands-on lab and created a vpc network using terraform !!
Thanks for shening. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/en/register-person?ref=P9L9FQKY
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.