In this section, you create a SendGrid Email API account and obtain an API key. If you already have SendGrid enabled, skip to Obtain a Sendgrid Email API Key and ensure your existing API key has adequate permissions. Go to the Google Cloud console
In this section, you create a SendGrid Email API account and obtain an API key. If you already have SendGrid enabled, skip to Obtain a Sendgrid Email API Key and ensure your existing API key has adequate permissions. Go to the Google Cloud console
In the search box at the top of the page, search for SendGrid Email API. SendGrid Search
* In the search box at the top of the page, search for SendGrid Email API. SendGrid Search
On the next page, select the plan that fits your needs.
* On the next page, select the plan that fits your needs.
Review the terms and if you're comfortable, click Subscribe.
* Review the terms and if you're comfortable, click Subscribe.
Activate the SendGrid service by clicking Register with SendGrid.
* Activate the SendGrid service by clicking Register with SendGrid.
On the registration screen, enter a username, password, and email address. Accept the terms of service and click Continue.
* On the registration screen, enter a username, password, and email address. Accept the terms of service and click Continue.
At the confirmation dialog, click Return to Google.
* At the confirmation dialog, click Return to Google.
- Obtain a SendGrid Email API key
## Obtain a SendGrid Email API key
Click Manage API keys on SendGrid website. A new tab opens for the SendGrid website. Manage API keys
Click Manage API keys on SendGrid website. A new tab opens for the SendGrid website. Manage API keys
Complete the form or sign in, if prompted. Then, click Get Started!
* Complete the form or sign in, if prompted. Then, click Get Started!
In the menu panel, expand Settings and click API Keys.
* In the menu panel, expand Settings and click API Keys.
On the next screen, click the Create API Key button.
* On the next screen, click the Create API Key button.
Under API Key Name, enter "Email Notifications," select Full Access, and then click the Create & View button.[Text Wrapping Break][Text Wrapping Break]API key name
* Under API Key Name, enter "Email Notifications," select Full Access, and then click the Create & View button.[Text Wrapping Break][Text Wrapping Break]API key name
You are shown the API key. Record the value. You need it in the next section.
* You are shown the API key. Record the value. You need it in the next section.
Click Done. You are shown the current set of API keys. Close the tab and return to the Google Cloud console.
* Click Done. You are shown the current set of API keys. Close the tab and return to the Google Cloud console.
Next, you deploy a Cloud Function to send notifications to an email address.
* Next, you deploy a Cloud Function to send notifications to an email address.
- Create the SendGrid Cloud Function
## Create the SendGrid Cloud Function
In this section, you deploy a function that sends notifications to your email account.
In this section, you deploy a function that sends notifications to your email account.
Ensure that you're using the same PROJECT_ID you used to create the Pub/Sub topic.
* Ensure that you're using the same PROJECT_ID you used to create the Pub/Sub topic.
Click Create Function. SendGrid function
* Click Create Function. SendGrid function
Set Function name to send-high-and-critical-finding-email-notification and the Trigger type to Pub/Sub.
* Set Function name to send-high-and-critical-finding-email-notification and the Trigger type to Pub/Sub.
Select the Pub/Sub topic that you created in Setting up a Pub/Sub topic.
* Select the Pub/Sub topic that you created in Setting up a Pub/Sub topic.
Click Save, and then click Next.
* Click Save, and then click Next.
On the next page, set Runtime to Python 3.10.
* On the next page, set Runtime to Python 3.10.
In the file list, click requirements.txt and add the following to the text field: sendgrid.[Text Wrapping Break][Text Wrapping Break]SendGrid requirements
* In the file list, click requirements.txt and add the following to the text field: sendgrid.[Text Wrapping Break][Text Wrapping Break]SendGrid requirements
Click main.py and replace the contents with your code snippet.
* Click main.py and replace the contents with your code snippet.
Navigate to the Entry point field and enter the name of the function in the code snippet (send_email_notification, in this example).
* Navigate to the Entry point field and enter the name of the function in the code snippet (send_email_notification, in this example).
Click Deploy. You are returned to the Cloud Functions list where you should see your new function. When a green check mark appears next to the function name, it has been successfully deployed. The process could take a few minutes.
* Click Deploy. You are returned to the Cloud Functions list where you should see your new function. When a green check mark appears next to the function name, it has been successfully deployed. The process could take a few minutes.