Modern Infrastructure as Code for Google Cloud

Pulumi's infrastructure as code SDK helps create, deploy, and manage GCP containers, serverless functions, and infrastructure using real programming languages.

Find out how to program the cloud with Pulumi and Google Cloud.

import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
import { k8sProvider, k8sConfig } from "./cluster";

const name = pulumi.getProject() + "-" + pulumi.getStack();
const canaryLabels = { app: "canary-" + name };
const canary = new k8s.apps.v1beta1.Deployment("canary", {
    spec: {
        selector: { matchLabels: canaryLabels },
        replicas: 1,
        template: {
            metadata: { labels: canaryLabels },
            spec: { containers: [{ name, image: "nginx" }] },
        },
    },
}, { provider: k8sProvider });

export let kubeConfig = k8sConfig;

Benefits of Pulumi

Real Code

Pulumi is infrastructure as real code. This means you get all the benefits of your favorite language and tool for provisioning cloud infrastructure: code completion, error checking, versioning, IDE support, and general productivity gains — without the need to manage YAML and DSL syntax.

Reusable Components

As Pulumi is code, you can build up a library of packages to further enhance efficiency. Build repeatable practices through versioned packages such as: standard policies, network best practices, architecture blueprints — and deploy them to your team.

Immutable Infrastructure

Pulumi provides the computation of necessary cloud resources with a 'Cloud Resource DAG' ensuring successful deployment of cloud infrastructure — efficiently building, updating, and destroying cloud resources as required.

Build and deploy a GKE cluster

Pulumi supports programming against Kubernetes – Minikube, custom on-premises, or cloud-hosted custom clusters or in managed clusters such as Google GKE.

This code defines a GKE cluster with configurable settings which could be used in a module which could then be used to deploy an app to the cluster.

Get Started

Pulumi can be used on any resource covering serverless, containers, and infrastructure using JavaScript, TypeScript, Python, and Go.

// Define a GKE cluster with configurable settings
import * as gcp from "@pulumi/gcp";
import { nodeCount, nodeMachineType, password, username } from "./config";

export const k8sCluster = new gcp.container.Cluster("gke-cluster", {
    initialNodeCount: nodeCount,
    nodeVersion: "latest",
    minMasterVersion: "latest",
    masterAuth: { username, password },
    nodeConfig: {
        machineType: nodeMachineType,
        oauthScopes: [
            "https://www.googleapis.com/auth/compute",
            "https://www.googleapis.com/auth/devstorage.read_only",
            "https://www.googleapis.com/auth/logging.write",
            "https://www.googleapis.com/auth/monitoring"
        ],
    },
});

Build and deploy Kubernetes apps to GKE

Pulumi supports programming against Azure's ACI container orchestrator. Pulumi is entirely unopinionated about how containers are built, published, and deployed to your orchestrator.

This code makes use of our previously defined GKE cluster and deploys an nginx canary onto the cluster with 1 replica.

Get Started

Pulumi can be used on any resource covering serverless, containers, and infrastructure using JavaScript, TypeScript, Python, and Go.

// Deploy an nginx canary to GKE
import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
import { k8sProvider, k8sConfig } from "./cluster";

// Create a canary deployment to test that this cluster works.
const name = pulumi.getProject() + "-" + pulumi.getStack();
const canaryLabels = { app: "canary-" + name };
const canary = new k8s.apps.v1beta1.Deployment("canary", {
    spec: {
        selector: { matchLabels: canaryLabels },
        replicas: 1,
        template: {
            metadata: { labels: canaryLabels },
            spec: { containers: [{ name, image: "nginx" }] },
        },
    },
}, { provider: k8sProvider });

// Export the Kubeconfig so that clients can easily access our cluster.
export let kubeConfig = k8sConfig;

Creating a Simple Web Server

Pulumi gives you a way to express infrastructure configuration using your favorite programming language.

This code uses TypeScript on Node.js to define the necessary environment, defines a very simple web server, and then creates the instance, before exporting the IP and Hostname.

Get Started

Pulumi can be used on any resource covering serverless, containers, and infrastructure using JavaScript, TypeScript, Python, and Go.

const gcp = require("@pulumi/gcp");

const computeNetwork = new gcp.compute.Network("network", {
    autoCreateSubnetworks: true,
});

const computeFirewall = new gcp.compute.Firewall("firewall", {
    network: computeNetwork.selfLink,
    allows: [{
        protocol: "tcp",
        ports: [ "22", "80" ],
    }],
});

// Create a simple web server.
const startupScript =
    "#!/bin/bash \n" +
    "echo 'Hello, World!' > index.html \n" +
    "nohup python -m SimpleHTTPServer 80 &";

const computeInstance = new gcp.compute.Instance("instance", {
    machineType: "f1-micro",
    metadataStartupScript: startupScript,
    bootDisk: {
        initializeParams: {
            image: "debian-cloud/debian-8",
        },
    },
    networkInterfaces: [{
        network: computeNetwork.id,
        accessConfigs: [{}], // must be empty
    }],
    serviceAccount: {
      scopes: ["https://www.googleapis.com/auth/cloud-platform"],
    },
}, { dependsOn: [computeFirewall] });

exports.instanceName = computeInstance.name;
exports.instanceIP = computeInstance.networkInterfaces.apply(ni => ni[0].accessConfigs[0].natIp);

Creating a Google Cloud Function

With Pulumi, you can combine infrastructure definitions and application code in one program. The @pulumi/cloud-gcp library is a set of Pulumi components that provide a higher-level abstraction over Azure. You can use this library and define application code at the same time as the infrastructure it depends on.

This example shows how to create a simple Google Cloud Function that returns a message when invoked.

Get Started

Pulumi can be used on any resource covering serverless, containers, and infrastructure using JavaScript, TypeScript, Python, and Go.

// Create a simple Function
import * as gcpFunction from "./gcpFunction";

let greetingFunction = new gcpFunction.HttpFunction("greeting", (req, res) => {
    res.send("Greetings from " + (req.body.name || "Google Cloud Functions") + "!");
});

export let url = greetingFunction.httpsTriggerUrl;

How Pulumi Works

How Pulumi works
Featured Customer

Learning Machine

Learning Machine, a blockchain SaaS company faced two challenges with their cloud infrastructure:

  • Skills gaps between Dev and DevOps creating silos, and fragility.
  • The need to more rapidly provision their expanding roster of new customers.

By moving to Pulumi, Learning Machine were able to solve both challenges with significant increases in capability:

25,000 Lines of CloudFormation reduced to 500 Lines of JavaScript
New customer provisioning time reduced from 3 weeks to 1 hour

Pulumi has given our team the tools and framework to achieve a unified development and DevOps model, boosting productivity and taking our business to any cloud environment that our customers need. We retired 25,000 lines of complex code that few team members understood and replaced it with 100s of lines in a real programming language.

— Kim Hamilton, CTO Learning Machine

Get Started with Pulumi

Pulumi works with your favorite language, and can be used with any cloud.

1 Install Pulumi
2 Try Our Tutorials
3 Read the Docs

Let Pulumi assist with your cloud infrastructure.

Need help with cloud-native infrastructure as code on GCP? Drop us a line.