aws-ecs: Cluster.addAsgCapacityProvider doesn't add security groups to Cluster.connections like addAutoScalingGroup did
Describe the issue
The docs for cluster.addAutoScalingGroup point towards using cluster.addAsgCapacityProvider instead, as the former is deprecated, and gone in v2.
They are nearly direct replacements.
The obvious - but wrong - replacement I started with was as follows: Before:
myCluster.addAutoScalingGroup(myAsg);
After:
const myCapacityProvider = new ecs.AsgCapacityProvider(this, 'MyCapacityProvider', {
autoScalingGroup: myAsg,
});
myCluster.addAsgCapacityProvider(myCapacityProvider);
However, this resulted in surprise issues with security groups elsewhere in my code (some code assumed an ECS Service had at least one security group in .connections.securityGroups, but it had zero, so errored.).
Turns out this is because cluster.addAutoScalingGroup adds security groups from the ASG's connections into the ECS Cluster's connections, but cluster.addAsgCapacityProvider does not.
See https://github.com/aws/aws-cdk/blob/c4e07a65f9580d3e39d4077d872bee6ad17d8924/packages/%40aws-cdk/aws-ecs/lib/cluster.ts#L385
So, in my example above, I needed to add:
myCluster.connections.connections.addSecurityGroup(...myAsg.connections.securityGroups)
I don't really understand why this isn't done in configureAutoScalingGroup like all the other common setup is. I think it would be fine to add, as it won't break for someone who has already added the workaround like I have, as it seems that re-adding security groups to connections no-op (they don't get duplicated).
So I think one (or more) of the following could resolve this:
- the docs pointing from
addAutoScalingGrouptoaddAsgCapacityProvidershould highlight the ways in which they are different, and guide towards how to do a direct replacement. - the options that can be passed to
addAsgCapacityProvidershould allow setting aaddSecurityGroupsFromAsgConnectionsToClusterConnectionsor something, and when set totrue, this does add security groups to the clusters connections. Cluster.configureAutoScalingGroupshould callthis.connections.connections.addSecurityGroup(...autoScalingGroup.connections.securityGroups);
Other related issues and PRs:
- https://github.com/aws/aws-cdk/pull/15107 this appears to have been an attempt to fix the issue, but the PR stalled
- and the linked issue https://github.com/aws/aws-cdk/issues/14764
- https://github.com/aws/aws-cdk/issues/16260#issuecomment-937918710
- https://github.com/aws/aws-cdk/issues/15733
Links
addAutoScalingGroup: https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.Cluster.html#addwbrautowbrscalingwbrgroupautoscalinggroup-optionsspan-classapi-icon-api-icon-deprecated-titlethis-api-element-is-deprecated-its-use-is-not-recommended%EF%B8%8Fspan
cluster.addAsgCapacityProvider: https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-ecs.Cluster.html#addwbrasgwbrcapacitywbrproviderprovider-options
+1
+1
Hi @plumdog,
Thanks for opening this issue! After reviewing the details, we found some other discussions that may be duplicates or closely related:
Potential Duplicate Issues
- #14764: Both issues identify the same technical problem where cluster.addAsgCapacityProvider fails to add security groups from ASG connections to cluster connections, while the deprecated addAutoScalingGroup method did this properly. The current issue and #14764 propose the same solution of adding the line "cluster.connections.connections.addSecurityGroup(...autoScalingGroup.connections.securityGroups)" to restore the expected behavior. Both issues demonstrate the same connectivity problems that occur when migrating from the deprecated method to the recommended one, affecting the same @aws-cdk/aws-ecs module.
Related Issues
-
#16260: This issue is related as it also involves problems when migrating from deprecated addCapacity/addAutoScalingGroup methods to addAsgCapacityProvider in the AWS ECS module. While the current issue focuses specifically on security group connections not being properly transferred, #16260 addresses instance registration problems with the cluster. Both stem from the same root cause: the incomplete implementation of addAsgCapacityProvider which doesn't handle all functionality that was previously managed by the deprecated methods.
-
#15733: This issue is related as it also discusses challenges when migrating from addCapacity to addAsgCapacityProvider in the AWS ECS module. It identifies the same security group connections problem, but extends further to address additional deployment failures that can occur even after implementing the security group workaround. Both issues highlight gaps in the migration path from deprecated methods to the recommended approach, with the security group handling being a key component of both problems.
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! 🙌
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.
Since there is no linked PR, we need a deeper review before closing.
Closing as duplicate of https://github.com/aws/aws-cdk/issues/14764
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.