First steps in Pulumi with Python & Google Cloud Platform
By: Karol Zlot
Tags: Infrastructure As Code, Python, Pulumi, Google Cloud Platform, Terraform

Hi, I just started learning Pulumi (alternative to Terraform). I like the idea behind it, but things are not always obvious, especially because my setup is not the most common. I will try to use Pulumi to set up Python apps on Google Cloud Platform and using Python as Pulumi configuration language.

I will document here the process of doing things.

First things which I assume you did already:

Make a new Google Cloud Project. (It seems Pulumi can create a new project, but for the beginning let’s do it manually.) Install pulumi (https://www.pulumi.com/docs/get-started/gcp/begin/) Configure Pulumi to access your Google Cloud account (also in a link above)

The next step is to make a new empty folder for your new project. Let’s name it `quickstart`.

mkdir quickstart && cd quickstart

Then inside this folder let’s init Pulumi project:

pulumi new gcp-python

Pulumi will ask about a few things:

project name: quickstart (Name of folder usually, it's up to you. You can't use the same project name for two projects, Pulumi won't allow this.) project description: (You can leave this with default, it's only to help you.) stack name: (just press "Enter" to leave it as default "dev", more information about it you can find in Pulumi documentation) gcp:project: The Google Cloud project to deploy into: (type your existing google-project name)

Then you can see __main__.py file, it contains “Hello World” configuration. It will just create a bucket in Google Cloud Storage.

"""A Google Cloud Python Pulumi program""" import pulumi

from pulumi_gcp import storage # Create a GCP resource (Storage Bucket)

bucket = storage.Bucket('my-bucket') # Export the DNS name of the bucket

pulumi.export('bucket_name', bucket.url)

Currently, nothing is created in Cloud. Let’s apply this configuration to create a bucket:

pulumi up

Select “yes” and press Enter. Changes should be successfully applied:

If changes are not applied successfully in your case, then I suggest not continue more advanced steps, but first, try to make this example work. You can for example create a new project or check if the billing account is associated with a project correctly.

If you are successful, then let’s check Google Cloud Console if Storage bucket was created:

Oh yes, it was indeed created :)

Now let’s delete it.

One way to do it is to comment out bucket definition lines in the configuration file, so it will look like this:

"""A Google Cloud Python Pulumi program""" import pulumi

from pulumi_gcp import storage # Create a GCP resource (Storage Bucket)

# bucket = storage.Bucket('my-bucket') # Export the DNS name of the bucket

# pulumi.export('bucket_name', bucket.url)

And now just apply changes by using pulumi up again:

Select “yes” and press Enter and you should see:

Now let’s check again Google Cloud Console:

It’s empty :)

Tip: you can also use pulumi destroy if you want to remove all things deployed by Pulumi.

I hope this article will make the beginning a little easier.

Leave a comment if something isn’t working for you or if you want to suggest how I can improve this article.