Closed5

AWSのBudgetをterraformで管理してみる

utmutm

環境

Terraform v1.5.0

事前にtemplateから作成していた"My Zero-Spend Budget"をimportする方向で進める

utmutm

初期化

main.tf

main.tf
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

resource "aws_budgets_budget" "my_zero_spend_budget" {
  name = "My Zero-Spend Budget"
}
$ terraform init
utmutm

terraform import

Importを参考にimportする

Budgets can be imported using AccountID:BudgetName, e.g.,
$ terraform import aws_budgets_budget.myBudget 123456789012:myBudget

$ terraform import aws_budgets_budget.my_zero_spend_budget AccountID:my_zero_spend_budget
...
...

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

terraform.tfstateが更新される

utmutm

main.tfを更新する

  • terraform.tfstateを参考にmain.tfを更新
  • terraform planで差分がないことを確認
╷
│ Error: Value for unconfigurable attribute
│
│   with aws_budgets_budget.my_zero_spend_budget,
│   on main.tf line 16, in resource "aws_budgets_budget" "my_zero_spend_budget":
│   16:   arn               = "arn:aws:budgets:: {AccountID}:budget/My Zero-Spend Budget"
│
│ Can't configure a value for "arn": its value will be decided automatically based on the result of applying this configuration.
╵
╷
│ Error: Invalid or unknown key
│
│   with aws_budgets_budget.my_zero_spend_budget,
│   on main.tf line 18, in resource "aws_budgets_budget" "my_zero_spend_budget":
│   18:   id                = "{AccountID}:My Zero-Spend Budget"
│
╵
  • id / arn は不要だった

In addition to all arguments above, the following attributes are exported:

  • planを実行する前に、terraform validate を実行して問題がないか検証しておくべきだった
$ terraform validate
Success! The configuration is valid.
  • 再度planを実行し差分がないことを確認
$ terraform plan
aws_budgets_budget.my_zero_spend_budget: Refreshing state... [id={AccountID}:My Zero-Spend Budget]

No changes. Your infrastructure matches the configuration.
このスクラップは2023/06/17にクローズされました