Save iptables firewall rules on Amazon Linux 2

Solution 1:

A simple way to do this (which works at the time of writing, with an AMI image timestamped at 2020-05-20) is to enable the iptables service. This allows rules to be saved to the system configuration, and be applied every time the instance boots.

First, install the service and activate it:

yum install iptables-services -y
systemctl enable iptables
systemctl start iptables

Second, add whatever iptables rules you like.

For example:

# Enable NAT forwarding
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Then, whenever you change the iptables rules:

service iptables save

The currently-applied set of rules are saved to /etc/sysconfig/iptables, and are restored on every boot.