aws / aws/aws-cdk

aws_ec2.SecurityGroup: Order of entries is not consistent

Open
#19,864 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-ec2 bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

I have a CDK stack which creates an aws_ec2.SecurityGroup() which is then attached to a aws_ec2.CfnInstance.

In other stacks, I create aws_elasticloadbalancingv2.ApplicationLoadBalancers which have the ec2 instance defined as a target.
These scripts add the target ports to the security group of the ec2 instance.

When different people do a 'cdk diff' the security group entries end up being shuffled around, leading to changes like this:
```plaintext
Resources
[~] AWS::EC2::SecurityGroup security_group_private_euc1-bigip1-ec2-instance_1 securitygroupprivateeuc1bigip1ec2instance1684D1D68
└─ [~] SecurityGroupIngress
└─ @@ -15,27 +15,34 @@
[ ] },
[ ] {
[ ] "CidrIp": "0.0.0.0/0",
[-] "Description": "from 0.0.0.0/0:12001-12001",
[-] "FromPort": 12001,
[+] "Description": "from 0.0.0.0/0:12006-12006",
[+] "FromPort": 12006,
[ ] "IpProtocol": "tcp",
[-] "ToPort": 12001
[+] "ToPort": 12006
[ ] },
[ ] {
[ ] "CidrIp": "0.0.0.0/0",
[-] "Description": "from 0.0.0.0/0:12006-12006",
[-] "FromPort": 12006,
[+] "Description": "from 0.0.0.0/0:12001-12001",
[+] "FromPort": 12001,
[ ] "IpProtocol": "tcp",
[-] "ToPort": 12006
[+] "ToPort": 12001
[ ] },
```

### Expected Behavior

As the order of security groups in EC2 doesn't make any difference I would expect CDK to either not care about this order as well, or sort the entries accordingly to have a consistent order.

### Current Behavior

The order of security group entries changes sometimes (mostly between different developers) causing a diff which effectively does nothing.

### Reproduction Steps

The issue should be clear from the description and as it's a race-condition there isn't really a way to consistently reproduce it.
It's part bug report part feature request I guess.

### Possible Solution

One way I could think of would be to sort the entries according to the tuple of all parameters.
This way the order is always consistent, but any change to an entry will still be shown as a difference.

### Additional Information/Context

_No response_

### CDK CLI Version

2.20.0 (build 738ef49)

### Framework Version

_No response_

### Node.js Version

v14.19.0

### OS

Ubuntu 20.04.3 LTS, Linux VIE-NOMPOS-MOB 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

### Language

Python

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the aws_ec2.SecurityGroup implementation and the synthesis path that produces AWS::EC2::SecurityGroup SecurityGroupIngress entries; use the reported CfnInstance and ApplicationLoadBalancer target setup to reproduce the behavior with cdk diff. Done means equivalent ingress entries have a deterministic order, while actual entry changes still appear in the diff.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.