AwsIntermediate

Building Scalable VPC Architectures in AWS

Admin
12 min read

TL;DR

A comprehensive guide to designing and implementing scalable Virtual Private Cloud architectures in AWS.

---

TL;DR: This guide delves into the principles and practical implementation of designing highly available, secure, and performant VPC architectures in AWS. It covers multi-Availability Zone (AZ) strategies, network segmentation using subnets and route tables, robust security group and NACL configurations, effective VPC connectivity options, and cost-aware optimization techniques, with a focus on practical examples for experienced engineers.

---

Introduction

Designing a scalable and resilient network infrastructure within Amazon Web Services (AWS) Virtual Private Cloud (VPC) is foundational for any mission-critical application. This article provides an in-depth look at advanced VPC design patterns, emphasizing strategies for high availability, robust security, and efficient resource utilization. We will explore core components, common architectural patterns, and crucial considerations for scaling.

One VPC, three subnet tiers, two Availability Zones Internet via the Internet Gateway Public subnets load balancers · bastions Private subnets app servers · workers Isolated subnets databases · no internet NAT per AZ outbound only Route tables one per tier, per AZ SGs + NACLs stateful + stateless The tier decides the traffic: public in, private out via NAT, isolated never leaves.

Core VPC Components and Best Practices

A well-architected VPC hinges on the judicious configuration of its fundamental building blocks. Understanding their purpose and best practices is critical for achieving scalability and resilience.

ComponentPurposeBest Practice
VPCIsolated virtual networkDefine CIDR blocks thoughtfully to allow for future expansion and prevent overlap with on-premises or other VPCs. Consider IPv6 adoption.
SubnetsNetwork segmentation within a VPCDistribute subnets across multiple Availability Zones (AZs) to ensure high availability. Use specific subnet types (public, private, isolated) aligned with traffic flow and security needs.
Route TablesRules that determine where network traffic is directedMaintain separate route tables for public and private subnets. Associate route tables with specific subnets to enforce defined traffic paths.
Internet GatewayEnables communication between a VPC and the internetAttach a single Internet Gateway to the VPC. Route internet-bound traffic from public subnets to the Internet Gateway.
NAT GatewayAllows instances in private subnets to initiate outbound traffic to the internetDeploy a NAT Gateway in each AZ that contains private subnets requiring internet access. This ensures redundancy; if one AZ's NAT Gateway fails, others can still serve traffic.
Security GroupsStateful firewalls for instancesImplement the principle of least privilege. Open only necessary ports and protocols. Group instances with similar security requirements into the same Security Group.
Network ACLsStateless firewalls for subnetsUtilize NACLs as an additional layer of defense to control traffic at the subnet boundary. They can be used for broader subnet-level access control.
VPC EndpointsEnables private connections to AWS servicesUse Interface Endpoints (powered by PrivateLink) for private access to supported AWS services without traversing the public internet, reducing NAT Gateway costs and improving security.

Multi-AZ VPC Architecture for High Availability

A fundamental aspect of scalable VPC design is ensuring resilience against single points of failure. Deploying resources across multiple Availability Zones (AZs) within a region is the standard approach.

In this pattern:

Public Subnets: Host internet-facing resources like load balancers and bastion hosts. They have a route to the Internet Gateway.

Private Subnets: Host application servers and other internal resources that do not require direct internet inbound access but may need outbound connectivity. They are routed via NAT Gateways.

Database Subnets (Isolated): Host databases. They are configured to only allow inbound traffic from specific security groups (e.g., application tier) and have no direct route to the Internet Gateway or NAT Gateway. This provides the highest level of isolation.

Redundant NAT Gateways: A NAT Gateway is deployed in each AZ that hosts private subnets. Each NAT Gateway is associated with an Elastic IP address. Route tables for private subnets in a given AZ point to the NAT Gateway in that same AZ. This ensures that if one AZ experiences an outage, the NAT Gateways in other AZs can continue to serve outbound traffic for their respective private subnets, and instances in private subnets can failover to other AZs.

Example: Infrastructure as Code (Pulumi TypeScript)

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const provider = new aws.Provider("aws", { region: "us-east-1" });

// Core VPC configuration
const vpc = new aws.ec2.Vpc("scalable-vpc", {
    cidrBlock: "10.0.0.0/16",
    enableDnsHostnames: true,
    enableDnsSupport: true,
    tags: {
        Name: "ScalableVPC",
    },
}, { provider });

// Availability Zones
const availabilityZones = aws.getAvailabilityZones({}, { provider });
const azA = availabilityZones.then(zones => zones.names[0]);
const azB = availabilityZones.then(zones => zones.names[1]);

// --- Network Interfaces for NAT Gateways ---
const natGwEipA = new aws.ec2.Eip("nat-gw-eip-a", {
    vpc: true,
}, { provider });

const natGwA = new aws.ec2.NatGateway("nat-gw-a", {
    allocationId: natGwEipA.id,
    subnetId: aws.ec2.getPublicSubnetOutput({
        availabilityZone: azA,
        filters: [{ name: "vpc-id", values: [vpc.id] }],
    }).apply(subnet => subnet.id),
    tags: {
        Name: "NATGateway-AZ-A",
    },
    opts: { dependsOn: [vpc] },
}, { provider });

const natGwEipB = new aws.ec2.Eip("nat-gw-eip-b", {
    vpc: true,
}, { provider });

const natGwB = new aws.ec2.NatGateway("nat-gw-b", {
    allocationId: natGwEipB.id,
    subnetId: aws.ec2.getPublicSubnetOutput({
        availabilityZone: azB,
        filters: [{ name: "vpc-id", values: [vpc.id] }],
    }).apply(subnet => subnet.id),
    tags: {
        Name: "NATGateway-AZ-B",
    },
    opts: { dependsOn: [vpc] },
}, { provider });

// --- Subnets ---
const publicSubnetA = new aws.ec2.Subnet("public-subnet-a", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: azA,
    mapPublicIpOnLaunch: true,
    tags: {
        Name: "PublicSubnet-AZ-A",
    },
}, { provider });

const privateSubnetA = new aws.ec2.Subnet("private-subnet-a", {
    vpcId: vpc.id,
    cidrBlock: "10.0.2.0/24",
    availabilityZone: azA,
    tags: {
        Name: "PrivateSubnet-AZ-A",
    },
}, { provider });

const databaseSubnetA = new aws.ec2.Subnet("database-subnet-a", {
    vpcId: vpc.id,
    cidrBlock: "10.0.3.0/24",
    availabilityZone: azA,
    tags: {
        Name: "DatabaseSubnet-AZ-A",
    },
}, { provider });

const publicSubnetB = new aws.ec2.Subnet("public-subnet-b", {
    vpcId: vpc.id,
    cidrBlock: "10.0.4.0/24",
    availabilityZone: azB,
    mapPublicIpOnLaunch: true,
    tags: {
        Name: "PublicSubnet-AZ-B",
    },
}, { provider });

const privateSubnetB = new aws.ec2.Subnet("private-subnet-b", {
    vpcId: vpc.id,
    cidrBlock: "10.0.5.0/24",
    availabilityZone: azB,
    tags: {
        Name: "PrivateSubnet-AZ-B",
    },
}, { provider });

const databaseSubnetB = new aws.ec2.Subnet("database-subnet-b", {
    vpcId: vpc.id,
    cidrBlock: "10.0.6.0/24",
    availabilityZone: azB,
    tags: {
        Name: "DatabaseSubnet-AZ-B",
    },
}, { provider });

// --- Route Tables ---
const internetGateway = new aws.ec2.InternetGateway("igw", {
    vpcId: vpc.id,
    tags: {
        Name: "InternetGateway",
    },
}, { provider });

// Public Route Table for AZ A
const publicRtA = new aws.ec2.RouteTable("public-rt-a", {
    vpcId: vpc.id,
    routes: [
        {
            cidrBlock: "0.0.0.0/0",
            gatewayId: internetGateway.id,
        }
    ],
    tags: {
        Name: "PublicRouteTable-AZ-A",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("public-rta-assoc-a", {
    subnetId: publicSubnetA.id,
    routeTableId: publicRtA.id,
}, { provider });

// Private Route Table for AZ A
const privateRtA = new aws.ec2.RouteTable("private-rt-a", {
    vpcId: vpc.id,
    routes: [
        {
            cidrBlock: "0.0.0.0/0",
            natGatewayId: natGwA.id,
        }
    ],
    tags: {
        Name: "PrivateRouteTable-AZ-A",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("private-rta-assoc-a", {
    subnetId: privateSubnetA.id,
    routeTableId: privateRtA.id,
}, { provider });

// Database Route Table for AZ A (no internet access)
const databaseRtA = new aws.ec2.RouteTable("database-rt-a", {
    vpcId: vpc.id,
    routes: [
        // Typically no 0.0.0.0/0 route. If needed for specific services, use VPC Endpoints.
    ],
    tags: {
        Name: "DatabaseRouteTable-AZ-A",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("database-rta-assoc-a", {
    subnetId: databaseSubnetA.id,
    routeTableId: databaseRtA.id,
}, { provider });

// Public Route Table for AZ B
const publicRtB = new aws.ec2.RouteTable("public-rt-b", {
    vpcId: vpc.id,
    routes: [
        {
            cidrBlock: "0.0.0.0/0",
            gatewayId: internetGateway.id,
        }
    ],
    tags: {
        Name: "PublicRouteTable-AZ-B",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("public-rtb-assoc-b", {
    subnetId: publicSubnetB.id,
    routeTableId: publicRtB.id,
}, { provider });

// Private Route Table for AZ B
const privateRtB = new aws.ec2.RouteTable("private-rt-b", {
    vpcId: vpc.id,
    routes: [
        {
            cidrBlock: "0.0.0.0/0",
            natGatewayId: natGwB.id,
        }
    ],
    tags: {
        Name: "PrivateRouteTable-AZ-B",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("private-rtb-assoc-b", {
    subnetId: privateSubnetB.id,
    routeTableId: privateRtB.id,
}, { provider });

// Database Route Table for AZ B (no internet access)
const databaseRtB = new aws.ec2.RouteTable("database-rt-b", {
    vpcId: vpc.id,
    routes: [
        // Typically no 0.0.0.0/0 route.
    ],
    tags: {
        Name: "DatabaseRouteTable-AZ-B",
    },
}, { provider });
new aws.ec2.RouteTableAssociation("database-rtb-assoc-b", {
    subnetId: databaseSubnetB.id,
    routeTableId: databaseRtB.id,
}, { provider });

// --- Security Groups (Example for a 3-tier app) ---
const webSg = new aws.ec2.SecurityGroup("web-sg", {
    vpcId: vpc.id,
    description: "Allow HTTP and HTTPS inbound traffic",
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, // For public load balancers or direct access
        { protocol: "tcp", fromPort: 443, toPort: 443, cidrBlocks: ["0.0.0.0/0"] },
    ],
    egress: [{ protocol: "-1", cidrBlocks: ["0.0.0.0/0"] }], // Allow all outbound
    tags: { Name: "WebTierSG" },
}, { provider });

const appSg = new aws.ec2.SecurityGroup("app-sg", {
    vpcId: vpc.id,
    description: "Allow inbound traffic from web tier",
    ingress: [
        // Assuming app servers listen on port 8080
        { protocol: "tcp", fromPort: 8080, toPort: 8080, securityGroups: [webSg.id] },
    ],
    egress: [{ protocol: "-1", cidrBlocks: ["0.0.0.0/0"] }], // Allow all outbound (via NAT)
    tags: { Name: "AppTierSG" },
}, { provider });

const dbSg = new aws.ec2.SecurityGroup("db-sg", {
    vpcId: vpc.id,
    description: "Allow inbound traffic from app tier to database",
    ingress: [
        // Example for PostgreSQL on port 5432
        { protocol: "tcp", fromPort: 5432, toPort: 5432, securityGroups: [appSg.id] },
    ],
    egress: [{ protocol: "-1", cidrBlocks: ["0.0.0.0/0"] }], // Usually restricted, only to specific internal IPs if needed
    tags: { Name: "DatabaseTierSG" },
}, { provider });

// Export VPC ID for reference
export const vpcId = vpc.id;

Network Segmentation and Security

Effective network segmentation is crucial for limiting the blast radius of security incidents and maintaining compliance.

Subnet Design Patterns

Subnet TypePurposeSecurity ConsiderationsExample Use Cases
PublicInternet-facing resourcesStrict inbound rules (e.g., only for Load Balancers, bastion hosts). Outbound access via NAT Gateway if required.Application Load Balancers, Bastion Hosts, NAT Gateways.
PrivateInternal applications, backend servicesNo direct internet ingress. Outbound access to the internet via NAT Gateway for patching, updates, or external API calls.Web servers (behind a load balancer), application servers, microservices.
IsolatedData tier, sensitive internal servicesNo direct internet ingress or egress. Access only from specific trusted subnets/security groups. Ideal for databases and internal management consoles.RDS databases, ElastiCache, internal APIs, batch processing workers.

Defence in depth — every layer can say no, independently Internet Gateway — the one door in from the internet NACLs — stateless subnet boundary, rule order decides Route tables — public to IGW, private to NAT, isolated nowhere Security groups — stateful, least privilege, per instance VPC Endpoints — AWS services without the public internet No single layer carries security — a packet must pass all five to reach a workload.

Security Group and Network ACL Synergy

Security Groups (SGs): Act as stateful firewalls attached to instances (ENIs). They allow or deny traffic based on protocol, port, and source/destination IP address or Security Group ID. They are evaluated before traffic reaches the instance.

Network Access Control Lists (NACLs): Act as stateless firewalls attached to subnets. They control inbound and outbound traffic at the subnet boundary. NACLs are evaluated in order by rule number, and a request can be allowed by one rule and denied by another.

Best Practice:

Use Security Groups as the primary defense mechanism for instances, adhering to the principle of least privilege.

Use NACLs for broader, subnet-level control, such as blocking traffic from known malicious IP ranges or enforcing broad network policies. For example, you might use a NACL to deny all inbound traffic on a specific port to a subnet, even if its Security Groups permit it.

VPC Connectivity Options

Connecting your VPC to other networks is essential for hybrid cloud strategies and centralized management.

Pick the connection by what crosses the boundary What connects to the VPC? start from the other end Another VPC same org, few peers Many VPCs / on-prem hub-and-spoke scale One AWS service private, no internet VPC peering non-transitive Transit Gateway hub + VPN / Direct Connect PrivateLink endpoint interface endpoint Peering does not transit — the moment a third network joins, you need the hub.

Hybrid Connectivity Architecture

Key Components:

AWS Transit Gateway (TGW): A network transit hub that connects VPCs and on-premises networks. It simplifies network management by acting as a central point of routing.

AWS Site-to-Site VPN: Provides encrypted IPsec tunnels over the public internet to connect your on-premises network to your VPC. Suitable for moderate bandwidth needs and disaster recovery.

AWS Direct Connect: Offers dedicated, private network connections from your data center to AWS, providing higher bandwidth and more consistent network performance than VPN.

VPC Peering: Enables direct network connectivity between two VPCs. While useful for simple connections, it doesn't scale well for complex mesh networks and has transitive routing limitations. Transit Gateway is generally preferred for larger deployments.

Advantages of Transit Gateway:

Scalability: Connects thousands of VPCs and on-premises networks.

Centralized Management: Simplifies routing and network administration.

* Reduced Complexity: Eliminates the need for complex VPC peering meshes.

Cost Optimization Strategies

While scalability and resilience are paramount, managing costs is equally important.

NAT Gateway Optimization

NAT Gateways incur hourly charges and data processing fees. Optimizing their usage can lead to significant cost savings.

| Strategy | Implementation

Why This Matters

Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

Trade-offs to Consider

Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

When NOT to Use This

Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

Decision Framework

Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.

Share this articleShare on XShare on LinkedIn

Keep the decision frameworks coming

One practical architecture guide a week — patterns, trade-offs, and when not to use them. No spam, unsubscribe anytime.