Skip to content

Commit f7c5f34

Browse files
committed
fix: support Amazon Linux 2023 for NAT instance
CDK's NatInstanceProviderV2 uses the `route` command in its default user data, which requires the net-tools package. However, Amazon Linux 2023 (the default AMI for NatInstanceProviderV2) doesn't have net-tools pre-installed, causing NAT instances to fail silently. This change provides custom user data that uses `ip route` instead of `route` to determine the default network interface, ensuring NAT functionality works correctly on AL2023. Reference: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ec2/lib/nat.ts
1 parent 74a5642 commit f7c5f34

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

cdk/lib/main-stack.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Construct } from 'constructs';
44
import { AsyncJob } from './constructs/async-job';
55
import { Auth } from './constructs/auth/';
66
import { Database } from './constructs/database';
7-
import { InstanceClass, InstanceSize, InstanceType, NatProvider, Vpc } from 'aws-cdk-lib/aws-ec2';
7+
import { InstanceClass, InstanceSize, InstanceType, NatProvider, UserData, Vpc } from 'aws-cdk-lib/aws-ec2';
88
import { HostedZone } from 'aws-cdk-lib/aws-route53';
99
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
1010
import { Webapp } from './constructs/webapp';
@@ -56,12 +56,29 @@ export class MainStack extends Stack {
5656
autoDeleteObjects: true,
5757
});
5858

59+
// Custom user data for NAT instance to support Amazon Linux 2023.
60+
// CDK's default user data uses `route` command which requires net-tools package,
61+
// but AL2023 doesn't have net-tools pre-installed. We use `ip route` instead.
62+
const natUserData = UserData.forLinux();
63+
natUserData.addCommands(
64+
'yum install iptables-services -y',
65+
'systemctl enable iptables',
66+
'systemctl start iptables',
67+
'echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/custom-ip-forwarding.conf',
68+
'sysctl -p /etc/sysctl.d/custom-ip-forwarding.conf',
69+
"IFACE=$(ip route show default | awk '{print $5}')",
70+
'/sbin/iptables -t nat -A POSTROUTING -o $IFACE -j MASQUERADE',
71+
'/sbin/iptables -F FORWARD',
72+
'service iptables save',
73+
);
74+
5975
const vpc = new Vpc(this, `Vpc`, {
6076
...(useNatInstance
6177
? {
6278
natGatewayProvider: NatProvider.instanceV2({
6379
instanceType: InstanceType.of(InstanceClass.T4G, InstanceSize.NANO),
6480
associatePublicIpAddress: true,
81+
userData: natUserData,
6582
}),
6683
natGateways: 1,
6784
}

cdk/test/__snapshots__/serverless-fullstack-webapp-starter-kit-without-domain.test.ts.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,10 +2716,11 @@ yum install iptables-services -y
27162716
systemctl enable iptables
27172717
systemctl start iptables
27182718
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/custom-ip-forwarding.conf
2719-
sudo sysctl -p /etc/sysctl.d/custom-ip-forwarding.conf
2720-
sudo /sbin/iptables -t nat -A POSTROUTING -o $(route | awk '/^default/{print $NF}') -j MASQUERADE
2721-
sudo /sbin/iptables -F FORWARD
2722-
sudo service iptables save",
2719+
sysctl -p /etc/sysctl.d/custom-ip-forwarding.conf
2720+
IFACE=$(ip route show default | awk '{print $5}')
2721+
/sbin/iptables -t nat -A POSTROUTING -o $IFACE -j MASQUERADE
2722+
/sbin/iptables -F FORWARD
2723+
service iptables save",
27232724
},
27242725
},
27252726
"Type": "AWS::EC2::Instance",

cdk/test/__snapshots__/serverless-fullstack-webapp-starter-kit.test.ts.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,10 +2548,11 @@ yum install iptables-services -y
25482548
systemctl enable iptables
25492549
systemctl start iptables
25502550
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/custom-ip-forwarding.conf
2551-
sudo sysctl -p /etc/sysctl.d/custom-ip-forwarding.conf
2552-
sudo /sbin/iptables -t nat -A POSTROUTING -o $(route | awk '/^default/{print $NF}') -j MASQUERADE
2553-
sudo /sbin/iptables -F FORWARD
2554-
sudo service iptables save",
2551+
sysctl -p /etc/sysctl.d/custom-ip-forwarding.conf
2552+
IFACE=$(ip route show default | awk '{print $5}')
2553+
/sbin/iptables -t nat -A POSTROUTING -o $IFACE -j MASQUERADE
2554+
/sbin/iptables -F FORWARD
2555+
service iptables save",
25552556
},
25562557
},
25572558
"Type": "AWS::EC2::Instance",

0 commit comments

Comments
 (0)