This tutorial shows you how to use Cloud Tasks within an App Engine application to trigger a Cloud Run function and send a scheduled email.
Understanding the code
This section walks you through the app's code and explains how it works.
Creating the task
The index page is served using handlers in the app.yaml
. The variables needed for task creation are passed in as environment variables.
This code creates the endpoint /send-email
. This endpoint handles form submissions from the index page and passes that data to the task creation code.
This code actually creates the task and sends it on to the Cloud Tasks queue. The code builds the task by:
Specifying the target type as
HTTP Request
.Specifying the
HTTP method
to be used and theURL
of the target.Setting the
Content-Type
header toapplication/json
so downstream applications can parse the structured payload.Adding a service account email so that Cloud Tasks can provide credentials to the request target, which requires authentication. The service account is created separately.
Checking to make sure the user input for date is within the 30 days maximum and adding it to the request as field
scheduleTime
.
Creating the email
This code creates the Cloud Run function that is the target for the Cloud Tasks request. It uses the request body to construct an email and send it via the SendGrid API.
Preparing the application
Setting up SendGrid
Create a SendGrid account.
- You can either do this manually via the SendGrid website
- or you can use the Google Cloud Launcher, which will create an account for you and integrate billing. See Creating a SendGrid account using Cloud Launcher.
Create a SendGrid API key:
Log in to your SendGrid account.
In the left hand navigation open Settings and click API Keys.
Click Create API Key and select restricted access. Under the Mail Send header, select Full Access.
Copy the API Key when it is displayed (you will only see this once, make sure you paste it somewhere so you can use it later on).
Downloading the source code
Clone the sample app repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Change to the directory that contains the sample code:
cd cloud-tasks/
Deploying the Cloud Run function
Navigate to the
function/
directory:cd function/
Deploy the function:
gcloud functions deploy sendEmail --runtime nodejs14 --trigger-http \ --no-allow-unauthenticated \ --set-env-vars SENDGRID_API_KEY=SENDGRID_API_KEY \
Replace
SENDGRID_API_KEY
with your API key.This command uses flags:
--trigger-http
to specify the Cloud Run functions trigger type.--no-allow-unauthenticated
to specify the function invocation requires authentication.--set-env-var
to set your SendGrid credentials
Set access control for the function to only allow authenticated users.
Select the
sendEmail
function in the Cloud Run functions UI.If you don't see permissions info for
sendEmail
, click SHOW INFO PANEL in the upper right hand corner.Click the Add principals button above.
Set New principals to
allAuthenticatedUsers
.Set the Role.
- First generation (1st gen) functions: set the role to
Cloud Function Invoker
- Second generation (2nd gen) functions: set the role to
Cloud Run Invoker
- First generation (1st gen) functions: set the role to
Click SAVE.
Creating a Cloud Tasks queue
Create a queue using the following
gcloud
command:gcloud tasks queues create my-queue --location=LOCATION
Replace
LOCATION
with your preferred location for the queue, for example,us-west2
. If you do not specify the location, the gcloud CLI picks the default.Verify that it was created successfully:
gcloud tasks queues describe my-queue --location=LOCATION
Replace
LOCATION
with the location of the queue.
Creating a service account
The Cloud Tasks request must provide credentials in the Authorization
header for the Cloud Run function to authenticate the request. This service account allows Cloud Tasks to create and add an OIDC token for that purpose.
In the Service accounts UI, click +CREATE SERVICE ACCOUNT.
Add a service account name(friendly display name) and select create.
Set the Role and click Continue.
- First generation (1st gen) functions: set the role to
Cloud Function Invoker
- Second generation (2nd gen) functions: set the role to
Cloud Run Invoker
- First generation (1st gen) functions: set the role to
Select Done.
Deploying the endpoint and the task creator to App Engine
Navigate to
app/
directory:cd ../app/
Update variables in the
app.yaml
with your values:To find your queue location, use the following command:
gcloud tasks queues describe my-queue --location=LOCATION
Replace
LOCATION
with the location of the queue.To find your function URL, use the following command:
gcloud functions describe sendEmail
Deploy the application to the App Engine standard environment, using the following command:
gcloud app deploy
Open the application to send a postcard as an email:
gcloud app browse