The Magical City Behind the NAT Gateway: An AWS Adventure
Published: 2023-07-12 22:39:42

This article is inspired by a brilliant [article](https://start.jcolemorrison.com/aws-vpc-core-concepts-analogy-guide/) I came across a few years ago. Since then, it has become a cornerstone of my recommendations, as it beautifully illustrates the core [AWS Virtual Private Cloud (VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) concepts.



Each project's infrastructure functions like a living organism — constantly evolving, expanding, and adapting to accommodate distinct needs throughout its lifecycle. This dynamic character of project infrastructure represents a reality we often overlook.

This post delves into a [Network Address Translation (NAT) gateway](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html) and attempts to illustrate various infrastructure configurations across projects of varying scales. To help illustrate this concept, we will use a whimsical analogy of a magical city.

## What is a NAT gateway?

In simple terms, a NAT gateway is a service that helps your resources in a private subnet of your VPC communicate with the internet.

When you create a VPC in AWS, you can have both public and private subnets. The public subnets can directly access the internet, but the private subnets are isolated and can't directly connect to the internet. This is where the NAT Gateway comes into play.

## The City

Imagine a vast, bustling, and magical city. This city is akin to your private subnets in AWS. This city's buildings, parks, and roads represent your EC2 instances, database services, application services, and other resources you're running within your subnet. Each distinct area in the city is like a different availability zone. Some zones may be residential, some commercial, some industrial, and others purely magical – in AWS, these could represent different application tiers or functions like web servers, application servers, and databases.

For sure, we want to protect our magical city from unwanted visitors - those from the outer world who may mean harm or disruption. This is where the castle walls with magical portals to the external world or the NAT gateway come into the picture. These walls and portals prevent strangers (unsolicited inbound traffic) from entering the city directly while allowing the residents (private subnet resources) to go outside and return safely (outbound internet access). It ensures only authorized entities can get in, like knights returning from a quest.

Certainly, as our city's architects and overseers, predicting its growth and expansion will be an enthralling journey. Like a living, breathing entity, the city will evolve, with new districts emerging, the city walls expanding, and the population growing. We'll face unexpected challenges. Let's gear up for this adventure, because with every sunrise, our magical city holds a promise of novel experiences and learning opportunities. I bet it's going to be an exciting ride, don't you?

## Village (Single NAT Gateway)

![Single NAT Gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcnka76fp5fm6f3o0bqo.png)

The whole city is surrounded by a wall and there is only one magical portal to the outside world. All the traffic from the different districts has to pass through this portal to reach the world beyond.

Using a single portal for your magical city can have its advantages:

- **Cost Saving:** Maintaining one portal is less expensive than having multiple ones. After all, each portal requires its own magic (or in real terms, more resources) to function and keep it safe, right?

- **Simplicity:** One portal, one spell. It's easier to manage and less complicated to maintain.

- **Security and Control:** A single point of entry and exit allows the city's guardians to effectively monitor, filter, and control what comes in and out of the city.

However, this one magical portal also carries potential risks:
- **A single point of failure:** What if the magic fails or the guardians are distracted by a dragon attack? Well, then nobody can get in or out of the city. 
- **Bottleneck:** If your city grows and more people need to use the portal, it could lead to delays and frustrated citizens.

This setup is ideal for new projects. It's essential to be aware of potential risks, but remember, this is likely the most cost-effective arrangement in the initial stages.

## Town (One NAT Gateway per availability zone)

![One NAT Gateway per availability zone](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2lccjcbzu7iy3n2370o.png)

Our city is flourishing and growing larger. We need to account for its ability to manage challenges like increased traffic or potential disruptions in any of the districts. Currently, each district has its own portal for reaching the outside world. This allows traffic from each area to flow easily through its specific gateway and access what lies beyond.

Here's why this might be a good idea:

- **Improved Redundancy and Resilience:** If one portal is under a troll attack, the other portals aren't affected and can keep functioning. This reduces the risk of the whole city getting cut off from the outside world.

- **High Availability:** With each district having its own portal, there's no worry if one portal goes down due to a curse or magical mishap. The other districts remain unaffected, and the citizens can continue their journey to and from the outside world.

- **Better Scalability:** With multiple portals, you effectively spread out the traffic, preventing any one portal from getting overcrowded. This helps avoid traffic jams and keeps everything moving smoothly.

However, just as with everything in magic and technology, there are trade-offs:

- **Increased Cost:** More portals mean more magic, and more magic means more cost. You'll need to factor this into your city's treasury.

- **Management Complexity:** Each portal needs its own team of wizards for maintenance and its own set of security spells, which means more things to manage and keep track of.

- **Security Challenges:** Having multiple access points requires diligent monitoring and security measures to prevent any dark forces from entering the city.

This stage effectively addresses the issues highlighted in the previous setup. Most projects maintain this arrangement for their entire lifespan. Although there's an additional cost involved, it successfully resolves all fundamental challenges. Therefore, it's a worthwhile stage for handling production workloads.

## City (One NAT Gateway per subnet)

![One NAT Gateway per subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wmiuvg508pdyj3gzxnj.png)

Here's the twist: every neighborhood has its own portal. That means all the traffic from such neighborhood can independently zip through its own portal to reach the world beyond.

Here's why having one NAT Gateway per subnet could be beneficial:

- **Performance:** Each neighborhood managing its own traffic through a dedicated portal can help prevent congestion and ensure smooth data flow.

- **Scalability:** As your city grows, new neighborhoods with their own portals can be added. This means your city's capacity to handle traffic can increase as needed.

- **Efficiency:** Each portal only handles the traffic for its neighborhood, which can increase efficiency as traffic patterns can be more predictable.

Yet, we're faced with similar trade-offs, but they bear even more significance now.

This stage could be particularly relevant for projects with heavy loads. It does offer performance enhancement. Here, your project is already up and running, and your primary goal is to ensure it operates as smoothly as possible. Your chief concern should be the quality of your service.

## Lessons Learned

To wrap things up, let's think back to our magic city, a city that, like our project, started small and had a single protective wall and one magical portal. But as the city grew, it needed more protective barriers and magical gateways. This is much like the evolution of a project's infrastructure, beginning with one NAT gateway and eventually scaling up to have individual NAT gateways for high-loaded subnets.

Our small, charming city once had only one wall and a magic portal that met everyone's needs. It's just like when a project is new; one NAT gateway can manage all the internet traffic for private subnets. But as our city got bigger and more people and areas needed to use the magic door, it became clear that one door just wouldn't be enough. This is where the idea of staying strong, or resilience, becomes important.

Imagine if the only magical portal in the city was blocked or failed for some reason. The whole city would be stuck, unable to move in or out. It's the same with a project. If the only NAT gateway in a project breaks down, it could cause big problems. So, having more magic doors, like having multiple NAT gateways, helps manage the increased flow of people and adds a layer of resilience. If one portal fails, there are other doors people can use. This is key to maintaining smooth operations and protecting the city, or project, from unexpected problems.

As city planners carefully choose where to put extra walls and doors, we must also be wise about our project setup. It might be easy to follow a simple guide and use one NAT gateway for the whole project. But, we should always make sure our infrastructure fits our current needs and consider possible risks.

I hope you enjoyed this journey with me. Feel free to share it, leave your thoughts, or ask questions if you found it helpful. Let's keep the conversation going. Until next time, happy coding!