地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

【Terraform】dynamicのメモ

List

locals {
  empty_array = []

  environment = "${var.variables != null ? list(var.variables) : local.empty_array}"
}

resource "aws_lambda_function" "this" {
...
  dynamic "environment" {
    for_each = "${local.environment}"

    content {
      variables = "${environment.value}"
    }
  }
...
}

Map

locals {
  empty_map   = {}
 vpc_config_map = "${map(
    "subnet_ids", var.subnet_ids,
    "security_group_ids", var.security_group_ids
  )}"

  vpc_config = "${var.subnet_ids != null ? local.vpc_config_map : local.empty_map}"
}

resource "aws_lambda_function" "this" {
...
  dynamic "vpc_config" {
    for_each = "${local.vpc_config}"

    content {
      subnet_ids         = "${vpc_config.subnet_ids}"
      security_group_ids = "${vpc_config.security_group_ids}"
    }
  }
...
}