Introduction
Back-End
EC2 instances like Application and Database servers are most often launched on
a Private subnet. As a recap, a Private subnet is a subnet that doesn’t have a
route to the Internet Gateway in its Route table. Besides, EC2 instances in the
Private subnet don’t have Elastic-IP address association. These two facts mean
that EC2 instances on the Private subnet don’t have Internet access. However,
these EC2 instances might still need occasional Internet access to get firmware
upgrades from the external source. We can use a NAT Gateway (NGW) for allowing
IPv4 Internet traffic from Private subnets to the Internet. When we launch an
NGW, we also need to allocate an Elastic-IP address (EIP) and associate it with
the NGW. This association works the same way as the EIP-to-EC2 association. It
creates a static NAT entry to IGW that translates NGW’s local subnet address to its associated
EIP. The NGW, in turn, is responsible for translating the source IP address
from the ingress traffic originated from the Private subnet to its local subnet
IP address. As an example, EC2 instance NWKT-EC2-Back-End sends packets towards
the Internet to NGW. When the NGW receives these packets, it rewrites the
source IP address 10.10.1.172 with its Public subnet IP address 10.10.0.195 and
forwards packets to the Internet gateway. IGW translates the source IP address
10.10.0.195 to EIP 18.132.96.95 (EIP associated with NGW). That means that the
source IP of data is rewritten twice, first by NGW and then by IGW.
Figure
4-1 illustrates our example NAT GW design and its configuration steps. As a
pretask, we launch an EC2 instance on the Private subnet 10.10.1.0/24 (1). We also
modify the existing Security Group (SG) to allow an Inbound/Outbound ICMP
traffic within VPC CIDR 10.10.0.0/16 (2). We also allow an SSH session
initiation from the 10.10.0.218/24. I’m using the same SG for both EC2
instances to keep things simple. Besides, both EC2 uses the same Key Pair.
Chapter 3 shows how to launch an EC2 instance and how we modify the SGs, and
that is why we go ahead straight to the NGW configuration.
When
we have done pre-tasks, we launch an NGW on the Public subnet (3). Then we allocate
an EIP and associate it with NGW (4). Next, we add a default route towards NGW
on the Private subnet Route Table (5).
The
last three steps are related to connectivity testing. First, verify Intra-VPC
IP connectivity using ICMP (6). Then we test the Internet connectivity (7). As
the last step, we can confirm that no route exists back to NWKT-EC-Backe-End
from the IGW. We are using an AWS Path Analyzer for that (8).
Note!
Our example doesn’t follow good design principles. AWS Availability Zones (AZ)
are restricted failure domains, which means that failure in one AZ doesn’t
affect the operation of other AZ. Now, if our NGW on AZ eu-west-2c fails, Internet traffic from the Private subnet on
eu-west2a fails. The proper design is to launch NGW on the AZ where
unidirectional egress Internet access is needed.
Figure 4-1: Example Topology.