使用 Cloud Tasks 触发 Cloud Run 函数

本教程介绍了如何在 App Engine 应用中使用 Cloud Tasks 来触发 Cloud Run 函数并发送定时发送的邮件。

目标

  • 了解每个组件中的代码。
  • 创建一个 SendGrid 账号。
  • 下载源代码。
  • 创建服务账号以对您的 Cloud Tasks 请求进行身份验证。
  • 部署 Cloud Run 函数,以接收 Cloud Tasks 请求并使用 SendGrid API 发送电子邮件。
  • 创建 Cloud Tasks 任务队列。
  • 部署可让用户发送电子邮件的客户端代码。

费用

Cloud Tasks、Cloud Run functions 和 App Engine 均提供免费层级,因此只要您在这些产品的免费层级内运行本教程,就不会产生额外费用。要了解详情,请参阅价格。

准备工作

  1. 选择或创建 Google Cloud 项目。

    转到 App Engine 页面

  2. 在您的项目中初始化 App Engine 应用:

    1. 在欢迎使用 App Engine网页点击创建应用。

    2. 为您的应用选择一个区域。此位置将用作 Cloud Tasks 请求的 LOCATION_ID 参数,因此请记下该位置。请注意,App Engine 命令中名为 europe-west 和 us-central 的两个位置在 Cloud Tasks 命令中分别称为 europe-west1 和 us-central1。

    3. 选择 Node.js 作为语言,选择标准作为环境。

    4. 如果系统弹出启用结算窗口,请选择您的结算账号。如果您目前没有结算账号,请点击创建结算账号并按照向导中的说明操作。

    5. 在开始页面,点击下一页。您稍后会处理此内容。

  3. 启用 Cloud Run functions 和 Cloud Tasks API。

    启用 API

  4. 安装并初始化 gcloud CLI。

了解代码

此部分介绍了应用代码及其工作原理。

创建任务

索引页面使用 app.yaml 中的处理程序提供。创建任务所需的变量将作为环境变量传入。

runtime: nodejs16

env_variables:
  QUEUE_NAME: "my-queue"
  QUEUE_LOCATION: "us-central1"
  FUNCTION_URL: "https://<region>-<project_id>.cloudfunctions.net/sendEmail"
  SERVICE_ACCOUNT_EMAIL: "<member>@<project_id>.iam.gserviceaccount.com"

# Handlers for serving the index page.
handlers:
  - url: /static
    static_dir: static
  - url: /
    static_files: index.html
    upload: index.html

此代码会创建端点 /send-email。此端点会处理从索引页面提交的表单,并将该数据传递给任务创建代码。

app.post('/send-email', (req, res) => {
  // Set the task payload to the form submission.
  const {to_name, from_name, to_email, date} = req.body;
  const payload = {to_name, from_name, to_email};

  createHttpTaskWithToken(
    process.env.GOOGLE_CLOUD_PROJECT,
    QUEUE_NAME,
    QUEUE_LOCATION,
    FUNCTION_URL,
    SERVICE_ACCOUNT_EMAIL,
    payload,
    date
  );

  res.status(202).send('📫 Your postcard is in the mail! 💌');
});

此代码会实际创建任务并将其发送到 Cloud Tasks 队列。代码通过以下方式构建任务:

  • 将目标类型指定为 HTTP Request。

  • 指定要使用的 HTTP method 和目标的 URL。

  • 将 Content-Type 标头设置为 application/json,以便下游应用可以解析结构化载荷。

  • 添加服务账号电子邮件,以便 Cloud Tasks 可以向请求目标提供凭据,这需要身份验证。此服务账号是单独创建的。

  • 检查以确保用户输入日期不超过 30 天,并将其作为字段 scheduleTime 添加到请求中。

const MAX_SCHEDULE_LIMIT = 30 * 60 * 60 * 24; // Represents 30 days in seconds.

const createHttpTaskWithToken = async function (
  project = 'my-project-id', // Your GCP Project id
  queue = 'my-queue', // Name of your Queue
  location = 'us-central1', // The GCP region of your queue
  url = 'https://example.com/taskhandler', // The full url path that the request will be sent to
  email = '<member>@<project-id>.iam.gserviceaccount.com', // Cloud IAM service account
  payload = 'Hello, World!', // The task HTTP request body
  date = new Date() // Intended date to schedule task
) {
  // Imports the Google Cloud Tasks library.
  const {v2beta3} = require('@google-cloud/tasks');

  // Instantiates a client.
  const client = new v2beta3.CloudTasksClient();

  // Construct the fully qualified queue name.
  const parent = client.queuePath(project, location, queue);

  // Convert message to buffer.
  const convertedPayload = JSON.stringify(payload);
  const body = Buffer.from(convertedPayload).toString('base64');

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url,
      oidcToken: {
        serviceAccountEmail: email,
        audience: url,
      },
      headers: {
        'Content-Type': 'application/json',
      },
      body,
    },
  };

  const convertedDate = new Date(date);
  const currentDate = new Date();

  // Schedule time can not be in the past.
  if (convertedDate < currentDate) {
    console.error('Scheduled date in the past.');
  } else if (convertedDate > currentDate) {
    const date_diff_in_seconds = (convertedDate - currentDate) / 1000;
    // Restrict schedule time to the 30 day maximum.
    if (date_diff_in_seconds > MAX_SCHEDULE_LIMIT) {
      console.error('Schedule time is over 30 day maximum.');
    }
    // Construct future date in Unix time.
    const date_in_seconds =
      Math.min(date_diff_in_seconds, MAX_SCHEDULE_LIMIT) + Date.now() / 1000;
    // Add schedule time to request in Unix time using Timestamp structure.
    // https://googleapis.dev/nodejs/tasks/latest/google.protobuf.html#.Timestamp
    task.scheduleTime = {
      seconds: date_in_seconds,
    };
  }

  try {
    // Send create task request.
    const [response] = await client.createTask({parent, task});
    console.log(`Created task ${response.name}`);
    return response.name;
  } catch (error) {
    // Construct error for Stackdriver Error Reporting
    console.error(Error(error.message));
  }
};

module.exports = createHttpTaskWithToken;

创建电子邮件

此代码会创建 Cloud Run 函数,该函数是 Cloud Tasks 请求的目标。它会验证来自 Cloud Tasks 的 OpenID Connect (OIDC) 不记名令牌,并检查收件人地址是否在 ALLOWED_RECIPIENT_EMAILS 环境变量中。然后,该函数会转义 HTML 明信片中用户提供的字段,并使用 SendGrid API 发送电子邮件。

const escapeHtml = require('escape-html');
const {OAuth2Client} = require('google-auth-library');
const sendgrid = require('@sendgrid/mail');

const authClient = new OAuth2Client();
const EMAIL_REGEX = /^[^\s@<>",;]+@[^\s@<>",;.]+(?:\.[^\s@<>",;.]+)+$/;

/**
 * Responds to an HTTP request from Cloud Tasks and sends an email using data
 * from the request body.
 *
 * @param {object} req Cloud Function request context.
 * @param {object} req.body The request payload.
 * @param {string} req.body.to_email Email address of the recipient.
 * @param {string} req.body.to_name Name of the recipient.
 * @param {string} req.body.from_name Name of the sender.
 * @param {object} res Cloud Function response context.
 */
exports.sendEmail = async (req, res) => {
  // Verify the OIDC bearer token attached by Cloud Tasks.
  const authHeader = (req.headers && req.headers.authorization) || '';
  const match = /^Bearer\s+(\S+)$/i.exec(authHeader);
  if (
    !match ||
    !process.env.FUNCTION_URL ||
    !process.env.SERVICE_ACCOUNT_EMAIL
  ) {
    res.status(401).send('Unauthorized.');
    return;
  }
  try {
    const ticket = await authClient.verifyIdToken({
      idToken: match[1],
      audience: process.env.FUNCTION_URL,
    });
    const payload = ticket.getPayload();
    if (
      !payload ||
      !payload.email_verified ||
      payload.email !== process.env.SERVICE_ACCOUNT_EMAIL
    ) {
      res.status(403).send('Forbidden.');
      return;
    }
  } catch (err) {
    console.error('OIDC token verification failed:', err);
    res.status(401).send('Unauthorized.');
    return;
  }

  // Get the SendGrid API key from the environment variable.
  const key = process.env.SENDGRID_API_KEY;
  if (!key) {
    const error = new Error(
      'SENDGRID_API_KEY was not provided as environment variable.'
    );
    error.code = 401;
    throw error;
  }
  sendgrid.setApiKey(key);

  // Get the body from the Cloud Task request.
  const {to_email, to_name, from_name} = req.body || {};
  if (!to_email) {
    const error = new Error('Email address not provided.');
    error.code = 400;
    throw error;
  } else if (!to_name) {
    const error = new Error('Recipient name not provided.');
    error.code = 400;
    throw error;
  } else if (!from_name) {
    const error = new Error('Sender name not provided.');
    error.code = 400;
    throw error;
  } else if (
    typeof to_email !== 'string' ||
    to_email.length > 254 ||
    !EMAIL_REGEX.test(to_email)
  ) {
    const error = new Error('Invalid email address.');
    error.code = 400;
    throw error;
  }

  // Restrict recipients to an allowlist to prevent open mail relay.
  const allowedRecipients = (process.env.ALLOWED_RECIPIENT_EMAILS || '')
    .split(',')
    .map(addr => addr.trim().toLowerCase())
    .filter(Boolean);
  if (
    allowedRecipients.length === 0 ||
    !allowedRecipients.includes(to_email.trim().toLowerCase())
  ) {
    res.status(403).send('Recipient email address is not allowed.');
    return;
  }

  // Construct the email request.
  const msg = {
    to: to_email,
    from: 'postcard@example.com',
    subject: 'A Postcard Just for You!',
    html: postcardHTML(to_name, from_name),
  };

  try {
    await sendgrid.send(msg);
    // Send OK to Cloud Task queue to delete task.
    res.status(200).send('Postcard Sent!');
  } catch (error) {
    console.error(error);
    // Any status code other than 2xx or 503 will trigger the task to retry.
    res.status(500).send('Unable to send email.');
  }
};

准备应用

设置 SendGrid

  1. 创建一个 SendGrid 账号。

  2. 创建 SendGrid API 密钥:

    1. 登录您的 SendGrid 账号。

    2. 在左侧导航栏中,打开设置,然后点击 API 密钥。

    3. 点击创建 API 密钥,然后选择受限访问权限。在邮件发送标头下,选择完整访问权限。

    4. 复制所显示的 API 密钥(您只能看到一次,请务必将其粘贴到别处,方便日后使用)。

下载源代码

  1. 将示例应用代码库克隆到本地机器:

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    
  2. 转到包含示例代码的目录:

    cd nodejs-docs-samples/cloud-tasks/tutorial-gcf/
    

创建服务账号

Cloud Tasks 请求必须在 Authorization 标头中提供凭据,以便 Cloud Run 函数对请求进行身份验证。此服务账号可让 Cloud Tasks 创建并添加 OIDC 令牌,以对请求进行身份验证。

  1. 在 Google Cloud 控制台中,打开服务账号页面。

    转到“服务账号”

  2. 点击创建服务账号。

  3. 在服务账号名称字段中,输入一个名称,然后点击创建并继续。

  4. 点击完成。您需要在部署函数后向此服务账号授予调用 sendEmail 函数的权限。

  5. 请记下服务账号的邮箱,以便在后续部分中将其用作 SERVICE_ACCOUNT_EMAIL 环境变量。

如需了解详情,请参阅创建服务账号。

部署 Cloud Run 函数

  1. 导航到 function/ 目录:

    cd function/
    
  2. 部署函数及其所需的环境变量:

    gcloud functions deploy sendEmail --runtime nodejs22 --trigger-http \
      --no-allow-unauthenticated \
      --set-env-vars SENDGRID_API_KEY=SENDGRID_API_KEY \
      --set-env-vars SERVICE_ACCOUNT_EMAIL=SERVICE_ACCOUNT_EMAIL \
      --set-env-vars "^;^ALLOWED_RECIPIENT_EMAILS=ALLOWED_RECIPIENT_EMAILS"

    替换以下内容:

    • SENDGRID_API_KEY:您的 SendGrid API 密钥。
    • SERVICE_ACCOUNT_EMAIL:您之前创建的服务账号的邮箱。sendEmail 函数会验证 OIDC 令牌中的此邮箱。
    • ALLOWED_RECIPIENT_EMAILS:以英文逗号分隔的允许收件人邮箱列表,例如 user1@example.com,user2@example.com。

    此命令使用以下标志:

    • --runtime:指定函数的 Node.js 运行时。

    • --trigger-http:指定 Cloud Run functions 触发器类型。

    • --no-allow-unauthenticated:需要进行身份验证才能调用函数。

    • --set-env-vars:设置所需的环境变量。由于 ALLOWED_RECIPIENT_EMAILS 环境变量可能包含英文逗号,因此该命令使用 ^;^ 作为备用分隔符。如需详细了解定界符转义,请参阅 gcloud topic escaping。

  3. 检索已部署函数的 HTTPS 网址:

    gcloud functions describe sendEmail

    在命令输出中,请注意第 1 代函数的 httpsTrigger.url 字段或第 2 代函数的 serviceConfig.uri 字段中的 HTTPS 网址。 您在下一步中以及在 app.yaml 文件中需要用到此网址。

    如需仅输出网址,请为第 1 代函数附加 --format="value(httpsTrigger.url)",或为第 2 代函数附加 --format="value(serviceConfig.uri)"。

  4. 如需验证 OIDC 令牌 audience 声明,请使用 FUNCTION_URL 环境变量更新函数:

    gcloud functions deploy sendEmail \
      --update-env-vars FUNCTION_URL=FUNCTION_URL

    将 FUNCTION_URL 替换为 gcloud functions describe sendEmail 返回的 HTTPS 网址。

  5. 向 Cloud Tasks 服务账号授予调用 sendEmail 的权限:

    1. 在 Google Cloud 控制台中,前往 Cloud Run functions 页面。

    前往 Cloud Run functions

    1. 选择 sendEmail 函数。

    2. 如果您没有看到 sendEmail 的权限信息,请点击显示信息面板。

    3. 点击添加主账号。

    4. 在新的主账号字段中,输入您之前创建的服务账号的邮箱。请勿使用 allAuthenticatedUsers 主账号,因为这会向任何经过身份验证的 Google 账号授予调用访问权限。

    5. 选择角色:

      • 第 1 代函数:选择 Cloud Functions Invoker (roles/cloudfunctions.invoker)。
      • 第 2 代函数:选择 Cloud Run Invoker (roles/run.invoker)。
    6. 点击保存。

创建 Cloud Tasks 队列

  1. 使用以下 gcloud 命令创建队列:

    gcloud tasks queues create my-queue --location=LOCATION

    将 LOCATION 替换为队列的首选位置,例如 us-west2。如果您未指定位置,gcloud CLI 会选择默认位置。

  2. 验证是否已成功创建队列:

    gcloud tasks queues describe my-queue --location=LOCATION

    将 LOCATION 替换为队列的位置。

将端点和任务创建者部署到 App Engine

  1. 导航到 app/ 目录:

    cd ../app/
    
  2. 使用您的值更新 app.yaml 中的变量:

    env_variables:
      QUEUE_NAME: "my-queue"
      QUEUE_LOCATION: "us-central1"
      FUNCTION_URL: "https://<region>-<project_id>.cloudfunctions.net/sendEmail"
      SERVICE_ACCOUNT_EMAIL: "<member>@<project_id>.iam.gserviceaccount.com"

    如需查找您的队列位置,请使用以下命令:

    gcloud tasks queues describe my-queue --location=LOCATION

    将 LOCATION 替换为队列的位置。

    使用您之前检索到的函数网址。

  3. 使用以下命令将应用部署到 App Engine 标准环境:

    gcloud app deploy
  4. 打开应用,以电子邮件的形式发送明信片:

    gcloud app browse

清除数据

完成本教程后,您可以清理您创建的资源,让它们停止使用配额,以免产生费用。以下部分介绍如何删除或关闭这些资源。

删除资源

您可以清理在 Google Cloud 上创建的资源,以免这些资源占用配额,日后产生费用。以下部分介绍如何删除或关闭这些资源。

删除 Cloud Run functions 函数

  1. 前往 Google Cloud 控制台中的 Cloud Run 函数页面。

    前往 Cloud Run functions 页面。

  2. 点击函数旁边的复选框。

  3. 点击页面顶部的删除按钮并确认删除操作。

删除 Cloud Tasks 队列

  1. 在控制台中打开 Cloud Tasks 任务队列页面。

    前往 Cloud Tasks 队列页面

  2. 选择要删除的队列名称,然后点击删除队列。

  3. 确认该操作。

删除项目

为了避免产生费用,最简单的方法是删除您为本教程创建的项目。

要删除项目,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除。
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

后续步骤