CDK: API Gateway to ECS via Service Connect

0

Using CDK, I am trying to configure an HTTP API Gateway integration that will proxy traffic to an ECS Service using Cloud Map and Service Connect. I need to specify the ARN of a service discovery service in my API Gateway integration. I cannot, for the life of me, determine how to dynamically resolve the ARN of a Service Discovery Service that has been created and managed by ECS via Service Connect.

I have tried using the L1 ECS Service and Service Discovery Service constructs to no avail.

I am using an HTTP Cloud Map Namespace and switching to a Private DNS Namespace is not an option for me at this time. If I create a Service Discover Service with an L1 construct and attempt to register an instance using the serviceRegistries property on the ECS Service, I can not specify a port; Cloud Formation will throw:

"Invalid request provided: UpdateService error: The values specified for serviceRegistries do not require a value for 'Port'. Remove the value and retry”

If I remove the port, traffic will not flow from API Gateway to the ECS Service. If I manually register a Service Instance via the console, I can specify a port, and traffic flows correctly.

In summary,

  • If I create the Cloud Map Service directly via CDK/CloudFormation, I can not specify a port
  • If I create the Cloud Map Service indirectly via ECS Service Connect configuration, I cannot access the ARN of the Service using CDK

What I am trying to achieve is definitely possible via the Console but I am struggling via CDK.

1 Answer
-1

When you create the Cloud Map service directly using CDK or CloudFormation, you cannot specify the port for the service. This is because the Cloud Map service is designed to be a general-purpose service discovery mechanism, and it does not have the concept of a specific port associated with the registered service. You can specify port while you are registering an instance with the service created. For more information on how to specify port while registering an instance in your service refer the below document. [+] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_servicediscovery-readme.html

When you create the Cloud Map service indirectly by integrating it with an ECS configuration, you are correct that you cannot directly access the ARN of the Cloud Map service using CDK. This is because the Cloud Map service is created as an implementation detail of the ECS service, and its ARN is not exposed as a separate output. You can use the aws_ecs.CfnService.ServiceRegistryProperty class in CDK. This class provides access to the registryArn property, which you can use to retrieve the ARN of the associated Cloud Map service. [+] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.CfnService.ServiceRegistryProperty.html

answered 2 months ago
  • "... it does not have the concept of a specific port associated with the registered service. You can specify port while you are registering an instance with the service created" - Correct, I am attempting to specify a port when registering an instance as I said: " If I create a Service Discover Service with an L1 construct and attempt to register an instance using the serviceRegistries property on the ECS Service, I can not specify a port"

    In regards to the aws_ecs.CfnService.ServiceRegistryProperty Interface, can you give me an example of usage? I can't quite figure out how it will be useful in my case.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions