我正在使用 terraform 创建 aws 预算警报。我知道如何在 aws 中为特定帐户创建一个警报。 我想创建一个 terraform 代码,我将能够在其中为 aws 中的 100 个帐户创建例如 100 个预算警报。 我无法为所有帐户创建一个预算警报,因为我想单独记帐我的费用,如果这些帐户中的任何一个超出预算,我想收到一封电子邮件。
如何实现?
答案 0 :(得分:0)
您需要设置为 terrform aws providers 对每个帐户具有跨帐户 Delegate access across AWS accounts using IAM roles IAM 角色访问权限,然后在创建预算警报时使用它们。
provider "aws" {
alias = "myaccount"
region = "eu-central-1"
assume_role {
role_arn = "arn:aws:iam::1234567890:role/AdminRole"
}
allowed_account_ids = ["1234567890"]
}
然后最终将它们用于各种资源
resource "aws_budgets_budget" "cost" {
provider = "aws.myaccount"
# ...
budget_type = "COST"
limit_amount = "100"
limit_unit = "USD"
}
OR
如果您使用 AWS Organizations 来集中管理帐户。
您可以使用 AWS StackSets 在目标帐户中使用创建警报的 terraform 代码创建代码构建作业。
Preferred
方式,因为一旦新帐户可用,这将负责创建代码构建作业(`取决于您在整个组织中执行的配置天气或使用组织单位(OU))。>
然后,您可以在 organizations master account
中使用 lambda 来使用自定义参数(如该特定帐户的 budget
)调用该代码构建作业。如果我们将一个事件 (SNS
) 从目标账户发送回主账户并将我们的 lambda 挂钩到相同的,这也可以自动化。
如果您要标记您的 AWS 帐户 Tagging AWS Organizations resources,您可以在 DynamoDB
的某处预定义一些信息,并在您的 lambda 中使用它来调用相应的代码构建作业。
OR
只需创建一个 stackset
并使用如下所示的一些 cloudformation 代码,该代码从 organization master account
触发,budget
数量作为堆栈的参数。
OU
),Resources:
# note that changing some attributes on Budget resources will result in an error.
# this unfortunately is a cloudformation error described here: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/365
# workaround is to delete the budget resource and then re-add it.
Budget:
Type: AWS::Budgets::Budget
Properties:
Budget:
BudgetName: !Sub '${resourcePrefix}-budget-${AWSAccount.Alias}' # AWSAccount.Alias resolves to IAM Alias of current account
BudgetLimit:
Amount: !GetAtt AWSAccount.Tags.budget-alarm-threshold # Resolves to value of tag of current account
Unit: USD
TimeUnit: MONTHLY
BudgetType: COST
NotificationsWithSubscribers:
- Notification:
NotificationType: FORECASTED
ComparisonOperator: GREATER_THAN
Threshold: 1
Subscribers:
- SubscriptionType: SNS
Address: !Ref BudgetAlarmTopic
- SubscriptionType: EMAIL
Address: olaf_conijn@hotmail.com
答案 1 :(得分:0)
对于这项任务,我同意 @samtoddler
的观点,即 AWS StackSets 会比 Terraform 更好地解决问题。但如果您仍然想使用 Terraform,一种方法是创建一个 terraform 模块并在您的目标帐户中使用它。
如果您需要Budget Alarms for Terraform,我编写了这个模块。它还可以与 Slack 集成以接收通知。