Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ec2): NatInstanceProviderV2 security group example #29769

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@
"MyVpcNatSecurityGroupAA76397E",
"GroupId"
]
},
{
"Fn::GetAtt": [
"SecurityGroupDD263621",
"GroupId"
]
}
],
"SourceDestCheck": false,
Expand Down Expand Up @@ -566,6 +572,12 @@
"MyVpcNatSecurityGroupAA76397E",
"GroupId"
]
},
{
"Fn::GetAtt": [
"SecurityGroupDD263621",
"GroupId"
]
}
],
"SourceDestCheck": false,
Expand Down Expand Up @@ -763,9 +775,11 @@
"GroupDescription": "Security Group for NAT instances",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
"CidrIp": "255.255.255.255/32",
"Description": "Disallow all traffic",
"FromPort": 252,
"IpProtocol": "icmp",
"ToPort": 86
}
],
"Tags": [
Expand All @@ -779,6 +793,24 @@
}
}
},
"SecurityGroupDD263621": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "aws-cdk-vpc-nat-instance-v2-custom/SecurityGroup",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow egress to S3",
"FromPort": 443,
"IpProtocol": "tcp",
"ToPort": 443
}
],
"VpcId": {
"Ref": "MyVpcF9F0CA6F"
}
}
},
"ALBAEE750D2": {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ class NatInstanceStack extends cdk.Stack {
const natGatewayProvider = ec2.NatProvider.instanceV2({
instanceType: new ec2.InstanceType('t3.small'),
creditSpecification: ec2.CpuCredits.UNLIMITED,
defaultAllowedTraffic: ec2.NatTrafficDirection.OUTBOUND_ONLY,
defaultAllowedTraffic: ec2.NatTrafficDirection.NONE,
keyPair,
userData,
});

const vpc = new ec2.Vpc(this, 'MyVpc', {
natGatewayProvider,
natGateways: 2,
});
const vpc = new ec2.Vpc(this, 'MyVpc', { natGatewayProvider });

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
vpc,
allowAllOutbound: false,
});
securityGroup.addEgressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443), 'Allow egress to S3');
for (const gateway of natGatewayProvider.gatewayInstances) {
bucket.grantWrite(gateway);
gateway.addSecurityGroup(securityGroup);
}

Array.isArray(vpc);
Expand All @@ -70,7 +73,6 @@ const stack = new NatInstanceStack(app, 'aws-cdk-vpc-nat-instance-v2-custom');

const integ = new IntegTest(app, 'nat-instance-v2-custom-integ-test', {
testCases: [stack],

});

integ.assertions.httpApiCall(stack.apiUrl, {})
Expand Down
9 changes: 7 additions & 2 deletions packages/aws-cdk-lib/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ new ec2.Vpc(this, 'TheVPC', {
provider.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/8'), ec2.Port.tcp(80));
```

You can also customize the characteristics of your NAT instances, as well as their initialization scripts:
You can also customize the characteristics of your NAT instances, including their security group,
as well as their initialization scripts:

```ts
declare const bucket: s3.Bucket;
Expand All @@ -233,15 +234,19 @@ userData.addCommands(
const provider = ec2.NatProvider.instanceV2({
instanceType: new ec2.InstanceType('t3.small'),
creditSpecification: ec2.CpuCredits.UNLIMITED,
defaultAllowedTraffic: ec2.NatTrafficDirection.NONE,
});

new ec2.Vpc(this, 'TheVPC', {
const vpc = new ec2.Vpc(this, 'TheVPC', {
natGatewayProvider: provider,
natGateways: 2,
});

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { vpc });
securityGroup.addEgressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443));
for (const gateway of provider.gatewayInstances) {
bucket.grantWrite(gateway);
gateway.addSecurityGroup(securityGroup);
}
```

Expand Down
Loading