CFN部署SNS订阅时filterPolicyScope未生效,触发嵌套过滤策略错误
问题
在CloudFormation(CFN)栈中定义了一个新的SQS订阅带过滤策略的SNS,配置如下:
- name: EventMessageSNSSubscription topicArn: { "Fn::FindInMap": [ 'EventMessageTopicMap', Ref: 'AWS::Region', Ref: 'Stage' ] } region: { Ref: 'AWS::Region' } protocol: sqs endpoint: { "Fn::GetAtt": [ "EventMessageQueue", "Arn" ] } filterPolicyScope: 'MessageBody' filterPolicy: '{"groupType": ["ALPHA"],"programAttributes":{"offerings": ["BETA"],"type": ["GAMMA"]}}'
部署时创建订阅出现错误:
Invalid parameter: Attributes Reason: Filter policy scope MessageAttributes does not support nested filter policy (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 76ba3243-ca5c-5be8-9bfc-6fe457e520ed; Proxy: null)
猜测是filterPolicyScope配置语法问题导致被忽略,使用了默认的MessageAttributes(不支持嵌套过滤策略),请问filterPolicyScope配置是否存在语法问题?
解决方案
你的filterPolicyScope配置确实存在语法问题——在CloudFormation的YAML配置中,这个参数的正确键名是FilterPolicyScope(首字母大写),而非小写开头的filterPolicyScope。
CloudFormation对资源属性的键名大小写敏感,当使用小写开头的键名时,CFN无法识别该属性,会自动使用默认的MessageAttributes作为过滤范围,而MessageAttributes不支持嵌套结构的过滤策略,因此触发了报错。
修改后的正确配置如下:
- name: EventMessageSNSSubscription topicArn: { "Fn::FindInMap": [ 'EventMessageTopicMap', Ref: 'AWS::Region', Ref: 'Stage' ] } region: { Ref: 'AWS::Region' } protocol: sqs endpoint: { "Fn::GetAtt": [ "EventMessageQueue", "Arn" ] } FilterPolicyScope: 'MessageBody' # 修改为首字母大写的键名 filterPolicy: '{"groupType": ["ALPHA"],"programAttributes":{"offerings": ["BETA"],"type": ["GAMMA"]}}'
另外建议将filterPolicy从字符串格式改为YAML原生对象格式,这样更易读且能降低语法错误概率:
filterPolicy: groupType: - "ALPHA" programAttributes: offerings: - "BETA" type: - "GAMMA"
内容的提问来源于stack exchange,提问作者Vegan Vegeta
相关产品推荐
相关产品推荐

