aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

(aws-ecs): Improve confusing `Connections` documentation/implementation

Open matsaune opened this issue 5 years ago • 8 comments

What is the problem?

Hi, I am using a ecsPatterns.NetworkLoadBalancedFargateService and tried to add a SecurityGroup in order to allow connections from NLB to fargate service. However this sg was not added as I expected to the fargateService.

Reproduction Steps

Define an ecsPatterns.NetworkLoadBalancedFargateService and add the following:

const securityGroupNotAdded = new ec2.SecurityGroup(this,"NblSecurityGroupNotAddedToService", { vpc: coreVpc, description: "Security group for connections from NLB to FargateService" }); securityGroupNotAdded.addIngressRule(ec2.Peer.ipv4(coreVpc.vpcCidrBlock),ec2.Port.tcp(80),"Allow from anyone in VPC on 80"); /** Somehow this does not get added. Seems like ony one sg allowed for fg service **/ loadBalancedFargateService.service.connections.addSecurityGroup(securityGroupNotAdded);

What did you expect to happen?

I expected this to be added to the SecurityGroups [] of the NetworkConfiguration of the Fargate service in order to allow connections from NLB.

What actually happened?

The sg is defined in cloudformation template but not registered in the SecurityGroups [] of the NetworkConfiguration of the Fargate service.

If this is a bug or a feature I do not know, but if feature and only allowed with one sg on fargate service it should be documented

CDK CLI Version

1.127.0 (build 0ea309a)

Framework Version

No response

Node.js Version

v16.4.2

OS

macOS 12.0.1 (21A559)

Language

Typescript

Language Version

Typescript Version 3.9.10

Other information

The workaround I use is as follows:

const securityGroup = loadBalancedFargateService.service.connections.securityGroups[0]; securityGroup.addIngressRule(ec2.Peer.ipv4(coreVpc.vpcCidrBlock),ec2.Port.tcp(80),"Allow from anyone in VPC on 80");

matsaune avatar Nov 02 '21 08:11 matsaune

This isn't the first time I've seen this bug report - check out #16117

I'm not sure how the connections object is intended to be used for this use case, so my theory posted in the original issue could be incorrect. Either way, this issue is confusing and our docs should be more clear if this isn't just a bug.

@madeline-k could you take a look at this?

peterwoodworth avatar Nov 02 '21 23:11 peterwoodworth

Another more elegant workaround: loadBalancedFargateService.service.connections.allowFrom( ec2.Peer.ipv4(coreVpc.vpcCidrBlock), ec2.Port.tcp(8080), ) Due to company policies (probably applies to more than us) we are not allowed to have open cidr blocks so must specify this.

matsaune avatar Nov 07 '21 09:11 matsaune

@peterwoodworth, I agree with what you said here. And this is the same scenario, just with a different construct. But I also agree this is confusing and we could consider either changing how the connections objects work on all constructs or changing the documentation.

@matsaune Thanks for opening this issue and providing the workaround!

madeline-k avatar Jan 13 '22 00:01 madeline-k

Oh, and this is happening with the underlying FargateService construct the is created by the NetworkLoadBalancedFargateServicePattern. So the same issue will happen with both.

madeline-k avatar Jan 13 '22 00:01 madeline-k

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jan 13 '23 04:01 github-actions[bot]

This is still an issue.

neilferreira avatar Feb 16 '23 07:02 neilferreira

Hi @matsaune,

Thanks for opening this issue! After reviewing the details, we found some other discussions that may be duplicates or closely related:

Potential Duplicate Issues

  • #5635: Both issues report the same problem where the connections.addSecurityGroup() method fails to add security groups to the ECS Fargate service's NetworkConfiguration. While the current issue uses NetworkLoadBalancedFargateService, issue #5635 experiences the same behavior with ApplicationLoadBalancedFargateService. Both share the same underlying implementation issue where security groups defined through the connections API are not properly propagated to the actual Fargate service configuration.

Related Issues

  • #7373: This issue describes the broader architectural problem where implementations of ec2.IConnectable are disconnected from their underlying AWS resources. The current issue is a specific manifestation of this problem, where the connections object for a Fargate service doesn't properly modify the service's actual security group configuration when using addSecurityGroup(). Both issues stem from the same disconnect between the connections API and resource configuration.

  • #16117: This issue documents the same connections.addSecurityGroup() method failing in a different context - with InterfaceVpcEndpoint rather than ECS Fargate services. Both issues show the same pattern where security groups are defined but not actually added to the underlying AWS resource configuration, suggesting a systematic problem with how the connections API interacts with certain resource types.

This message was generated automatically to help connect related conversations and improve discoverability.

If you feel this issue brings new or distinct information, feel free to add a comment to keep it open. Otherwise, we'll close this issue in 7 days if we don't recieve a response to help keep discussions consolidated.

Please react with 👍 or 👎 to let us know if this response was helpful!

Thank you for helping improve CDK! 🙌

maintainer-for-aws avatar Aug 11 '25 16:08 maintainer-for-aws

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Aug 13 '25 20:08 github-actions[bot]