aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.67k stars 3.92k forks source link

(servicediscovery): CloudMapOptions support for both A and SRV records #18894

Open davide-romanini-milkman opened 2 years ago

davide-romanini-milkman commented 2 years ago

General Issue

Cdk doesn't allow ECS service with both A and SRV dns records

The Question

According to AWS official documentation, under certain conditions you can create any combination of A or SRV records for each service task. I successfully tested this through the old aws console, creating a service that automatically registers both A and SRV records on the associated CloudMap private hosted zone. I also talked with AWS support and they suggested to open this issue.

This feature however seems to be completely forbidden by CDK: according to the documentation the only available option is either using A or SRV records, not both.

My question is: there's some special reason for this restriction? It seems useful for various reasons having both kind of records, so it seems strange to disallow that. Do you know if some workaround is available?

CDK CLI Version

1.102.0

Framework Version

No response

Node.js Version

No response

OS

No response

Language

Typescript

Language Version

No response

Other information

No response

peterwoodworth commented 2 years ago

This is configurable in CloudFormation under the AWS::ServiceDiscovery::Service.DnsConfig.DnsRecords, but not directly configurable by the CDK's higher level constructs.

I'm not sure how you're creating this service (it could be a child of an Ecs.Ec2Service for example), but you'll be able to access the service through escape hatches and modify the attribute I linked above directly

Relabelled as a feature request to directly support with Ecs.BaseService and ServiceDiscovery.Service classes

nacitar commented 2 years ago

Just to provide an actual example for other people that run into this (instead of just vague links to automatically generated documentation):

// NOTE: This snippet uses C# 9 features like the more flexible new()
// ASSUMPTION: The discoveryNamespace variable exists.
using ServiceDiscovery = Amazon.CDK.AWS.ServiceDiscovery;

ServiceDiscovery.Service discoveryService = new(this, "DiscoveryService", new ServiceDiscovery.ServiceProps
{
    RoutingPolicy = ServiceDiscovery.RoutingPolicy.MULTIVALUE,
    Name = "WHATEVER_YOU_WANT",
    Namespace = discoveryNamespace
});
// ARBITRARY BLOCK; You can't add multiple record types without utilizing the Cfn escape hatch
// https://github.com/aws/aws-cdk/issues/18894
{
    ServiceDiscovery.CfnService cfnDiscoveryService = discoveryService.Node.DefaultChild as ServiceDiscovery.CfnService
        ?? throw new Exception("If you see this, ServiceDiscovery.Service no longer wraps ServiceDiscovery.CfnService.");
    cfnDiscoveryService.DnsConfig = new DnsConfigProperty
    {
        DnsRecords = new DnsRecordProperty[] {
            new() {
                Ttl = 60,
                Type = "A"
            },
            new() {
                Ttl = 60,
                Type = "SRV"
            }
        }
    };
}

The arbitrary block is just to prevent a simple workaround from resulting in the cfnDiscoveryService variable polluting the enclosing scope.