如何在API Gateway V2集成SQS FIFO队列时用两个请求参数设置MessageGroupId?
问题解决:API Gateway V2集成SQS FIFO时拼接MessageGroupId失败
问题原因
API Gateway V2的AWS_PROXY集成(SQS-SendMessage子类型)中,RequestParameters仅支持引用单个请求体属性(如$request.body.orgKey),不允许直接通过拼接两个变量的方式生成新值。你写的'${request.body.orgKey}${request.body.entityType}'会被API Gateway当作一个完整的变量路径去解析,自然找不到对应的属性,从而抛出无法解析的错误。
解决方案
改用**映射模板(Mapping Templates)**来实现字段拼接,需要将PayloadFormatVersion切换为2.0(1.0版本不支持映射模板),通过Velocity模板语法完成两个字段的拼接。修改后的CloudFormation配置如下:
APIGatwaySQSIntegration: Type: 'AWS::ApiGatewayV2::Integration' Properties: ApiId: !Ref ApiGateway CredentialsArn: '${self:provider.iam.role}' IntegrationType: 'AWS_PROXY' IntegrationSubtype: 'SQS-SendMessage' PayloadFormatVersion: '2.0' # 必须改为2.0以启用映射模板 RequestParameters: QueueUrl: !Ref webhooksQueue MessageBody: '$request.body' MessageDeduplicationId: '$request.body.eventId' RequestTemplates: application/json: | { "MessageGroupId": "$input.path('$.orgKey')$input.path('$.entityType')" }
说明
PayloadFormatVersion: '2.0'是启用映射模板的前提,API Gateway V2仅在该版本下支持自定义请求模板。- 映射模板中使用
$input.path('$.orgKey')和$input.path('$.entityType')分别获取两个字段值,直接拼接即可生成合并后的MessageGroupId。 - 原有的
RequestParameters配置可以保留,映射模板中定义的参数会覆盖或补充原有配置。
内容的提问来源于stack exchange,提问作者Eugene Kim
相关产品推荐
相关产品推荐

