You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform项目中为AWS员工沙箱账户配置50美元固定预算并集成AWS Budget Actions?

为AWS沙箱账户自动配置50美元预算与超支防护

我来帮你把AWS预算和告警动作无缝集成到你的Terraform代码里,这样每创建一个新的员工沙箱账户,就能自动配上50美元的固定预算,还能做好超支防护~

下面是完整的集成方案,直接和你现有的账户创建代码搭配使用就行:

1. 先创建50美元的月度成本预算

我们用aws_budgets_budget资源来定义预算,并且绑定到新创建的沙箱账户:

resource "aws_organizations_account" "account" {
  name       = "sandbox"
  email      = "new-user+sandbox@company.com"
  role_name  = "myOrganizationRole"
}

resource "aws_budgets_budget" "sandbox_budget" {
  name              = "sandbox-${aws_organizations_account.account.name}-monthly-budget"
  budget_type       = "COST"
  limit_amount      = "50"
  limit_unit        = "USD"
  time_unit         = "MONTHLY"

  # 把预算绑定到刚创建的沙箱账户
  cost_filters {
    linked_account = [aws_organizations_account.account.id]
  }

  cost_types {
    include_tax = true
    include_subscription = true
    use_amortized = false
    use_blended = false
  }
}

这里的cost_filters是关键,它能确保预算只监控这个特定沙箱账户的开销,不会和其他账户混在一起。

2. 添加超支告警和自动防护动作

光有预算不够,我们还得设置告警,甚至自动执行防护动作。用aws_budgets_budget_action就能实现:

第一步:设置80%阈值的告警通知

当开销达到预算的80%(也就是40美元)时,给运维团队发邮件提醒:

# 获取当前组织主账户的ID,用来生成执行角色ARN
data "aws_caller_identity" "current" {}

resource "aws_budgets_budget_action" "sandbox_budget_alert" {
  budget_name = aws_budgets_budget.sandbox_budget.name
  action_type = "NOTIFICATION"
  notification_type = "ACTUAL"

  threshold       = 80
  threshold_type  = "PERCENTAGE"

  action_threshold {
    action_threshold_type = "PERCENTAGE"
    action_threshold_value = 80
  }

  definition {
    notification {
      notification_type = "ACTUAL"
      comparison_operator = "GREATER_THAN"
      threshold = 80
      threshold_type = "PERCENTAGE"
    }

    subscriber {
      address = "devops-team@company.com" # 替换成你的告警收件邮箱
      subscription_type = "EMAIL"
    }
  }

  # 这个角色需要有执行预算动作的权限,你可以用AWS预定义的AWSBudgetsActionsExecutionRole
  execution_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-budgets-execution-role"
}

第二步:设置100%阈值的自动防护(可选)

如果想更严格,当开销达到50美元时,自动停止沙箱里的资源(比如EC2实例),可以加这个动作:

resource "aws_budgets_budget_action" "sandbox_budget_stop_resources" {
  budget_name = aws_budgets_budget.sandbox_budget.name
  action_type = "APPLY_IAM_POLICY"
  notification_type = "ACTUAL"

  threshold       = 100
  threshold_type  = "PERCENTAGE"

  action_threshold {
    action_threshold_type = "PERCENTAGE"
    action_threshold_value = 100
  }

  definition {
    iam_policy_action {
      # 这个策略需要提前在沙箱账户里创建,包含停止资源的权限(比如ec2:StopInstances)
      policy_arn = "arn:aws:iam::${aws_organizations_account.account.id}:policy/StopSandboxResources"
    }

    notification {
      notification_type = "ACTUAL"
      comparison_operator = "GREATER_THAN_OR_EQUAL"
      threshold = 100
      threshold_type = "PERCENTAGE"
    }
  }

  execution_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-budgets-execution-role"
}

3. 几个关键注意点

  • 执行角色权限:确保你用的execution_role_arn有足够的权限,比如发送邮件通知、应用IAM策略等。AWS有预定义的AWSBudgetsActionsExecutionRole角色,你可以直接用或者基于它自定义。
  • 预算创建位置:因为沙箱账户是通过Organizations创建的子账户,预算需要在组织主账户里创建,然后通过linked_account绑定到子账户。
  • 阈值灵活调整:你可以根据需求修改阈值,比如设置70%发第一次提醒,90%发第二次,100%执行防护动作,给用户留缓冲时间。
  • 时间范围:示例用的是月度预算,如果你想按天或者季度设置,调整time_unit为DAILY或QUARTERLY就行。

把这些代码整合到你的main.tf里,以后每创建一个新沙箱账户,Terraform就会自动帮你配置好50美元的预算和超支防护啦~

内容的提问来源于stack exchange,提问作者Pierre-Alexandre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:27:28