/gcf-newproj-notification

Send email alerts when new GCP projects are created.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

gcf-newproj-notification

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.

Prerequisites

  1. Sign up for SendGrid Email API in the GCP Marketplace.

Create Pub/Sub Topic

  1. Enable API:
gcloud services enable pubsub.googleapis.com
  1. Create Topic:
gcloud pubsub topics create createProjectsTopic

Create Aggregated Log Sink

  1. 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"
  1. Grant IAM permissions on the Pub/Sub Topic to the service account referenced after running the sink create command.
  2. 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:

Create Cloud Function

  1. Enable APIs:
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
  1. 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]
  1. 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:

Test

  1. 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.

Cleanup

  1. Remove Cloud Function.
  2. Remove Pub/Sub Topic.
  3. Remove any test Projects.
  4. Remove Log Sink.

Dependencies

  • @sendgrid/mail: Twilio SendGrid's v3 Node.js Library

References