
Overview
Learning how to configure complex environments with Terraform is a must-have skill. In this hands-on lab, we will provision an autoscaling group with a load balancer.
Objectives: Successfully complete this lab by achieving the following learning objectives:
Create a Service Account
- From Google Cloud console’s main navigation, choose IAM & Admin > Service Accounts.
- Click Create service account.
- Give your service account a name.
- Click Create.
- In the roles dropdown, select Project > Owner.
- Click Continue and then Done.

Log in to the Host Instance and Ensure Terraform Is Installed
- From Google Cloud navigation, choose Compute Engine > VM instances.

- Click SSH next to
terraform-instance
. - Use
root
privileges:
sudo -i
- Change into the
root
directory:
cd /
- 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
at the prompt. - Click on the link in the output.
- Select the Cloud Student account.
- Click Allow.
- Copy the code provided.
- Paste the code into the terminal.

- Create the service account key:
gcloud iam service-accounts keys create /downloads/auto-scaling.json --iam-account <SERVICE_ACCOUNT_EMAIL>
In our case:
gcloud iam service-accounts keys create /downloads/auto-scaling.json --iam-account auto-scaling@using-terraf-159-400aac65.iam.gserviceaccount.com

Create and Deploy the Configuration File
- Create a
main.tf
file:
vim main.tf
- Paste in the following, replacing all instances of
<PROJECT_NAME>
with your project name, which can be found in the top navigation bar of the Google Cloud console:
provider "google" {
version = "3.20.0"
credentials = file("/downloads/auto-scaling.json")
project = "<PROJECT_NAME>"
region = "us-central1"
zone = "us-central1-c"
}
resource "google_compute_network" "vpc_network" {
name = "new-terraform-network"
}
resource "google_compute_autoscaler" "foobar" {
name = "my-autoscaler"
project = "<PROJECT_NAME>"
zone = "us-central1-c"
target = google_compute_instance_group_manager.foobar.self_link
autoscaling_policy {
max_replicas = 5
min_replicas = 2
cooldown_period = 60
cpu_utilization {
target = 0.5
}
}
}
resource "google_compute_instance_template" "foobar" {
name = "my-instance-template"
machine_type = "n1-standard-1"
can_ip_forward = false
project = "<PROJECT_NAME>"
tags = ["foo", "bar", "allow-lb-service"]
disk {
source_image = data.google_compute_image.centos_7.self_link
}
network_interface {
network = google_compute_network.vpc_network.name
}
metadata = {
foo = "bar"
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
resource "google_compute_target_pool" "foobar" {
name = "my-target-pool"
project = "<PROJECT_NAME>"
region = "us-central1"
}
resource "google_compute_instance_group_manager" "foobar" {
name = "my-igm"
zone = "us-central1-c"
project = "<PROJECT_NAME>"
version {
instance_template = google_compute_instance_template.foobar.self_link
name = "primary"
}
target_pools = [google_compute_target_pool.foobar.self_link]
base_instance_name = "terraform"
}
data "google_compute_image" "centos_7" {
family = "centos-7"
project = "centos-cloud"
}
module "lb" {
source = "GoogleCloudPlatform/lb/google"
version = "2.2.0"
region = "us-central1"
name = "load-balancer"
service_port = 80
target_tags = ["my-target-pool"]
network = google_compute_network.vpc_network.name
}
- Run below command to replace the project Name:
sed -i 's/<PROJECT_NAME>/using-terraf-159-400aac65/g' main.tf

- Initialize the configuration file:
terraform init

- Validate the configuration file:
terraform validate

- Create the execution plan:
terraform plan

- Apply the changes:
terraform apply


- Enter
yes
at the prompt. It will take a few minutes to complete. - In the Google Cloud console, refresh the VM instances page. We should now see our two new instances listed.

- Navigate to VPC network > VPC networks. We should see our
new-terrafor
m-network
listed.

- Navigate to Network services > Load balancing. We should see our two new load balancers listed.

The point of view of your article has taught me a lot, and I already know how to improve the paper on gate.oi, thank you. https://www.gate.io/de/signup/XwNAU
I have read your article carefully and I agree with you very much. This has provided a great help for my thesis writing, and I will seriously improve it. However, I don’t know much about a certain place. Can you help me? https://www.gate.io/de/signup/XwNAU
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.com/lv/register?ref=OMM3XK51