もりはやメモφ(・ω・ )

ITとか読書感想文とか

Terraform の random_id.xxx.b64 が random_id.xxx.b64_url になっていた

小ネタです。ある日、RDSと全く関係のないTerraformコードを追記したところ、意図しないところでCIがこけたので修正した話です。

最初に結論

  • Resources の random_idb64 を使って以下のエラーが出てしまった場合、単純に b64 --> b64_url とすれば差分なしで通るはずです。
Error: Unsupported attribute

  on aws_rds_postgres_hoge.tf line 20, in module "rds_postgress_digdag":
  20:   password = "${random_id.hoge.b64}"

This object has no argument, nested block, or exported attribute named "b64".

ついでに上記の場合は12系のための "皮むき" がされていないので以下にすべきでしょう。

#before
password = "${random_id.hoge.b64}"

#after
password = random_id.hoge.b64_url

調査

エラーを受けて、ちょっと調べると

Changelogに以下の記載がありました。今からほんの3ヶ月ほど前にバージョンが2系から3系へ上がり、ブレーキングな変更として b64 属性が廃止されたとあります。

3.0.0 (October 09, 2020)
Binary releases of this provider will now include the linux-arm64 platform.

BREAKING CHANGES:

Upgrade to version 2 of the Terraform Plugin SDK, which drops support for Terraform 0.11. This provider will continue to work as expected for users of Terraform 0.11, which will not download the new version. (#118)
Remove deprecated b64 attribute (#118)

さらにこのコミット provider/random: Add b64_std to random_idb64_urlb64_std が追加されていることがわかります。2016-11-07 とあるのでずいぶん前ですね。

同コミット内の以下のコメントの通り、 b64 の廃止と、 b64_url が 従来の b64 と同じ動作をするとあります。

provider/random: Add b64_std to random_id

This commit makes three related changes to the `random_id` resource:

1. Deprecate the `b64` attribute
2. Introduce a new `b64_url` attribute which functions in the same
   manner as the original `b64` attribute
3. Introduce a new `b64_std` attribute which uses standard base64
   encoding for the value rather than URL encoding.

Resource identifiers continue to use URL encoded base 64.

The reason for adding standard encoding of the base 64 value is to allow
the use of generated values as a Serf Encryption Key for separating
Consul clusters - these rely on standard encoding and do not permit some
characters which are allowed by URL encoding. `b64_url` is introduced
in order that there is consistency in specifying the desired encoding
during interpolation.

ここから、2016-11-07のコミットでやろうとしていた b64 の廃止を、2020-10-09 のバージョンアップでようやくやることができたことがわかります。歴史を感じれて面白いですね。(もちろん単純に放置してたのを思い出して実装しただけの可能性も大いにあります)


参考: random_id は RDS 作成のためのモジュール内で使ってます

最後に、参考程度に random_id を利用していたコードをぺたり*1

module "rds_mysql_morihayadb_dev" {
  source = "terraform-aws-modules/rds/aws"

  # RDS Identifer
  identifier = "morihayadb-dev"

  # DB Name
  name = "morihayadb"

  username = "morihaya"
  password = random_id.mysql.b64_url

  port              = "3306"
  apply_immediately = false

  subnet_ids = data.terraform_remote_state.vpc.outputs.vpc.private_subnets

  vpc_security_group_ids = [
    data.terraform_remote_state.vpc.outputs.security_group_to_mysql,
  ]

  engine               = "mysql"
  engine_version       = "5.7.23"
  family               = "mysql5.7"
  major_engine_version = "5.7"

  instance_class    = "db.t3.small"
  allocated_storage = "10"
  storage_encrypted = false

  allow_major_version_upgrade = false
  auto_minor_version_upgrade  = false
  deletion_protection         = true

  maintenance_window      = "Mon:12:00-Mon:13:00"
  backup_window           = "15:00-17:00"
  backup_retention_period = 7

  iam_database_authentication_enabled = false

  final_snapshot_identifier = "morihaya"
  skip_final_snapshot       = true

  monitoring_interval = 60
  monitoring_role_arn = data.aws_iam_role.rds_monitoring_role.arn

  enabled_cloudwatch_logs_exports = [
    "audit",
    "error",
    "general",
    "slowquery",
  ]

  copy_tags_to_snapshot = true

  tags = merge(
    data.terraform_remote_state.project.outputs.tags,
    {
      Env   = "dev"
      Group = "morihayadb"
    }
  )

  timeouts = {
    create = "40m"
    delete = "40m"
    update = "80m"
  }
}

*1:よくあるやつです