Search code examples
amazon-web-servicesterraformamazon-ecsterraform-provider-aws

Use extraHosts block in Terraform for ECS


So I have an ecs-task-definition, and I want to add a value to the hosts file in my container. This can be done using the extraHosts block:

extraHosts: A list of hostnames and IP address mappings to append to the /etc/hosts file on the container list(string)

see here for more info.

I'm trying to get this extraHosts block running in the terraform-aws-ecs-task-definition module linked above. The block I have looks like this:

  extraHosts = [
    {
      "ipAddress": "x.x.x.x",
      "hostname": "mongo1"
    },
    {
      "ipAddress": "x.x.x.x",
      "hostname": "mongo2"
    },
  ]

But this syntax gets me:

The given value is not suitable for child module variable "extraHosts" defined
at
.terraform/modules/ecs-task-definition/terraform-aws-ecs-task-definition-2.0.1/variables.tf:66,1-22:
element 0: string required.

Has anyone used the extraHosts block in the past for their IaC? Can you please advise as to the correct syntax?


Solution

  • The module is asking for the wrong type. The module's extraHosts variable wants list(string) while you are trying to pass list(object(...))).

    There's already an issue at https://github.com/mongodb/terraform-aws-ecs-task-definition/issues/33 but switching the type to list(any) or, more precisely, list(object([ipAddress = string, hostname = string])) in the module should fix it.