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 3 years have passed since last update.

terraformでfor_eachを使う

Last updated at Posted at 2021-05-19

#for_eachを使わない場合
terraformから複数のssmパラメータをデプロイする場合、for_eachを使わない場合は下記のように複数のresourceブロックを書く必要があります。

ssm.tf
resource "aws_ssm_parameter" "parameter1" {
  name  = "key1"
  type  = "String"
  value = "value1"
}


resource "aws_ssm_parameter" "parameter2" {
  name  = "key2"
  type  = "String"
  value = "value2"
}

resource "aws_ssm_parameter" "parameter3" {
  name  = "key3"
  type  = "String"
  value = "value3"
}

この方法だとパラメータの数が多くなると見通しが悪くなってしまいます。

#for_eachを使って見通しを良くしよう
ここで、このようにfor_eachとローカルブロックを使用することで見通しをよくすることができます。

ssm.tf

resource "aws_ssm_parameter" "parameter_for_ecs" {
  for_each=local.parameters
  name  = each.key
  type  = "String"
  value = each.value
}

locals {
  parameters={
    "key1":"value1",
    "key2":"value2",
    "key3":"value3"
  }
}
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?