本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
自定义 SMS 发件人 Lambda 触发器
当您为用户群体分配自定义短信发件人触发器时,如果用户事件要求 Amazon Cognito 发送短信,则它会调用 Lambda 函数,而不是其原定设置行为。使用自定义发件人触发器,您的 Amazon Lambda 函数可以通过您选择的方法和提供商向用户发送 SMS 通知。您的函数的自定义代码必须处理和传递用户群体中的所有短信。
此触发器适用于以下场景:您可能希望更好地控制用户池发送短信消息的方式。您的 Lambda 函数可以自定义对 Amazon SNS API 操作的调用,例如,当您想要管理多个 IDs 起源或交叉时。 Amazon Web Services 区域您的函数还可能将消息重定向到另一个传递媒介或第三方服务。
要了解如何配置自定义电子邮件发件人触发器,请参阅激活自定义发件人 Lambda 触发器。
自定义 SMS 发件人 Lambda 触发器源
下表显示了 Lambda 代码中自定义 SMS 触发器源的触发事件。
TriggerSource value |
事件 |
---|---|
CustomSMSSender_SignUp |
用户注册,Amazon Cognito 随即发送欢迎消息。 |
CustomSMSSender_ForgotPassword |
用户请求代码以重置其密码。 |
CustomSMSSender_ResendCode |
用户请求新代码来确认其注册。 |
CustomSMSSender_VerifyUserAttribute |
用户创建新电子邮件地址或电话号码属性,而 Amazon Cognito 将发送代码用于验证该属性。 |
CustomSMSSender_UpdateUserAttribute |
用户更新电子邮件地址或电话号码属性,而 Amazon Cognito 将发送代码用于验证该属性。 |
CustomSMSSender_Authentication |
配置了 SMS 多重验证 (MFA) 的用户将登录。 |
CustomSMSSender_AdminCreateUser |
您在用户池中创建新用户,而 Amazon Cognito 向其发送临时密码。 |
自定义 SMS 发件人 Lambda 触发器参数
Amazon Cognito 传递给此 Lambda 函数的请求是以下参数和 Amazon Cognito 添加到所有请求中的常用参数的组合。
自定义 SMS 发件人请求参数
- type
-
请求版本。对于自定义 SMS 发件人事件,此字符串的值始终为
customSMSSenderRequestV1
。 - code
-
您的函数可以解密并发送给您的用户的加密代码。
- clientMetadata
-
一个或多个键值对,您可以将它们作为自定义输入提供给自定义 SMS 发件人 Lambda 函数触发器。要将此数据传递给您的 Lambda 函数,您可以使用AdminRespondToAuthChallenge和 RespondToAuthChallengeAPI 操作中的 ClientMetadata 参数。Amazon Cognito 在传递给身份验证后函数的请求中不包含来自 ClientMetadata 参数AdminInitiateAuth和 InitiateAuthAPI 操作的数据。
- userAttributes
-
表示用户属性的一个或多个键值对。
自定义 SMS 发件人响应参数
Amazon Cognito 不需要响应中任何额外的返回信息。您的函数可以使用 API 操作来查询和修改资源,或者将事件元数据记录到外部系统。
代码示例
以下 Node.js 示例在您的自定义 SMS 发件人 Lambda 函数中处理 SMS 消息事件。此示例假设您的函数定义了两个环境变量。
KEY_ID
-
您要用于加密和解密用户代码的 KMS 密钥的 ID。
KEY_ARN
-
要用于加密和解密用户代码的 KMS 密钥的 Amazon 资源名称 (ARN)。
部署此函数
-
在您的开发者工作区中安装最新版本的 NodeJS。
-
在您的工作空间中创建一个新的 NodeJS 项目。
-
使用初始化您的项目
npm init -y
。 -
为 Lambda 函数创建脚本:。
touch index.mjs
-
将以下示例的内容粘贴到
index.mjs
。 -
下载项目依赖关系, Amazon Encryption SDK:
npm install @aws-crypto/client-node
. -
将项目目录压缩成一个文件:
zip -r my_deployment_package.zip .
.
import { KmsKeyringNode, buildClient, CommitmentPolicy } from '@aws-crypto/client-node'; // Configure the encryption SDK client with the KMS key from the environment variables const { encrypt, decrypt } = buildClient( CommitmentPolicy.REQUIRE_ENCRYPT_ALLOW_DECRYPT ); const generatorKeyId = process.env.KEY_ID; const keyIds = [process.env.KEY_ARN]; const keyring = new KmsKeyringNode({ generatorKeyId, keyIds }); // Example function to simulate sending SMS. // This example logs message details to CloudWatch Logs from your Lambda function. // Update this function with custom logic that sends an SMS message to 'phoneNumber' with body 'message'. const sendSMS = async (phoneNumber, message) => { // Log the destination with the phone number masked. console.log(`Simulating SMS send to ${phoneNumber.replace(/[^+]/g, '*')}`); // Log the message with the code masked. console.log(`Message content: ${message.replace(/\b\d{6,8}\b/g, '********')}`); // Simulate API delay await new Promise(resolve => setTimeout(resolve, 100)); console.log('SMS sent successfully'); return true; }; export const handler = async (event) => { try { // Decrypt the secret code using encryption SDK let plainTextCode; if (event.request.code) { const { plaintext, messageHeader } = await decrypt(keyring, Buffer.from(event.request.code, 'base64')); plainTextCode = Buffer.from(plaintext).toString('utf-8'); } // Handle different trigger sources if (event.triggerSource == 'CustomSMSSender_SignUp') { const phoneNumber = event.request.userAttributes.phone_number; const message = `Welcome! Your verification code is: ${plainTextCode}`; await sendSMS(phoneNumber, message); } else if (event.triggerSource == 'CustomSMSSender_ResendCode') { // Handle resend code } else if (event.triggerSource == 'CustomSMSSender_ForgotPassword') { // Handle forgot password } else if (event.triggerSource == 'CustomSMSSender_UpdateUserAttribute') { // Handle update attribute } else if (event.triggerSource == 'CustomSMSSender_VerifyUserAttribute') { // Handle verify attribute } else if (event.triggerSource == 'CustomSMSSender_AdminCreateUser') { // Handle admin create user } return; } catch (error) { console.error('Error in custom SMS sender:', error); throw error; } };
使用自定义 SMS 发件人函数评估 SMS 消息功能
自定义 SMS 发件人 Lambda 函数接受您的用户池将发送的 SMS 消息,并且该函数根据您的自定义逻辑传送内容。Amazon Cognito 将 自定义 SMS 发件人 Lambda 触发器参数 发送到您的函数。您的函数可以用这些信息做您想做的事。例如,您可以将代码发送到 Amazon Simple Notification Service (Amazon SNS) 主题。Amazon SNS 主题订阅者可以是 SMS 消息、HTTPS 终端节点或电子邮件地址。
要使用自定义短信发送器 Lambda 函数为 Amazon Cognito 短信创建测试环境,请参阅上的 aws-sample amazon-cognito-user-pools development-and-testing-with 库sms-redirected-to-email中的--
当您将此解决方案部署到用户池时,对于 Amazon Cognito 通常通过 SMS 消息发送的所有消息,Lambda 函数将改为发送到中央电子邮件地址。使用此解决方案自定义和预览 SMS 消息,并测试促使 Amazon Cognito 发送 SMS 消息的用户池事件。完成测试后,回滚 CloudFormation 堆栈,或者从用户池中移除自定义短信发送器功能分配。
重要
不要使用 amazon-cognito-user-pool-development-and-testing-with-