This Google Cloud Functions function expects the Pub/Sub topic to receive Cloud Logging events based on the following filter, upon which SendGrid is leveraged to send an email notification:
logName:organizations/[ORGANIZATION_ID]/logs/ AND resource.type=project AND protoPayload.methodName=CreateProject
Disclaimer: This is not an officially supported Google project.
- Sign up for SendGrid Email API in the GCP Marketplace.
- Enable API:
gcloud services enable pubsub.googleapis.com
- Create Topic:
gcloud pubsub topics create createProjectsTopic
- Create sink:
gcloud logging sinks create createProjectsSink pubsub.googleapis.com/projects/[PROJECT_NAME]/topics/createProjectsTopic --include-children --organization=[ORGANIZATION_ID]--log-filter="logName:organizations/[ORGANIZATION_ID]/logs/ AND resource.type=project AND protoPayload.methodName=CreateProject"
- Grant IAM permissions on the Pub/Sub Topic to the service account referenced after running the sink create command.
- Verify the sink was created:
gcloud logging sinks list --organization=[ORGANIZATION_ID]
Variable definitions:
- [PROJECT_NAME]: Name of the GCP project where the service will be deployed.
- [ORGANIZATION_ID]: ID # of the GCP organization.
Further documentation:
- Operations > Operations Suite > Logging > Documentation > Creating an aggregated sink
- Enable APIs:
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
- Configure environment variable YAML file by editting
.env.yaml
and entering:
SOURCE_EMAIL: [SOURCE_EMAIL]
DESTINATION_EMAIL: [DESTINATION_EMAIL]
SENDGRID_API_KEY: [SENDGRID_API_KEY]
- Deploy function:
gcloud functions deploy newProjNotification \
--runtime nodejs10 \
--trigger-topic createProjectsTopic
--env-vars-file .env.yaml
Variable definitions:
- [SOURCE_EMAIL]: Email address the notification will appear to be from.
- [DESTINATION_EMAIL]: Email address the notification will be sent to.
- [SENDGRID_API_KEY]: API Key from SendGrid.
Further documentation:
- Cloud Functions > Documentation > Cloud Pub/Sub Tutorial
- GCP Marketplace > SendGrid Email API
- Create a new test project to trigger an event.
gcloud projects create [PROJECT_ID]
Variable definitions:
- [PROJECT_ID]: Test project name that will trigger the function.
- Remove Cloud Function.
- Remove Pub/Sub Topic.
- Remove any test Projects.
- Remove Log Sink.
- @sendgrid/mail: Twilio SendGrid's v3 Node.js Library