如何在AWS CloudFormation中为AWS预算配置多邮箱通知接收方
CloudFormation拆分逗号分隔邮箱列表到Budget订阅者字段
问题
我有如下CloudFormation模板:
Parameters: EmailList: Type: String Resources: Budget: Type: AWS::Budgets::Budget Properties: Budget: BudgetName: "my-test-budget" NotificationsWithSubscribers: - Notification: NotificationType: !Ref NotificationType ComparisonOperator: GREATER_THAN Threshold: !Ref WarningThreshold Subscribers: Address: !Ref EmailList
EmailList是逗号分隔的邮箱字符串(例如:abc@gmail.com,hello123@gmail.com),直接引用会报错,因为Address字段仅支持单个邮箱。用!Split返回数组也不兼容,!Select只能指定单个邮箱,无法适配用户提供的任意数量邮箱。如何拆分EmailList并将每个邮箱单独分配到Address字段?
解决方案
方法1:使用AWS语言扩展(推荐)
借助CloudFormation的AWS::LanguageExtensions提供的Fn::ForEach函数,可直接循环处理邮箱数组,动态生成订阅者对象:
Transform: AWS::LanguageExtensions Parameters: EmailList: Type: String NotificationType: Type: String Default: ACTUAL WarningThreshold: Type: Number Default: 80 Resources: Budget: Type: AWS::Budgets::Budget Properties: Budget: BudgetName: "my-test-budget" NotificationsWithSubscribers: - Notification: NotificationType: !Ref NotificationType ComparisonOperator: GREATER_THAN Threshold: !Ref WarningThreshold Subscribers: !ForEach [email, !Split [",", !Ref EmailList], [{ Address: !Ref email, SubscriptionType: EMAIL }]]
关键步骤
- 用
Transform: AWS::LanguageExtensions启用语言扩展,才能使用Fn::ForEach功能。 !Split [",", !Ref EmailList]将逗号分隔的邮箱字符串拆分为数组。!ForEach遍历数组,为每个邮箱生成独立的订阅者对象,最终组成符合要求的Subscribers数组。
方法2:自定义宏(兼容旧环境)
如果无法使用语言扩展,可通过自定义宏实现:
- 创建Lambda函数,接收拆分后的邮箱数组,返回格式为
[{"Address": "xxx", "SubscriptionType": "EMAIL"}, ...]的JSON数组。 - 在CloudFormation模板中声明宏,并调用宏传入拆分后的邮箱列表:
Transform: [MyEmailSubscriberMacro] # ... 其他参数和资源 ... Subscribers: !GenerateSubscribers [!Split [",", !Ref EmailList]]
(注:MyEmailSubscriberMacro和!GenerateSubscribers需根据实际宏定义调整)
内容的提问来源于stack exchange,提问作者Ranyk
相关产品推荐
相关产品推荐

