Compute Engine
In this section you will create the Compute Engine to run an application, with the related VPC network and a Service Account.
A VPC, or Virtual Private Cloud, is a network block all of your own. The Service Account is an account that is used to run the Compute Engine virtual machine. It could have a separate set of IAM rules, which tell what it can access. Custom Service Accounts should be used instead of the default ones according to Google's recommendations, so you have better control over the privileges.
In this exercise, you will learn how to create multiple resources and have them reference each other.
Exercises: VPC and Service Account
- Create a
resourceblock with the typegoogle_compute_networknameddefaultwith the following parameters:name=${var.name}-vpc-networkauto_create_subnetworks=true- Remember to specify the project
- Create a
resourceblock with the typegoogle_service_accountnameddefaultwith the following parameters:account_id=${var.name}-service-accountdisplay_name=${var.name}-service-account- Remember to specify the project
Help
terraform/compute_engine.tf
resource "google_compute_network" "default" {
name = "${var.name}-vpc-network"
project = var.project
auto_create_subnetworks = true
}
terraform/compute_engine.tf
resource "google_service_account" "default" {
account_id = "${var.name}-service-account"
display_name = "${var.name} Service Account"
project = var.project
}
Extra
- Instead of using
auto_create_subnetworks=true, create a separate custom subnet- The resource is
google_compute_subnetwork, the instruction will help you select aip_cidr_range - You will need to also specify the
subnetworkin thenetwork_interfaceblock below for the Compute Engine instance
- The resource is
Exercises: Compute Engine Virtual Machine
- Create a
resourceblock with typegoogle_compute_instancenameddefaultwith following parametersname="${var.name}-instance"machine_type="e2-micro"zone=var.zone- Remember to specify the project
tags=["http", "ssh"]metadata_startup_script="sudo apt update && sudo apt -y install apache2"- For the
boot_diskimageconfiguration, usedebian-cloud/debian-11 - For the
network_interfacereference the VPC you have created- Hint: You will need the
self_linkof the resource - The
access_configblock can be left empty
- Hint: You will need the
- For the
service_accountemailconfiguration reference theemailof the Service Account you createdscopes=["cloud-platform"]
Extra
- Move the
metadata_startup_scriptto a separate file calledstartup.sh- You can then use Terraform's functions to read the file from disk
- Add an output to show the assigned public IP address of the created Compute Engine instance
Help
terraform/compute_engine.tf
resource "google_compute_instance" "default" {
name = "${var.name}-instance"
machine_type = "e2-micro"
zone = var.zone
project = var.project
tags = ["http", "ssh"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = google_compute_network.default.self_link
access_config {}
}
metadata_startup_script = "sudo apt update && sudo apt -y install apache2"
service_account {
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
Check your configuration
To check your Terraform configuration run:
terraform plan
What changed? Does everything look OK?
Apply your configuration
Run terraform apply, to make changes to the infrastructure:
terraform apply
After the apply, the VM is up and running, hopefully serving our HTTP server Apache2 and SSH.
But is it accessible? If you added the output, you can try to do that either with the browser, or SSH.
Don't worry if you didn't, it's not yet accessible because we haven't created the firewall rules. Let's do that next!
Answer
terraform/compute_engine.tf
resource "google_compute_network" "default" {
name = "${var.name}-vpc-network"
auto_create_subnetworks = true
}
resource "google_service_account" "default" {
account_id = "${var.name}-service-account"
display_name = "${var.name} Service Account"
}
resource "google_compute_instance" "default" {
name = "${var.name}-instance"
machine_type = "e2-micro"
zone = var.zone
tags = ["http", "ssh"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = google_compute_network.default.self_link
access_config {}
}
metadata_startup_script = "sudo apt update && sudo apt -y install apache2"
service_account {
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
Next
Next, we set up some firewall rules for the Compute Engine instance.