terraformでリソース内の一部をループ処理させる

# forを使うといいみたい

## 呼び先のmoduleでループする部分
- forを使う
- for_eachやcountはリソースに対してのみなので今回の場合は使えない
- indexを使いたい場合は、forの中で`i`の宣言が必要
    - また、`i`だけだとだめで、この場合`e`も併せて宣言しておく必要がある（使わなくても）

```hcl
      environment = [
        for i,e in var.container_definitions[0].environment :
        {
          "name" : var.container_definitions[0].environment[i]["name"],
          "value" : var.container_definitions[0].environment[i]["value"]
        }
      ]
```

## moduleを呼ぶ部分
- environment部分が可変なので呼ばれるmoduleの中身も対応できるようにしたい

```
      container_port = 80
      host_port      = 80
      environment = [
        {
          name  = "DATABASE_URL"
          value = "mysql2://admin:!aaaa2@vamdemic-dev.cxxg"
        },
        {
          name  = "HOST"
          value = "stg.vamdemic.com"
        },
      ]
```

## variables
```hcl
variable "container_definitions" {
  type = list(object({
    name           = string
    image          = string
    cpu            = number
    memory         = number
    container_port = number
    host_port      = number
    environment    = list(map(string))
  }))
}
```

## 呼び先の全体

```hcl
resource "aws_ecs_task_definition" "default" {
  family                   = "${var.product}-${var.env}"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  cpu                      = "256"
  memory                   = "512"
  container_definitions = jsonencode([
    {
      name      = var.container_definitions[0].name
      image     = var.container_definitions[0].image
      cpu       = var.container_definitions[0].cpu
      memory    = var.container_definitions[0].memory
      essential = true
      environment = [
        for i,e in var.container_definitions[0].environment :
        {
          "name" : var.container_definitions[0].environment[i]["name"],
          "value" : var.container_definitions[0].environment[i]["value"]
        }
      ]

      portMappings = [
        {
          containerPort = var.container_definitions[0].container_port
          hostPort      = var.container_definitions[0].host_port
        }
      ]
    },
    {
      name      = var.container_definitions[1].name
      image     = var.container_definitions[1].image
      cpu       = var.container_definitions[1].cpu
      memory    = var.container_definitions[1].memory
      essential = true
      portMappings = [
        {
          containerPort = var.container_definitions[1].container_port
          hostPort      = var.container_definitions[1].host_port
        }
      ]
    }
  ])
  task_role_arn      = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/ecsTaskExecutionRole"
  execution_role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/ecsTaskExecutionRole"


data "aws_caller_identity" "current" {}

```

## 呼び元の全体

```hcl
module "ecs_task_definition" {
  source  = "./modules/ecs/task"
  product = local.product
  env     = local.env

  container_definitions = [
    {
      name           = "web"
      image          = module.ecr["web"].repository_url
      cpu            = 10
      memory         = 256
      container_port = 80
      host_port      = 80
      environment = [
        {
          name  = "DATABASE_URL"
          value = "mysql2://admin:!aaaa2@vamdemic-dev.cxxg"
        },
        {
          name  = "HOST"
          value = "stg.vamdemic.com"
        },
      ]
    },
    {
      name           = "api"
      image          = module.ecr["api"].repository_url
      cpu            = 10
      memory         = 256
      container_port = 3000
      host_port      = 3000
      environment = [
        {
          name  = "RAILS_SERVE_STATIC_FILES"
          value = "1"
        },
        {
          name  = "RAILE_STATIC_FILES"
          value = "2"
        }
      ]
    }
  ]
}
```