Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Google Cloud network and instance with ComponentResource

A Google Cloud Python Pulumi program using ComponentResource

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set gcp-py-network-component
cd pulumi-examples/gcp-py-network-component

This example uses pulumi.ComponentResource as described here to create a Google Cloud network and instance.

The use of pulumi.ComponentResource demonstrates how multiple low-level resources can be composed into a higher-level, reusable abstraction.

Prerequisites#

  1. Install Pulumi
  2. Configure GCP credentials
  3. Install Python

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set the Google Cloud project and region:

    Terminal window
    pulumi config set gcp:project proj-123456
    pulumi config set gcp:region us-central1
  3. Configure one or more subnetwork CIDRs for the program to use. Note: This example uses structured configuration as per Structured Configuration:

    Terminal window
    pulumi config set --path 'subnet_cidr_blocks[0]' 172.1.0.0/16
    pulumi config set --path 'subnet_cidr_blocks[1]' 172.2.0.0/16
  4. Install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  5. Run pulumi up to preview and deploy the changes:

    Terminal window
    pulumi up
    Updating (dev):
    Type Name Status
    + pulumi:pulumi:Stack gcp-py-network-component-dev created
    + ├─ my:modules:Vpc demo created
    + │ └─ gcp:compute:Network demo created
    + │ ├─ gcp:compute:Subnetwork demo-0 created
    + │ ├─ gcp:compute:Subnetwork demo-1 created
    + │ ├─ gcp:compute:Router demo created
    + │ └─ gcp:compute:RouterNat demo created
    + └─ my:modules:Instance demo created
    + ├─ gcp:compute:Address demo-nginx created
    + ├─ gcp:compute:Firewall demo-nginx created
    + └─ gcp:compute:Instance demo-nginx created
    Outputs:
    network : "demo-7a734d7"
    public_ip: "34.66.58.210"
    Resources:
    + 11 created
    Duration: 46s
  6. Get the IP address of the newly-created instance from the stack’s outputs:

    Terminal window
    pulumi stack output nginx_public_ip
    13.64.196.146
  7. Check to see that your server is now running:

    Terminal window
    curl http://$(pulumi stack output nginx_public_ip)
    Powered by Pulumi!

Cleaning up#

Once you’re finished experimenting, destroy your stack and remove it:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.