如何通过CDK为Amplify Lambda传递SQS URL并实现消息发送?
在AWS Amplify环境下用CDK配置Lambda向SQS发送消息的正确方式
问题根源
你遇到的process.env['SQS_URL']返回undefined的问题,核心原因是:
- 通过
Function.fromFunctionAttributes获取的Lambda是只读引用,直接修改env属性不会生效,CDK不会将该变更同步到云端。 - 错误为Lambda指定了新执行角色,覆盖了Amplify原有的角色,且该角色的信任主体配置不符合Lambda执行要求。
修正后的CDK配置代码
import * as cdk from 'aws-cdk-lib'; import { AmplifyDependentResourcesAttributes, AmplifyHelpers } from '@aws-amplify/cli-extensibility-helper'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as sqs from 'aws-cdk-lib/aws-sqs'; import * as lambdaEventSources from 'aws-cdk-lib/aws-lambda-event-sources'; import * as iam from 'aws-cdk-lib/aws-iam'; export class cdkStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps, amplifyResourceProps?: any) { super(scope, id, props); // 声明依赖的Amplify资源 const dependencies: AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency( this, amplifyResourceProps.category, amplifyResourceProps.resourceName, [ { category: 'function', resourceName: 'usersimulation' }, { category: 'function', resourceName: 'marketprocessor' } ] ); // 创建SQS队列 const sqsQueue = new sqs.Queue(this, 'sqs', { queueName: `sqs_queue_${cdk.Fn.ref('env')}` }); // 获取Amplify创建的usersimulation Lambda的核心属性 const userSimulationLambdaName = cdk.Fn.ref(dependencies.function.usersimulation.Name); const userSimulationLambdaRoleArn = cdk.Fn.ref(dependencies.function.usersimulation.Role); // 通过CfnFunction引用已有Lambda,支持修改配置 const userSimulationLambda = lambda.CfnFunction.fromCfnFunctionAttributes(this, 'usersimulation-lambda', { functionName: userSimulationLambdaName }); // 给Lambda添加SQS_URL环境变量 userSimulationLambda.addPropertyOverride('Environment.Variables.SQS_URL', sqsQueue.queueUrl); // 获取Lambda原有执行角色,添加SQS发送权限 const userSimulationRole = iam.Role.fromRoleArn(this, 'usersimulation-role', userSimulationLambdaRoleArn); sqsQueue.grantSendMessages(userSimulationRole); // 配置marketmessageprocessor的SQS事件源 const marketMessageProcessorArn = cdk.Fn.ref(dependencies.function.marketprocessor.Arn); const marketMessageProcessorLambda = lambda.Function.fromFunctionAttributes(this, 'marketmessageprocessor-lambda', { functionArn: marketMessageProcessorArn, sameEnvironment: true }); marketMessageProcessorLambda.addEventSource(new lambdaEventSources.SqsEventSource(sqsQueue, { batchSize: 5 })); // 给marketprocessor角色添加SQS消费权限 const marketProcessorRoleArn = cdk.Fn.ref(dependencies.function.marketprocessor.Role); const marketProcessorRole = iam.Role.fromRoleArn(this, 'marketprocessor-role', marketProcessorRoleArn); sqsQueue.grantConsumeMessages(marketProcessorRole); } }
关键修正点说明
用
CfnFunction修改环境变量:
Amplify创建的Lambda属于已存在资源,Function.fromFunctionAttributes是只读引用,无法直接修改配置。必须用CfnFunction.fromCfnFunctionAttributes获取引用,再通过addPropertyOverride更新环境变量,确保CDK生成有效的CloudFormation变更。复用Amplify的Lambda执行角色:
不要手动创建新角色,直接通过依赖资源获取Amplify为Lambda生成的角色ARN,再给该角色添加对应SQS操作权限,避免破坏原有权限配置。权限配置:
- 给
usersimulation角色添加SQS发送消息权限,确保Lambda能调用sendMessageBatch接口。 - 给
marketmessageprocessor角色添加SQS消费权限,确保能读取并处理队列消息。
- 给
Lambda代码注意事项
保持你现有的发送消息代码不变,部署后需确认:
- Lambda控制台的环境变量列表中存在
SQS_URL,且值为目标队列的正确地址。 - 使用的AWS SDK版本正常初始化,无兼容性问题。
内容的提问来源于stack exchange,提问作者Ilijanovic
相关产品推荐
相关产品推荐

