Google Cloud Functions in Python

View Code Deploy

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.

Creating the Stack

  1. Create a new stack:

    pulumi stack init gcp-py-functions
  2. Configure GCP project and region:

    pulumi config set gcp:project <projectname>
    pulumi config set gcp:region <region>
  3. Create a Python virtualenv, activate it, and install dependencies:

    This installs the dependent packages needed for our Pulumi program.

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

    $ pulumi up
    Previewing changes:
    ...
    
    Performing changes:
    ...
    Resources:
        + 4 created
    Duration: 1m2s

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’s as fxn_url.

$ 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/.)

$ 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:

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

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

    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 to 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:

    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:

    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 setup 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}}

Get started on Google Cloud with Pulumi today.