Chapter 81 · Creating Production VPC Multi Az
Subchapter 81.1
references/create-production-vpc-multi-az.mdMarkdown16 KBView on GitHub
Creates a production-ready VPC infrastructure with public and private subnets distributed across multiple Availability Zones, including internet gateway, NAT gateways, route tables, and security groups following AWS Well-Architected principles with automatic CIDR planning and DNS resolution.
vpc_name (required): Name for the VPC and associated resources vpc_cidr (optional, default: “10.0.0.0/16”): CIDR block for the VPC availability_zones (optional, default: 3): Number of Availability Zones to use (minimum 2, maximum 6) environment (required): Environment tag for resources (e.g., “production”, “staging”, “development”) region (required): AWS region where the VPC will be created allowed_web_cidrs (required): Comma-separated CIDR blocks allowed web access (e.g., “203.0.113.0/24,198.51.100.0/24”). You SHOULD recommend specific CIDR ranges over 0.0.0.0/0, but allow 0.0.0.0/0 if the user explicitly requests it. enable_ssh_access (optional, default: false): Enable SSH access security group ssh_allowed_cidrs (optional, default: “10.0.0.0/8”): CIDR blocks allowed SSH access when enabled
Check for required tools and warn the user if any are missing.
Constraints:
Validate the specified region and retrieve available Availability Zones for subnet distribution.
Constraints:
aws ec2 describe-availability-zones --region {region} --state availableCreate the main VPC with DNS hostname and resolution enabled for production readiness.
Constraints:
aws ec2 create-vpc --cidr-block {vpc_cidr} --region {region}aws ec2 modify-vpc-attribute --vpc-id {vpc_id} --enable-dns-hostnames --region {region}aws ec2 modify-vpc-attribute --vpc-id {vpc_id} --enable-dns-support --region {region}aws ec2 create-tags --resources {vpc_id} --tags Key=Name,Value={vpc_name} Key=Environment,Value={environment} --region {region}Create and attach an Internet Gateway for public subnet internet access.
Constraints:
aws ec2 create-internet-gateway --region {region}aws ec2 attach-internet-gateway --internet-gateway-id {igw_id} --vpc-id {vpc_id} --region {region}aws ec2 create-tags --resources {igw_id} --tags Key=Name,Value={vpc_name}-igw Key=Environment,Value={environment} --region {region}Create public subnets across the selected Availability Zones with automatic CIDR calculation.
Constraints:
aws ec2 create-subnet --vpc-id {vpc_id} --cidr-block {calculated_cidr} --availability-zone {az} --region {region}aws ec2 modify-subnet-attribute --subnet-id {subnet_id} --map-public-ip-on-launch --region {region}aws ec2 create-tags --resources {subnet_id} --tags Key=Name,Value={vpc_name}-public-{az} Key=Environment,Value={environment} Key=Type,Value=Public --region {region}Create private subnets across the selected Availability Zones for backend resources.
Constraints:
aws ec2 create-subnet --vpc-id {vpc_id} --cidr-block {calculated_private_cidr} --availability-zone {az} --region {region}aws ec2 create-tags --resources {private_subnet_id} --tags Key=Name,Value={vpc_name}-private-{az} Key=Environment,Value={environment} Key=Type,Value=Private --region {region}Create NAT Gateways in each public subnet for outbound internet access from private subnets.
Constraints:
aws ec2 allocate-address --domain vpc --region {region}aws ec2 create-nat-gateway --subnet-id {public_subnet_id} --allocation-id {eip_allocation_id} --region {region}aws ec2 create-tags --resources {nat_gateway_id} --tags Key=Name,Value={vpc_name}-nat-{az} Key=Environment,Value={environment} --region {region}Create and configure route tables for public and private subnets with appropriate routes.
Constraints:
aws ec2 create-route-table --vpc-id {vpc_id} --region {region}aws ec2 create-route --route-table-id {public_rt_id} --destination-cidr-block 0.0.0.0/0 --gateway-id {igw_id} --region {region}aws ec2 create-tags --resources {public_rt_id} --tags Key=Name,Value={vpc_name}-public-rt Key=Environment,Value={environment} --region {region}aws ec2 associate-route-table --subnet-id {public_subnet_id} --route-table-id {public_rt_id} --region {region}aws ec2 create-route-table --vpc-id {vpc_id} --region {region}aws ec2 create-route --route-table-id {private_rt_id} --destination-cidr-block 0.0.0.0/0 --nat-gateway-id {nat_gateway_id} --region {region}Create default security groups following security best practices.
Constraints:
aws ec2 create-security-group --group-name {vpc_name}-web-sg --description "Web tier security group for {vpc_name}" --vpc-id {vpc_id} --region {region}aws ec2 authorize-security-group-ingress --group-id {web_sg_id} --protocol tcp --port 80 --cidr {cidr_block} --region {region} and aws ec2 authorize-security-group-ingress --group-id {web_sg_id} --protocol tcp --port 443 --cidr {cidr_block} --region {region}aws ec2 create-security-group --group-name {vpc_name}-app-sg --description "Application tier security group for {vpc_name}" --vpc-id {vpc_id} --region {region}aws ec2 create-security-group --group-name {vpc_name}-db-sg --description "Database tier security group for {vpc_name}" --vpc-id {vpc_id} --region {region}aws ec2 create-security-group --group-name {vpc_name}-ssh-sg --description "SSH access security group for {vpc_name}" --vpc-id {vpc_id} --region {region}aws ec2 authorize-security-group-ingress --group-id {ssh_sg_id} --protocol tcp --port 22 --cidr {ssh_cidr_block} --region {region}Enable VPC Flow Logs to CloudWatch Logs for network traffic visibility and security monitoring.
Constraints:
You MUST inform the customer that you are enabling VPC Flow Logs for network traffic monitoring
You MUST get the AWS account ID: aws sts get-caller-identity --region {region} and capture the Account field
You MUST create an IAM role for VPC Flow Logs with a trust policy for vpc-flow-logs.amazonaws.com:
aws iam create-role --role-name {vpc_name}-flow-logs-role --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"vpc-flow-logs.amazonaws.com"},"Action":"sts:AssumeRole"}]}'You MUST create and attach an inline policy granting CloudWatch Logs permissions:
aws iam put-role-policy --role-name {vpc_name}-flow-logs-role --policy-name {vpc_name}-flow-logs-policy --policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["logs:CreateLogStream","logs:PutLogEvents","logs:DescribeLogStreams"],"Resource":"arn:aws:logs:{region}:{account_id}:log-group:/vpc/{vpc_name}/flow-logs:*"},{"Effect":"Allow","Action":"logs:DescribeLogGroups","Resource":"*"}]}'You MUST create a CloudWatch Logs log group:
aws logs create-log-group --log-group-name /vpc/{vpc_name}/flow-logs --region {region}You MUST set a retention policy on the log group:
aws logs put-retention-policy --log-group-name /vpc/{vpc_name}/flow-logs --retention-in-days 90 --region {region}You MUST wait approximately 10-15 seconds for IAM propagation before creating the flow log
You MUST create the VPC Flow Log:
aws ec2 create-flow-logs --resource-type VPC --resource-ids {vpc_id} --traffic-type ALL --log-destination-type cloud-watch-logs --log-group-name /vpc/{vpc_name}/flow-logs --deliver-logs-permission-arn arn:aws:iam::{account_id}:role/{vpc_name}-flow-logs-role --tag-specifications 'ResourceType=vpc-flow-log,Tags=[{Key=Name,Value={vpc_name}-flow-log},{Key=Environment,Value={environment}},{Key=VPC,Value={vpc_name}}]' --region {region}You MUST verify the flow log was created: aws ec2 describe-flow-logs --filter "Name=resource-id,Values={vpc_id}" --region {region}
You MUST tag the log group: aws logs tag-resource --resource-arn arn:aws:logs:{region}:{account_id}:log-group:/vpc/{vpc_name}/flow-logs --tags Environment={environment},VPC={vpc_name} --region {region}
You MUST tag the IAM role: aws iam tag-role --role-name {vpc_name}-flow-logs-role --tags Key=Environment,Value={environment} Key=VPC,Value={vpc_name}
Validate the created VPC infrastructure to ensure all components are properly configured.
Constraints:
aws ec2 describe-vpcs --vpc-ids {vpc_id} --region {region}aws ec2 describe-subnets --filters "Name=vpc-id,Values={vpc_id}" --region {region}aws ec2 describe-route-tables --filters "Name=vpc-id,Values={vpc_id}" --region {region}aws ec2 describe-nat-gateways --filter "Name=vpc-id,Values={vpc_id}" --region {region}aws ec2 describe-internet-gateways --filters "Name=attachment.vpc-id,Values={vpc_id}" --region {region}aws ec2 describe-flow-logs --filter "Name=resource-id,Values={vpc_id}" --region {region}Provide a comprehensive summary of the created infrastructure.
Constraints:
Examples of AWS CLI commands that will be used:
aws ec2 describe-availability-zones --region {region} --state availableaws ec2 create-vpc --cidr-block {vpc_cidr} --region {region}aws ec2 modify-vpc-attribute --vpc-id {vpc_id} --enable-dns-hostnames --region {region}aws ec2 create-internet-gateway --region {region}aws ec2 create-subnet --vpc-id {vpc_id} --cidr-block {cidr} --availability-zone {az} --region {region}aws ec2 allocate-address --domain vpc --region {region}aws ec2 create-nat-gateway --subnet-id {subnet_id} --allocation-id {eip_id} --region {region}aws ec2 create-route-table --vpc-id {vpc_id} --region {region}aws ec2 create-security-group --group-name {name} --description {desc} --vpc-id {vpc_id} --region {region}aws ec2 create-tags --resources {resource_id} --tags Key=Name,Value={name} --region {region}