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
resource
block with the typegoogle_compute_network
nameddefault
with the following parameters:name
=${var.name}-vpc-network
auto_create_subnetworks
=true
- Remember to specify the project
- Create a
resource
block with the typegoogle_service_account
nameddefault
with the following parameters:account_id
=${var.name}-service-account
display_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
subnetwork
in thenetwork_interface
block below for the Compute Engine instance
- The resource is
Exercises: Compute Engine Virtual Machine
- Create a
resource
block with typegoogle_compute_instance
nameddefault
with 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_disk
image
configuration, usedebian-cloud/debian-11
- For the
network_interface
reference the VPC you have created- Hint: You will need the
self_link
of the resource - The
access_config
block can be left empty
- Hint: You will need the
- For the
service_account
email
configuration reference theemail
of the Service Account you createdscopes
=["cloud-platform"]
Extra
- Move the
metadata_startup_script
to 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.