VPC design

Only the load balancer faces the internet

VPC 10.0.0.0/16 · one regionAvailability Zone aAvailability Zone binternetgatewaypublic 10.0.1.0/24ALB · NATpublic 10.0.2.0/24ALB · NATprivate app 10.0.11.0/24app serversprivate app 10.0.12.0/24app serversprivate data 10.0.21.0/24primaryprivate data 10.0.22.0/24standbyLayers an attacker must passedge: CloudFront, WAF, Shieldnetwork: private subnets, NACLseach server: security groupsidentity: IAM roles, least privilegedata: encrypted with KMS and TLSdetection: CloudTrail, GuardDutyPut only the load balancer on the internet; keep servers and data in private subnets, in two zones.A subnet is public when its route table sends 0.0.0.0/0 to the internet gateway. Private app servers reach out through aNAT gateway in their own zone, and nothing on the internet can start a connection to them. The database tier acceptsonly the app tier. Each layer assumes the one before it may fail: that is defence in depth.

Public subnets hold the load balancer and NAT gateway; app and data tiers sit in private subnets in two zones.

NAT gateways and VPC endpoints · Going deeper

Reach AWS services through endpoints, not the NAT gateway

app serverprivate subnetNAT gatewaythrough NAT: 10,000 GB × $0.045 = $450 a monthgateway endpointS3 and DynamoDB only: no chargeS310,000 GB a monthReach AWS services through VPC endpoints, not the NAT gateway: here $450 a month becomes nothing.A NAT gateway costs $0.045 an hour ($65.70 a month for one in each of two zones) plus $0.045 for every GB it handles.Gateway endpoints for S3 and DynamoDB are free and keep the traffic on AWS’s network. Interface endpoints (PrivateLink)reach most other services privately for about $0.01 an hour per zone and $0.01 a GB. Use one NAT gateway per zone,so losing a zone does not cut the other zone off the internet (example list prices, us-east-1, 2025).

Gateway endpoints for S3 and DynamoDB are free; the same 10,000 GB through NAT costs $450 a month.

Security groups

Each tier accepts traffic only from the tier in front of it

internetLoad balanceralb-sgin: 443 from anywhereApp serversapp-sgin: 8080 from alb-sgDatabasedb-sgin: 5432 from app-sgany other server in the VPC✗ not in app-sg: refusedSecurity groups let each tier accept traffic only from the tier in front of it, by name rather than address.A security group is a stateful firewall on each network interface: it holds only allow rules, and replies to anallowed request go back automatically. Because a rule can name another group, servers can come and go and therules still hold. Anything not allowed is refused, even from inside the VPC.

A security group is a stateful allow-list on each interface, and a rule can name another group instead of addresses.

Network ACLs · Going deeper

Security groups and network ACLs compared

Security groupNetwork ACLApplies toone network interface (instance, ALB, RDS)a whole subnetRulesallow onlyallow and deny, numbered, first match winsRepliesstateful: allowed automaticallystateless: allow them too (ports 1024–65535)Can refer toother security groupsIP address ranges onlyBy defaultnew group: nothing in, everything outdefault NACL: everything in and outSecurity groups do the real work; NACLs are a coarse subnet fence that can also deny.Use security groups for every rule about which tier talks to which. Add NACL rules for what a security groupcannot express: blocking a specific address range at the subnet edge. Remember a NACL is stateless, so an allowrule for inbound HTTPS also needs an outbound rule for the reply’s ephemeral ports.

Network ACLs are stateless subnet rules that can deny; security groups are stateful and allow only.

Encryption and keys

Encrypt data with a data key, and the data key with KMS

AWS KMSKMS keynever leaves KMS(hardware modules)Your applicationor an AWS serviceStorageGenerateDataKeytwo copies of the keyencrypt locallyencrypted dataencrypted data keyto read: KMS decrypts the data key only if IAM andthe key policy allow it, and CloudTrail logs every useData is encrypted with a data key, and the data key with a KMS key that never leaves KMS.This is envelope encryption: bulk data is encrypted locally and fast, and KMS only handles small keys (its own Encrypttakes at most 4 KB). Access to the data becomes permission to use one key, which IAM controls and CloudTrail records.S3, EBS and RDS do this for you when encryption is on. In transit, TLS everywhere: ACM issues and renews free publiccertificates for load balancers and CloudFront.

Envelope encryption: data is encrypted locally with a data key; a KMS key that never leaves KMS encrypts that key.

Secrets Manager · Going deeper

Keep secrets out of code, and let them rotate

Password in code or configDB_PASSWORD="hunter2" in the repositorycopied to every laptop and build lognever changed, because changing it breaks thingsapp (IAM role)GetSecretValueSecrets Managerencrypted with KMSrotates the passwordevery 30 days, DB tooKeep secrets out of code: fetch them at run time with a role, and let them rotate.Secrets Manager stores database passwords and API keys encrypted, hands them only to principals IAM allows, logs everyread, and rotates them on a schedule, updating the database to match (about $0.40 a secret a month). Systems ManagerParameter Store suits plain configuration. Better still, avoid passwords: IAM database authentication and roles.

Secrets Manager hands secrets to IAM roles at run time and rotates them on a schedule.

IAM and least privilege

Everything is denied unless a policy allows it

Request: role app-role wants s3:GetObject on orders-bucket/2026/*Any explicit Deny?yes → deniedAllowed by the SCP?no → deniedAllowed by the role orresource policy?no → denied by defaultAllowedshort-lived credentialsEvery request is denied unless a policy allows it, and any explicit Deny wins.Give workloads IAM roles, never long-lived access keys: EC2, Lambda and containers receive short-lived credentialsautomatically. Grant least privilege: the actions and the resources the job needs, no wildcards. Across many accounts,AWS Organizations service control policies set the most any account may do, even its administrators; they grantnothing themselves. Effective permission is the overlap of every layer.

An explicit Deny wins; an SCP caps the account; a role or resource policy must allow. Use IAM roles with least privilege.

WAF and Shield · Going deeper

Stop attacks at the edge, before they reach your servers

users and botsCloudFrontShield Standard: automaticnetwork-layer DDoSAWS WAF web ACLmanaged rules: SQL injection, XSSrate rule: 2,000 / 5 min per IPLoad balanceronly from CloudFrontShield Advanced: about $3,000 a month, for targets of large attacks:a response team, attack reports, and credit for scaling costsStop attacks at the edge, before they reach your servers: CloudFront with Shield, and WAF rules in front.Shield Standard protects every AWS customer from common network floods at no charge. WAF inspects web requests:managed rule groups block known exploits and bad IP lists, and rate-based rules cap how fast one address may call.Let the load balancer accept traffic only from CloudFront, so the edge cannot be bypassed.

Shield Standard absorbs network floods for everyone; AWS WAF blocks exploits and rate-limits each address.

Hybrid and multi-VPC networking

Connect many networks through a hub, not a mesh

peering: 15 links for 6 VPCs1,225 links for 50Transit Gateway: 6 attachments50 for 50, one route table to manageTo your data centreSite-to-Site VPNencrypted tunnels over the internetup to 1.25 Gbps a tunnel · ready in minutesDirect Connecta private line: 1, 10, 100 or 400 Gbpssteady latency · weeks to install · add encryptionConnect many networks through a hub, not a mesh; use VPN to start quickly and Direct Connect for steady volume.Peering links two VPCs and does not pass traffic on, so a full mesh of 50 VPCs needs 1,225 links. Transit Gateway is a regionalrouter: each VPC, VPN and Direct Connect attaches once. A common design runs Direct Connect with a VPN as backup.PrivateLink exposes one service to other VPCs without joining the networks at all.

Peering 50 VPCs needs 1,225 links; a Transit Gateway needs 50 attachments. VPN starts fast; Direct Connect is a private line.

Lesson complete

You finished “Security and network design”

Nicely done.

Open in a new tab