0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Terraformでタグを一部のリソースのみ追加する方法

0
Posted at

概要

variableで変数を指定してすでに全リソースにタグが付与している状態だけど
一部のリソースだけ他社に監視をお願いするためにタグを新規に追加しないといけないことがあった
調べたところmerge()を使ったやり方は二つある
個人的には明示的に入れずに変数にしてからmerge()に入れた方がいいかも

方法

  • リソースブロックの中にタグを明示的に埋め込む

埋め込んだタグが変わらなければ以下を使う

resource "aws_s3_bucket" "hoge_bucket" {
  bucket = "hoge-dev-s3-bucket"
  tags   = merge(
    var.previous_tag, 
    {
      new_tag    = "hoge"
    }
}
  • variable.tfに入れてタグを追加する

埋め込んだタグが変わる可能性がある

また、複数のリソースでタグを入れる必要がある場合は以下を使う

variable.tf

variable "new_tag" {
  type = map(any)
  default = {
    new_tag  = "hoge"
  }
}

s3.tf

resource "aws_s3_bucket" "hoge_bucket" {
  bucket = "hoge-dev-s3-bucket"
  tags   = merge(var.previous_tag, var.new_tag)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?