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:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set gcp-py-functionscd pulumi-examples/gcp-py-functionsThis 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#
Deploying the example#
-
Create a new stack:
Terminal window pulumi stack init dev -
Configure the GCP project and region:
Terminal window pulumi config set gcp:project <projectname>pulumi config set gcp:region <region> -
Install dependencies:
Terminal window python3 -m venv venvsource venv/bin/activatepip install -r requirements.txt -
Run
pulumi upto preview and deploy changes:Terminal window pulumi upResources:+ 4 createdDuration: 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 as
fxn_url:Terminal window pulumi stack output fxn_urlhttps://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:
-
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.
-
Update the stack’s configuration, encrypting the API key value:
Terminal window pulumi config set googleMapsApiKey <INSERT_API_KEY> --secret -
Set the target destination to compute directions to:
Terminal window pulumi config set destination <DESTINATION> -
(Optional) Add a travel time offset, e.g. add 5 minutes to the estimate.
Terminal window pulumi config set travelOffset <TRAVEL_OFFSET> -
Run
pulumi upto 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:
-
Log into your Twilio account, and create a new access token and/or phone number to send SMS messages from.
-
Add the Twilio configuration data to your Pulumi stack:
Terminal window pulumi config set twillioAccessToken <TWILIO_ACCESS_TOKEN> --secretpulumi config set twillioAccountSid <TWILIO_ACCOUNT_SID> --secretpulumi config set fromPhoneNumber <FROM_PHONE_NUMBER> -
Enter the phone number the Cloud Function will send messages to:
Terminal window pulumi config set toPhoneNumber <TO_PHONE_NUMBER> --secret -
Run
pulumi upto 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.
-
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.
-
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 frompulumi stack output fxn_url):<FUNCTION_URL>?long={{Longitude}}&lat={{Latitude}}
Cleaning up#
Once you’re finished experimenting, destroy your stack and remove it:
pulumi destroypulumi stack rm