Skip to content

marcusmonteirodesouza/google-cloud-sendgrid-email-sending

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Cloud - SendGrid Email Sending

This is an example of how to send emails in Google Cloud via SendGrid.

System Overview

Architecture Diagram

system architecture diagram

Components

A Pub/Sub triggered Cloud Function that sends emails using SendGrid.

It expects a message with the following structure:

{
  "to": "recipient@example.com",
  "subject": "My email subject",
  "body": "<h1>The email body can be html</h1>"
}

Deployment

The system is deployed using terraform, running in Cloud Build.

Pre-requisites

  1. Create an API key in SendGrid.
  2. Set up domain authentication in SendGrid.
  3. Create a Google Cloud Organization.
  4. Install terraform.
  5. Install the gcloud CLI.

Bootstrap

This is the process that creates the Google Cloud Project, enables the required APIs, and grants the necessary permissions to the Service Accounts, including the ones required for the Cloud Build Service Account to deploy the system.

  1. Run gcloud auth login
  2. Run gcloud auth application-default login.
  3. cd into the deployment/google-cloud/terraform/bootstrap folder.
  4. Comment out the entire contents of the deployment/google-cloud/terraform/bootstrap/backend.tf file.
  5. Create a terraform.tfvars file and add your variables' values. Leave the sourcerepo_name empty for now.
  6. Run terraform init.
  7. Run terraform apply -target=module.project.
  8. Uncomment the deployment/google-cloud/terraform/bootstrap/backend.tf file's contents and add the value of the tfstate_bucket output as the value of the bucket attribute.
  9. Run terraform init and answer yes.
  10. Create a Cloud Source Repository in the project your just created. Optionally, fork this repository and create a Cloud Source Repository by mirroring your forked repo. Update the sourcerepo_name variable with the repository name.
  11. Run terraform apply.

Deployment

This is a Cloud Build build that actually deploys the system.

  1. The pipeline can be triggered by either:
    • Push a commit to your Cloud Source Repository or to your Github fork.
    • Go to your project's Cloud Build Dashboard and manually run the push-to-branch-deployment trigger.