Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Google Cloud HCL Starter

A minimal Google Cloud Pulumi HCL program

Before you begin#

For this walkthrough, you'll need:

Create a new project#

This starter creates a Google Cloud Storage bucket in the US multi-region and exports the bucket’s gs:// URL.

Terminal window
mkdir my-project; cd my-project
pulumi new gcp-hcl

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

It also asks for the template’s setting, gcp:project. It has no default, so enter the ID of the Google Cloud project to deploy into:

KeyDescriptionDefault
gcp:projectThe Google Cloud project to deploy into—
You can change values like these later with pulumi config set :
Terminal window
pulumi config set gcp:project <value>

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • main.tf contains the program code that declares your cloud resources.

Take a moment to open them up and have a look. Here’s what’s in main.tf:

main.tf
terraform {
required_providers {
gcp = {
source = "pulumi/gcp"
}
}
}
# Create a GCP resource (Storage Bucket)
resource "gcp_storage_bucket" "my-bucket" {
location = "US"
}
# Export the bucket's gs:// URL
output "bucket_name" {
value = gcp_storage_bucket.my-bucket.url
}

Deploy the project#

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

Pulumi adds a random suffix to most resource names, so my-bucket becomes something like my-bucket-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

Terminal window
pulumi up

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the bucket’s gs:// URL. Read it any time with pulumi stack output <name>, or pass it to another command. This one prints the bucket’s details, including its name and location:

Terminal window
gcloud storage buckets describe $(pulumi stack output bucket_name)
Need help?

If pulumi up reports failed to load application credentials, sign in again:

Terminal window
gcloud auth application-default login

Still stuck? Let us know.

Customize the project#

Put a file in the bucket. In main.tf, add this below the bucket:

resource "gcp_storage_bucket_object" "hello" {
bucket = gcp_storage_bucket.my-bucket.name
name = "hello.txt"
content = "Hello, Pulumi!"
}

Deploy again. The preview shows one object to create:

Terminal window
pulumi up

Then read the file back from the bucket. It prints Hello, Pulumi!:

Terminal window
gcloud storage cat "$(pulumi stack output bucket_name)/hello.txt"

Cleaning up#

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure platform for humans and agents.