-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ec2): security group lookup via filters (#30625)
### Issue # (if applicable) Closes #30331. ### Reason for this change This will improve the security group lookup functionality for importing existing security groups into a CDK stack. ### Description of changes I added the ability to lookup existing security groups via more filters. Filters are supported by the [DescribeSecurityGroups API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSecurityGroups.html), and using these filters can be immensely useful for looking up existing security groups, especially if your account or organization follows predictable rules regarding things like security group tags. ### Description of how you validated changes I added unit tests similar to the ones that test the normal lookup by ID or name. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
21 changed files
with
1,485 additions
and
26 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...k-integ/test/aws-ec2/test/integ.security-group-lookup.js.snapshot/LookupStack.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
...integ/test/aws-ec2/test/integ.security-group-lookup.js.snapshot/LookupStack.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"Parameters": { | ||
"BootstrapVersion": { | ||
"Type": "AWS::SSM::Parameter::Value<String>", | ||
"Default": "/cdk-bootstrap/hnb659fds/version", | ||
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" | ||
} | ||
}, | ||
"Rules": { | ||
"CheckBootstrapVersion": { | ||
"Assertions": [ | ||
{ | ||
"Assert": { | ||
"Fn::Not": [ | ||
{ | ||
"Fn::Contains": [ | ||
[ | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"5" | ||
], | ||
{ | ||
"Ref": "BootstrapVersion" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." | ||
} | ||
] | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...security-group-lookup.js.snapshot/SgLookupTestDefaultTestDeployAssert9466B7BF.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
...curity-group-lookup.js.snapshot/SgLookupTestDefaultTestDeployAssert9466B7BF.template.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
...k-integ/test/aws-ec2/test/integ.security-group-lookup.js.snapshot/StackWithSg.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
233 changes: 233 additions & 0 deletions
233
...integ/test/aws-ec2/test/integ.security-group-lookup.js.snapshot/StackWithSg.template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
{ | ||
"Resources": { | ||
"MyVpcF9F0CA6F": { | ||
"Type": "AWS::EC2::VPC", | ||
"Properties": { | ||
"CidrBlock": "10.0.0.0/16", | ||
"EnableDnsHostnames": true, | ||
"EnableDnsSupport": true, | ||
"InstanceTenancy": "default", | ||
"Tags": [ | ||
{ | ||
"Key": "Name", | ||
"Value": "my-vpc-name" | ||
} | ||
] | ||
} | ||
}, | ||
"MyVpcRestrictDefaultSecurityGroupCustomResourceA4FCCD62": { | ||
"Type": "Custom::VpcRestrictDefaultSG", | ||
"Properties": { | ||
"ServiceToken": { | ||
"Fn::GetAtt": [ | ||
"CustomVpcRestrictDefaultSGCustomResourceProviderHandlerDC833E5E", | ||
"Arn" | ||
] | ||
}, | ||
"DefaultSecurityGroupId": { | ||
"Fn::GetAtt": [ | ||
"MyVpcF9F0CA6F", | ||
"DefaultSecurityGroup" | ||
] | ||
}, | ||
"Account": "12345678" | ||
}, | ||
"UpdateReplacePolicy": "Delete", | ||
"DeletionPolicy": "Delete" | ||
}, | ||
"CustomVpcRestrictDefaultSGCustomResourceProviderRole26592FE0": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
} | ||
} | ||
] | ||
}, | ||
"ManagedPolicyArns": [ | ||
{ | ||
"Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
} | ||
], | ||
"Policies": [ | ||
{ | ||
"PolicyName": "Inline", | ||
"PolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"ec2:AuthorizeSecurityGroupIngress", | ||
"ec2:AuthorizeSecurityGroupEgress", | ||
"ec2:RevokeSecurityGroupIngress", | ||
"ec2:RevokeSecurityGroupEgress" | ||
], | ||
"Resource": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":ec2:test-region:12345678:security-group/", | ||
{ | ||
"Fn::GetAtt": [ | ||
"MyVpcF9F0CA6F", | ||
"DefaultSecurityGroup" | ||
] | ||
} | ||
] | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"CustomVpcRestrictDefaultSGCustomResourceProviderHandlerDC833E5E": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"S3Bucket": "cdk-hnb659fds-assets-12345678-test-region", | ||
"S3Key": "bde7b5c89cb43285f884c94f0b9e17cdb0f5eb5345005114dd60342e0b8a85a1.zip" | ||
}, | ||
"Timeout": 900, | ||
"MemorySize": 128, | ||
"Handler": "__entrypoint__.handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"CustomVpcRestrictDefaultSGCustomResourceProviderRole26592FE0", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "nodejs18.x", | ||
"Description": "Lambda function for removing all inbound/outbound rules from the VPC default security group" | ||
}, | ||
"DependsOn": [ | ||
"CustomVpcRestrictDefaultSGCustomResourceProviderRole26592FE0" | ||
] | ||
}, | ||
"MySgAFDC270F2": { | ||
"Type": "AWS::EC2::SecurityGroup", | ||
"Properties": { | ||
"GroupDescription": "StackWithSg/MySgA", | ||
"SecurityGroupEgress": [ | ||
{ | ||
"CidrIp": "0.0.0.0/0", | ||
"Description": "Allow all outbound traffic by default", | ||
"IpProtocol": "-1" | ||
} | ||
], | ||
"Tags": [ | ||
{ | ||
"Key": "myTag", | ||
"Value": "my-value" | ||
} | ||
], | ||
"VpcId": { | ||
"Ref": "MyVpcF9F0CA6F" | ||
} | ||
} | ||
}, | ||
"MySgB343D3C61": { | ||
"Type": "AWS::EC2::SecurityGroup", | ||
"Properties": { | ||
"GroupDescription": "StackWithSg/MySgB", | ||
"SecurityGroupEgress": [ | ||
{ | ||
"CidrIp": "0.0.0.0/0", | ||
"Description": "Allow all outbound traffic by default", | ||
"IpProtocol": "-1" | ||
} | ||
], | ||
"Tags": [ | ||
{ | ||
"Key": "myTagKey", | ||
"Value": "true" | ||
} | ||
], | ||
"VpcId": { | ||
"Ref": "MyVpcF9F0CA6F" | ||
} | ||
} | ||
}, | ||
"MySgC50C8732C": { | ||
"Type": "AWS::EC2::SecurityGroup", | ||
"Properties": { | ||
"GroupDescription": "my-description", | ||
"SecurityGroupEgress": [ | ||
{ | ||
"CidrIp": "0.0.0.0/0", | ||
"Description": "Allow all outbound traffic by default", | ||
"IpProtocol": "-1" | ||
} | ||
], | ||
"VpcId": { | ||
"Ref": "MyVpcF9F0CA6F" | ||
} | ||
} | ||
}, | ||
"MySgDA51BA0C2": { | ||
"Type": "AWS::EC2::SecurityGroup", | ||
"Properties": { | ||
"GroupDescription": "ownerId description", | ||
"SecurityGroupEgress": [ | ||
{ | ||
"CidrIp": "0.0.0.0/0", | ||
"Description": "Allow all outbound traffic by default", | ||
"IpProtocol": "-1" | ||
} | ||
], | ||
"VpcId": { | ||
"Ref": "MyVpcF9F0CA6F" | ||
} | ||
} | ||
} | ||
}, | ||
"Parameters": { | ||
"BootstrapVersion": { | ||
"Type": "AWS::SSM::Parameter::Value<String>", | ||
"Default": "/cdk-bootstrap/hnb659fds/version", | ||
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" | ||
} | ||
}, | ||
"Rules": { | ||
"CheckBootstrapVersion": { | ||
"Assertions": [ | ||
{ | ||
"Assert": { | ||
"Fn::Not": [ | ||
{ | ||
"Fn::Contains": [ | ||
[ | ||
"1", | ||
"2", | ||
"3", | ||
"4", | ||
"5" | ||
], | ||
{ | ||
"Ref": "BootstrapVersion" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.