AWS CloudFormation配置SQS集成时MessageAttributes类型不匹配报错如何解决
问题根因
你遇到的类型不匹配报错,核心是 AWS::ApiGatewayV2::Integration 选择 SQS-SendMessage 作为集成子类型时,RequestParameters 下的所有字段都要求传入字符串类型值。MessageAttributes 不能直接写YAML结构化对象/数组,需要传入符合SQS API规范的JSON序列化字符串。
正确配置方案
推荐直接使用CloudFormation内置的 !ToJsonString 函数自动将YAML对象转为符合要求的JSON字符串,写法如下:
HttpApiPayloadRouteIntegration: Type: AWS::ApiGatewayV2::Integration Properties: ApiId: !Ref HttpApi Description: Proxy incoming HTTP Payload into SQS IntegrationType: AWS_PROXY IntegrationSubtype: SQS-SendMessage PayloadFormatVersion: "1.0" CredentialsArn: !GetAtt HttpApiRole.Arn RequestParameters: QueueUrl: !Ref HTTPApiEventQueue MessageBody: $request.body MessageGroupId: $request.body.repository.full_name MessageDeduplicationId: !Join [ "-", [$request.body.repository.full_name, $request.body.alert.number], ] # 正确配置:结构化属性用!ToJsonString转为JSON字符串 MessageAttributes: !ToJsonString firstAttribute: DataType: String StringValue: hello world secondAttribute: DataType: String StringValue: goodbye world
如果你的CloudFormation环境不支持 !ToJsonString 函数,也可以直接手写转义后的JSON字符串:
MessageAttributes: | { "firstAttribute": { "DataType": "String", "StringValue": "hello world" }, "secondAttribute": { "DataType": "String", "StringValue": "goodbye world" } }
如果需要从请求上下文/请求参数动态取值,直接替换对应StringValue的内容即可,示例:
MessageAttributes: !ToJsonString sourceIp: DataType: String StringValue: $context.identity.sourceIp requestPath: DataType: String StringValue: $request.path
内容的提问来源于stack exchange,提问作者user3180997
相关产品推荐
相关产品推荐

