내용으로 건너뛰기

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개 답변
-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

답변함 2년 전
  • "... 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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

관련 콘텐츠