Why can't my EC2 instance connect to the internet using an internet gateway?

Last updated: 2021-02-19

My Amazon Elastic Compute Cloud (Amazon EC2) instance has a public IP address, but can’t access the internet. How can I fix this?

Resolution

Verify that the instance meets all prerequisites

The instance must meet the following conditions:

Verify that the instance has a public IP address

If the instance doesn't have a public IP address, but has an internet gateway, then the instance isn't accessible outside of the virtual private cloud (VPC) that it resides in.

To allow the instance connectivity to the internet, allocate an Elastic IP address and associate it with the instance.

Or, enable the public IPv4 addressing attribute in your subnet. Enabling the IPv4 addressing attribute means that instances launched in the subnet are attributed public IP addresses at launch.

Verify that a firewall isn't blocking access

If the instance meets the preceding conditions and internet connectivity issues persist, then try the following:

1.FSPTest the accessibility of the site or location from a known working instance or device using the ping or curl tools.

2.FSPVerify that any firewall devices or software allow traffic over HTTP or HTTPs.

Run the following command to verify that there aren't rules blocking traffic:

$ sudo iptables -L
$ sudo iptables -L -t nat

If the preceding command indicates blocked traffic, remove the rule or add a rule allowing traffic for that specific port. In the following example, replace examplerule with the new rule and port 80 with your specific port number.

$ sudo iptables -D examplerule
$ sudo iptables -A INPUT -p tcp --destination-port 80 -j ACCEPT

Windows Server:

For Windows Server default firewalls, run the following command:

netsh advfirewall firewall show rule name=all

If the preceding command indicates blocked traffic, then remove the old rule, or add a new rule allowing traffic for that specific port. In the following example, replace port 80 with your specific port number.

netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80