Skip to main content

Google Cloud Functions in Python

A minimal Python Pulumi program using Google Cloud Functions.

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-functions
cd pulumi-examples/gcp-py-functions

This example shows how to deploy a Python-based Google Cloud Function.

The deployed Cloud Function allows you to notify a friend via SMS about how long it will take to arrive at a location. This uses the Google Maps API and Twilio, and also benefits from using a Flic button and IFTTT. But none of that is necessary to use Pulumi to provision the Google Cloud Platform functions.

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. Configure the GCP project and region:

    Terminal window
    pulumi config set gcp:project <projectname>
    pulumi config set gcp:region <region>
  3. Install dependencies:

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

    Terminal window
    pulumi up
    Resources:
    + 4 created
    Duration: 1m2s
  5. Once the application is deployed, you can start accessing the Google Cloud Function by making an HTTP request to the function’s endpoint. It is exported from the stack as fxn_url:

    Terminal window
    pulumi stack output fxn_url
    https://us-central1-pulumi-gcp-dev.cloudfunctions.net/eta_demo_function...

    You can specify a starting location via latitude and longitude coordinates via URL query parameters. (You can find your current location on https://www.latlong.net/.)

    Terminal window
    curl "$(pulumi stack output fxn_url)?lat=<YOUR_LATITUDE>&long=<YOUR_LONGITUDE>"
    Sent text message to...

Configuration#

Google Maps for travel time#

The application uses the Google Maps API to estimate travel time data. To set this up:

  1. Get a Google Maps API key by clicking ‘Get started’.

    • Check the Routes and then click continue.
    • Select the GCP project you are deploying your Cloud Function to.
  2. Update the stack’s configuration, encrypting the API key value:

    Terminal window
    pulumi config set googleMapsApiKey <INSERT_API_KEY> --secret
  3. Set the target destination to compute directions to:

    Terminal window
    pulumi config set destination <DESTINATION>
  4. (Optional) Add a travel time offset, e.g. add 5 minutes to the estimate.

    Terminal window
    pulumi config set travelOffset <TRAVEL_OFFSET>
  5. Run pulumi up to re-deploy your Cloud Function with the new configuration.

Twilio for SMS notifications#

To have the Cloud Function send a text message, you’ll need a Twilio key too:

  1. Log into your Twilio account, and create a new access token and/or phone number to send SMS messages from.

  2. Add the Twilio configuration data to your Pulumi stack:

    Terminal window
    pulumi config set twillioAccessToken <TWILIO_ACCESS_TOKEN> --secret
    pulumi config set twillioAccountSid <TWILIO_ACCOUNT_SID> --secret
    pulumi config set fromPhoneNumber <FROM_PHONE_NUMBER>
  3. Enter the phone number the Cloud Function will send messages to:

    Terminal window
    pulumi config set toPhoneNumber <TO_PHONE_NUMBER> --secret
  4. Run pulumi up to re-deploy your Cloud Function with the new configuration.

Flic button to trigger the Cloud Function#

With Pulumi having set up the cloud infrastructure, the next step is to have a simple way to trigger it. With Flic you can trigger the Cloud Function with literally the push of a button.

To make sure to include the button presser’s location, you can use IFTTT.

  1. Install the Flic app on your phone and pair your button. Enable location services for the Flic app and add an IFTTT for one of the click gestures.

  2. Create a new Applet on IFTTT: “If You click a Flic, then Make a web request”

    • For “If” select the “Flic” service then “Flic is clicked”.
    • Select your Flic button and the appropriate gesture from the menu.
    • For “Then” select the “Make a web request” service
    • Under URL enter following (replace <FUNCTION_URL> with the value from pulumi stack output fxn_url): <FUNCTION_URL>?long={{Longitude}}&lat={{Latitude}}

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.