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 resources.
-
Create a new stack:
pulumi stack init gcp-py-functions
-
Configure GCP project and region:
pulumi config set gcp:project <projectname> pulumi config set gcp:region <region>
-
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
-
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...
The application uses the Google Maps API to estimate travel time. 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:
pulumi config set googleMapsApiKey <INSERT_API_KEY> --secret
-
Set the target destination to compute directions to:
pulumi config set destination <DESTINATION>
-
(Optional) Add a travel time offset, e.g. add 5 minutes to the estimate.
pulumi config set travelOffset <TRAVEL_OFFSET>
-
Run
pulumi up
to re-deploy your cloud function with the new configuration.
To have the Cloud Function send a text message, you'll need to 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:
pulumi config set twillioAccessToken <TWILIO_ACCESS_TOKEN> --secret pulumi config set twillioAccountSid <TWILIO_ACCOUNT_SID> --secret pulumi config set fromPhoneNumber <FROM_PHONE_NUMBER>
-
Enter the phone number the Cloud Function will send messages to:
pulumi config set toPhoneNumber <TO_PHONE_NUMBER> --secret
-
Run
pulumi up
to re-deploy your cloud function with the new configuration.
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.
-
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}}