This is a demo of using Terraform to deploy multiple Cloud Functions and other related resources. It includes:
- storage bucket to hold zipped code for Cloud Functions
- storage bucket to use as trigger for Cloud Function
- PubSub topic to use as trigger for Cloud Function
- Cloud Function using bucket trigger to publish a PubSub message
- Cloud Function using pubsub trigger to post a message to Slack
- Create a file "terraform.tfvars" in the root folder
- Add the following variables
project-name
= name of GCP project for relevant resourcesgcp-region
= GCP region, e.g. us-central1creds-file
= file containing GCP credentials for creating resources (typically .json)slack-url
= Slack webhook URL for posting messages, e.g. "https://hooks.slack.com/services/XXXXXX/YYYYYY/ZZZZZZZZ"
- Change the names and descriptions of the GCP resources.
- From the command-line, type
terraform init
- Type
terraform plan
to confirm the setup - Type
terraform apply
to deploy the resources
Both of the Cloud Functions here use Go.
This function responds to new files uploaded in the trigger bucket. By default, it does not process the file. Additional logic could be added to do so. If the environment variable TOPIC_ID is set, it will send a message on the topic. The SendMessage function shows an example of adding arbitrary attributes to the message, which can be useful to subscribers.
This function responds to the pubsub topic "demo-topic." It will read the "status" attribute from the message, if available, and generate an emoji to accompany the message text. This message will be sent to the Slack webhook endpoint.