amazon-web-servicesterraformamazon-ecsterraform-provider-awsaws-cloudmap

Error creating ECS Service with DNS SRV records using Terraform


I need to create DNS entries for each running container/task in AWS ECS so I think SRV records would work. However Terraform (1.2.4 with provider hashicorp/aws v4.22.0) errors with:

│ Error: failed creating ECS service (ecs-service-xxx): InvalidParameterException: When specifying a value for serviceRegistries that is configured to use a type 'SRV' DNS record, you must also enter a value for either 'port' or the 'containerName' and 'containerPort' combination, but not both. Registry: arn:aws:servicediscovery:us-west-1:nnnnnnnnn:service/srv-xxxxxxxxxxx

This is my code:

resource "aws_service_discovery_service" "service-discovery-service" {
  name = "service-discovery-service-${var.environmentName}"

  dns_config {
    namespace_id = aws_service_discovery_private_dns_namespace.service-discovery-private-dns-namespace.id

    dns_records {
      ttl  = 10
      type = "SRV"
    }

    routing_policy = "MULTIVALUE"
  }

  health_check_custom_config {
    failure_threshold = 1
  }
}

This is the Terraform documentation: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/service_discovery_service#dns_records

I tried just putting containerName and containerPort fields in dns_records but those arguments do not seem to be supported.

Any ideas?


Solution

  • You have to specify those settings in the service_registries section of your aws_ecs_service resource.