Skip to main content

Creating Resources on Google Cloud

10 min

In Pulumi, resources represent the fundamental units that make up your infrastructure, such as virtual machines, networks, storage, and databases. A resource is used to define and manage an infrastructure object in your Pulumi configuration. In this tutorial, you will create a simple static website hosted on an Google Cloud virtual instance. You will then refer to documentation in the Pulumi Registry to configure the storage account as a website.

What you'll learn
  • How to create a new resource
  • How to reference resource definitions in the Pulumi documentation
Prerequisites

Create a new project#

To start, login to the Pulumi CLI and ensure it is configured to use your Google Cloud account. Next, create a new project and stack.

Terminal window
# Example using Python
mkdir pulumi-tutorial-gcp
cd pulumi-tutorial-gcp
pulumi new gcp-python

Then use the following code snippet to scaffold your project with the required imports and overall program structure that you will fill in as you go along, making sure to replace the value of project with the name/ID of your own Google Cloud project:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const region = "us-central1";
const zone = "us-central1-a";
const project = "pulumi-devrel"; // REPLACE
const startupScript = `#!/bin/bash
echo "Hello, World!" > index.html
nohup python3 -m http.server 80 &`;
// Create a VPC network.
const vpcNetwork = new gcp.compute.Network("vpc-network", {
project: project,
autoCreateSubnetworks: true,
});
// Create an IP address.
const ipAddress = new gcp.compute.Address("ip-address", {
project: project,
region: region,
});
// Steps:
// [1] Create a compute instance.
// [2] Create and configure a firewall.
const instanceUrl = ipAddress.address.apply(address => `http://${address}`);
// Export the DNS name of the bucket
export const instanceURL = instanceUrl;

The above code creates the following on your behalf:

  • A VPC network
  • An IP address
  • An output of the web server’s URL

Create a virtual machine#

The first resource you will create will be virtual machine that will host the web server. The specific details of how to create your virtual machine differ by cloud provider. In the case of Google Cloud, you will be creating a virtual machine instance. The Pulumi Registry provides the documentation for all of the Pulumi providers and their associated resources. Open the gcp.compute.Instance documentation page to view a description of this resource, example usage, the resource definition, and supported properties. You will now define your instance resource as shown below:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const region = "us-central1";
const zone = "us-central1-a";
const project = "pulumi-devrel"; // REPLACE
const startupScript = `#!/bin/bash
echo "Hello, World!" > index.html
nohup python3 -m http.server 80 &`;
// Create a VPC network.
const vpcNetwork = new gcp.compute.Network("vpc-network", {
project: project,
autoCreateSubnetworks: true,
});
// Create an IP address.
const ipAddress = new gcp.compute.Address("ip-address", {
project: project,
region: region,
});
// [1] Create a compute instance.
const computeInstance = new gcp.compute.Instance("webserver-instance", {
machineType: "e2-micro",
project: project,
zone: zone,
metadataStartupScript: startupScript,
bootDisk: {
initializeParams: {
image: "debian-cloud/debian-12",
},
},
networkInterfaces: [
{
accessConfigs: [
{
natIp: ipAddress.address,
},
],
network: vpcNetwork.id,
},
],
serviceAccount: {
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
},
});
// [2] Create and configure a firewall.
// TO- DO
const instanceUrl = ipAddress.address.apply(address => `http://${address}`);
// Export the DNS name of the bucket
export const instanceURL = instanceUrl;

All resources have a required name argument. Each resource has both a logical name and a physical name. The logical name is how the resource is known inside Pulumi. This is the value provided to the required name argument. The physical name is the name used for the resource in the cloud provider that a Pulumi program is deploying to. It is a combination of the logical name plus a random suffix which helps to prevent resource naming collisions.

In the above example, the logical name for our instance resource is “website-server”, and the physical name might typically look something like “website-server-d7c2fa0”.

In addition to names, resources have properties and options.

Properties are used to specify what type of resource to create. Properties are often resource-specific, and they can be required or optional depending on the specifications of the provider.

Some of the properties inside of your gcp.compute.Instance resource include:

PropertyDescription
machine typetells the Google Cloud provider what type of machine to create
boot disktells the provider the configuration for the instance’s boot disk
network interfacestells the provider what networks to attach to the instance

Options let you control certain aspects of a resource (such as showing explicit dependencies or importing existing infrastructure). You do not have any options defined for this resource, but you can learn more about how it works in the Resource options documentation.

Deploy your resources#

Now run the pulumi up command to preview and deploy the resources you’ve just defined in your project.

Terminal window
pulumi up -y
Previewing update (web-server)
Type Name Plan
+ pulumi:pulumi:Stack gcp-web-server create
+ ├─ gcp:compute:Network vpc-network create
+ ├─ gcp:compute:Address ip-address create
+ └─ gcp:compute:Instance webserver-instance create
Outputs:
instanceURL: output<string>
Resources:
+ 4 to create
Updating (web-server)
Type Name Status
+ pulumi:pulumi:Stack gcp-web-server created (73s)
+ ├─ gcp:compute:Address ip-address created (3s)
+ ├─ gcp:compute:Network vpc-network created (53s)
+ └─ gcp:compute:Instance webserver-instance created (15s)
Outputs:
instanceURL: "http://34.171.149.220"
Resources:
+ 4 created
Duration: 1m15s

The public IP address of your instance has been provided for you as an output, and you can use this to access your web server. However, if you try to visit this address, your request will eventually time out. This is because you have not yet configured web traffic access for your instance. You will do this by creating your firewall resource.

Create a firewall#

In this section, you will use Pulumi documentation to configure the firewall on your own. The firewall must allow web traffic on port 80 in order for you to access your web server. Use the following steps as a guide for adding the firewall resource:

  • Navigate to the Google Cloud Registry documentation page
  • Search for the gcp.compute.Firewall resource
  • Define the firewall resource in your project code
  • Configure the firewall to allow traffic on port 80
  • Preview and deploy your updated project code

Once you have completed these steps, navigate to your instance IP address again. You should now be greeted with a “Hello world!” home page message that indicates your web server is running and publicly accessible.

View complete solution#

You can view the complete project code below:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const region = "us-central1";
const zone = "us-central1-a";
const project = "pulumi-devrel"; // REPLACE
const startupScript = `#!/bin/bash
echo "Hello, World!" > index.html
nohup python3 -m http.server 80 &`;
// Create a VPC network.
const vpcNetwork = new gcp.compute.Network("vpc-network", {
project: project,
autoCreateSubnetworks: true,
});
// Create an IP address.
const ipAddress = new gcp.compute.Address("ip-address", {
project: project,
region: region,
});
// [1] Create a compute instance.
const computeInstance = new gcp.compute.Instance("webserver-instance", {
machineType: "e2-micro",
project: project,
zone: zone,
metadataStartupScript: startupScript,
bootDisk: {
initializeParams: {
image: "debian-cloud/debian-12",
},
},
networkInterfaces: [
{
accessConfigs: [
{
natIp: ipAddress.address,
},
],
network: vpcNetwork.id,
},
],
serviceAccount: {
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
},
});
// [2] Create and configure a firewall.
const firewall = new gcp.compute.Firewall("firewall", {
project: project,
network: vpcNetwork.selfLink,
allows: [
{
protocol: "tcp",
ports: ["80"],
},
],
sourceRanges: ["0.0.0.0/0"],
});
const instanceUrl = ipAddress.address.apply(address => `http://${address}`);
// Export the DNS name of the bucket
export const instanceURL = instanceUrl;

Clean up#

Before moving on, tear down the resources that are part of your stack to avoid incurring any charges.

  1. Run pulumi destroy to tear down all resources. You’ll be prompted to make sure you really want to delete these resources. A destroy operation may take some time, since Pulumi waits for the resources to finish shutting down before it considers the destroy operation to be complete.
  2. To delete the stack itself, run pulumi stack rm. Note that this command deletes all deployment history from the Pulumi Service.

Next steps#

In this tutorial, you create a virtual machine instance configured as a web server, and you created a firewall resource configured for public web access by referencing the Pulumi Registry. You also reviewed resource properties and example usage of various resources.

To learn more about creating resources in Pulumi, take a look at the following resources:

Related

The infrastructure as code platform for any cloud.