如何通过SNS发送自定义主题与内容的Cloud Custodian邮件通知?
问题解答:Cloud Custodian结合SNS实现自定义邮件主题与内容
仅用SNS完全可以实现自定义邮件主题和内容,你的配置遗漏了**format参数**,导致Cloud Custodian默认发送结构化JSON消息,而非渲染后的模板内容。
核心原因
Cloud Custodian的notify动作搭配SNS传输时,默认会发送包含资源元数据的JSON格式消息,SNS邮件订阅会将这个JSON直接作为邮件正文展示,同时使用平台默认主题。要让SNS发送你定义的主题和模板内容,需要指定输出格式为纯文本或HTML。
修改后的策略配置
在notify动作中添加format: text参数,即可让Custodian渲染模板并通过SNS发送自定义内容:
policies: - name: iam-user-permission-check resource: aws.iam-user description: | Finding IAM users with specific tags. filters: - and: - type: check-permissions match: allowed actions: - '*:*' - "tag:c7n": "absent" actions: - type: notify subject: "IAM Users Without Proper Tags" format: text # 新增:指定输出格式为纯文本 template: | The following IAM users match the filter criteria: {% for user in resources %} - IAM User: {{ user.UserName }} Tags: {{ user.Tags }} {% endfor %} transport: type: sns topic: <sns-topic-arn> region: us-east-1
额外注意事项
- 如果需要HTML格式邮件,将
format设为html,同时调整模板为HTML结构(比如添加<ul>、<li>标签)。 - 确保SNS主题的邮件订阅已完成激活,未激活的订阅会收到SNS平台的默认通知,而非自定义内容。
内容的提问来源于stack exchange,提问作者Ramesh Thiyagarajan
相关产品推荐
相关产品推荐

