https://www.cloudskillsboost.google
Select - DevOps Engineer, SRE Learning Path
Select - Getting Started with Terraform for Google Cloud
High Level Objectives
- Verify Terraform installation
- Define Google Cloud as the provider
- Create, change, and destroy Google Cloud resources by using Terraform
Skills
- terraform version
- Provider
- create,change,destroy
- terraform
Version Stack
Stack | Version |
---|---|
Terraform | 1.3.6 |
terraform --version
touch main.tf
- main.tf
terraform {
required_providers {
google = {
source = "hashicorp/google"
}
}
}
provider "google" {
region = "us-central1"
zone = "us-central1-c"
}
terraform init
- Add the following to main.tf
resource "google_compute_instance" "terraform" {
name = "terraform"
machine_type = "n1-standard-2"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
}
terraform plan
## The configuration fails with the following error. This is because you cannot configure a compute engine without a network.
- Now add the network by including the following code segment to the google_compute_instance block.
network_interface {
network = "default"
access_config {
}
}
terraform plan
terraform apply
- Validate on console
- Add a tags argument to the instance we just created so that it looks like this:
resource "google_compute_instance" "terraform" {
name = "terraform"
machine_type = "n1-standard-2"
tags = ["web", "dev"]
# ...
}
terraform plan
terraform apply
- Edit machine type
resource "google_compute_instance" "terraform" {
name = "terraform"
machine_type = "n1-standard-1"
tags = ["web", "dev"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {
}
}
allow_stopping_for_update = true
}
terraform plan
terraform apply
- Validate
terraform destroy