|
180 | 180 | RuleAction rule['action'] || 'allow'
|
181 | 181 | Egress rule['egress'] || false
|
182 | 182 | CidrBlock cidr
|
183 |
| - unless rule.has_key?('protocol') && rule['protocol'].to_s == '-1' |
| 183 | + unless rule.has_key?('protocol') && ((rule['protocol'].to_s == '-1' || rule['protocol'].to_s == '1')) |
184 | 184 | PortRange ({ From: rule['from'], To: rule['to'] || rule['from'] })
|
185 | 185 | end
|
| 186 | + if rule.has_key?('icmp') |
| 187 | + Icmp({ |
| 188 | + 'Type' => 3, # Destination Unreachable |
| 189 | + 'Code' => 4 # Fragmentation Needed |
| 190 | + }) |
| 191 | + end |
186 | 192 | }
|
187 | 193 | end
|
188 | 194 | end
|
|
435 | 441 | NetworkInterfaceId Ref("NetworkInterface#{az}")
|
436 | 442 | }
|
437 | 443 |
|
438 |
| - nat_userdata = <<~USERDATA |
439 |
| - #!/bin/bash |
440 |
| - INSTANCE_ID=$(curl http://169.254.169.254/2014-11-05/meta-data/instance-id -s) |
441 |
| - aws ec2 attach-network-interface --instance-id $INSTANCE_ID --network-interface-id ${NetworkInterface#{az}} --device-index 1 --region ${AWS::Region} |
442 |
| - /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchTemplate#{az} --region ${AWS::Region} |
443 |
| - systemctl disable postfix |
444 |
| - systemctl stop postfix |
445 |
| - systemctl enable snat |
446 |
| - systemctl start snat |
447 |
| - USERDATA |
448 |
| - |
| 444 | + if external_parameters[:nat_2023] |
| 445 | + nat_userdata = <<~USERDATA |
| 446 | + #!/bin/bash |
| 447 | + # Fetch the metadata token for IMDSv2 |
| 448 | + TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") |
| 449 | + INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/2014-11-05/meta-data/instance-id -s) |
| 450 | + aws ec2 modify-network-interface-attribute --network-interface-id ${NetworkInterface#{az}} --no-source-dest-check --region ${AWS::Region} |
| 451 | + /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchTemplate#{az} --region ${AWS::Region} |
| 452 | + dnf -y install iptables iptables-utils iptables-services amazon-ssm-agent cronie |
| 453 | + systemctl enable amazon-ssm-agent |
| 454 | + systemctl start amazon-ssm-agent |
| 455 | + sysctl -w net.ipv4.ip_forward=1 |
| 456 | + sysctl -w net.ipv4.conf.ens5.rp_filter=0 |
| 457 | + echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf |
| 458 | + echo net.ipv4.conf.ens5.rp_filter = 0 >> /etc/sysctl.conf |
| 459 | + iptables -t nat -A POSTROUTING -s ${CIDR} -o ens5 -j MASQUERADE |
| 460 | + iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
| 461 | + iptables-save > /etc/sysconfig/iptables |
| 462 | + echo "@reboot root aws ec2 modify-network-interface-attribute --network-interface-id ${NetworkInterface#{az}} --no-source-dest-check --region ${AWS::Region}" >> /etc/crontab |
| 463 | + systemctl enable crond --now |
| 464 | + systemctl enable iptables --now |
| 465 | + |
| 466 | + USERDATA |
| 467 | + else |
| 468 | + nat_userdata = <<~USERDATA |
| 469 | + #!/bin/bash |
| 470 | + INSTANCE_ID=$(curl http://169.254.169.254/2014-11-05/meta-data/instance-id -s) |
| 471 | + aws ec2 attach-network-interface --instance-id $INSTANCE_ID --network-interface-id ${NetworkInterface#{az}} --device-index 1 --region ${AWS::Region} |
| 472 | + /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchTemplate#{az} --region ${AWS::Region} |
| 473 | + systemctl disable postfix |
| 474 | + systemctl stop postfix |
| 475 | + systemctl enable snat |
| 476 | + systemctl start snat |
| 477 | + USERDATA |
| 478 | + end |
| 479 | + network_interfaces = { |
| 480 | + DeviceIndex: 0, |
| 481 | + AssociatePublicIpAddress: true, |
| 482 | + Groups: [ Ref(:NatInstanceSecurityGroup) ] |
| 483 | + } |
| 484 | + if external_parameters[:nat_2023] |
| 485 | + network_interfaces = { |
| 486 | + NetworkInterfaceId: Ref("NetworkInterface#{az}"), |
| 487 | + DeviceIndex: 0 |
| 488 | + } |
| 489 | + end |
449 | 490 | template_data = {
|
450 | 491 | TagSpecifications: [
|
451 | 492 | { ResourceType: 'instance', Tags: nat_tags },
|
|
455 | 496 | InstanceType: Ref(:NatInstanceType),
|
456 | 497 | UserData: FnBase64(FnSub(nat_userdata)),
|
457 | 498 | IamInstanceProfile: { Name: Ref(:NatInstanceProfile) },
|
458 |
| - NetworkInterfaces: [{ |
459 |
| - DeviceIndex: 0, |
460 |
| - AssociatePublicIpAddress: true, |
461 |
| - Groups: [ Ref(:NatInstanceSecurityGroup) ] |
462 |
| - }] |
| 499 | + NetworkInterfaces: [network_interfaces] |
463 | 500 | }
|
464 | 501 |
|
465 | 502 | spot_options = {
|
|
546 | 583 | DesiredCapacity '1'
|
547 | 584 | MinSize '1'
|
548 | 585 | MaxSize '1'
|
549 |
| - VPCZoneIdentifier [Ref("SubnetPublic#{az}")] |
| 586 | + AvailabilityZones [FnSelect(az, FnGetAZs(Ref('AWS::Region')))] if external_parameters[:nat_2023] |
| 587 | + VPCZoneIdentifier [Ref("SubnetPublic#{az}")] unless external_parameters[:nat_2023] |
550 | 588 | LaunchTemplate({
|
551 | 589 | LaunchTemplateId: Ref("LaunchTemplate#{az}"),
|
552 | 590 | Version: FnGetAtt("LaunchTemplate#{az}", :LatestVersionNumber)
|
|
0 commit comments