Creating a custom VPC in AWS - Step-by-Step CLI Walkthrough
Start learning this pattern below
Jump into concepts and practice - no test required
AWSTemplateFormatVersion: '2010-09-09' Description: Custom VPC with public and private subnets Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: my-custom-vpc PublicSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 10.0.1.0/24 MapPublicIpOnLaunch: true AvailabilityZone: us-east-1a Tags: - Key: Name Value: my-public-subnet PrivateSubnet: Type: AWS::EC2::Subnet Properties: VpcId: !Ref MyVPC CidrBlock: 10.0.2.0/24 MapPublicIpOnLaunch: false AvailabilityZone: us-east-1a Tags: - Key: Name Value: my-private-subnet InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: my-internet-gateway AttachGateway: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref MyVPC InternetGatewayId: !Ref InternetGateway PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref MyVPC Tags: - Key: Name Value: my-public-route-table PublicRoute: Type: AWS::EC2::Route DependsOn: AttachGateway Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway PublicSubnetRouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet RouteTableId: !Ref PublicRouteTable
This file creates a custom VPC with a large private IP range (10.0.0.0/16). It defines one public subnet and one private subnet inside the VPC. An Internet Gateway is attached to allow internet access for the public subnet. A route table is created and associated with the public subnet to send internet traffic through the gateway.
MyVPC: The main private network.
PublicSubnet: A subnet that can access the internet.
PrivateSubnet: A subnet isolated from the internet.
InternetGateway: Connects the VPC to the internet.
RouteTable and Route: Directs internet traffic from the public subnet.
aws cloudformation create-stack --stack-name my-custom-vpc-stack --template-body file://vpc.yaml
--stack-name - Names the CloudFormation stack for easy management--template-body - Specifies the local template file to useaws cloudformation describe-stacks --stack-name my-custom-vpc-stack
--stack-name - Specifies which stack to describeaws ec2 describe-vpcs --filters Name=tag:Name,Values=my-custom-vpc
--filters - Filters VPCs by tag nameIf you remember nothing else from this pattern, remember: a custom VPC lets you control your own private network space in the cloud with your own IP ranges and internet access rules.
Practice
Solution
Step 1: Understand what a VPC is
A VPC is a private network in AWS where you control IP ranges and network settings.Step 2: Identify the purpose of a custom VPC
Creating a custom VPC lets you choose your IP range and control network setup for your resources.Final Answer:
To have a private network with a specific IP range for your resources -> Option DQuick Check:
Custom VPC = Private network with chosen IP range [OK]
- Thinking VPC automatically assigns public IPs
- Believing AWS manages the network without user control
- Confusing VPC with external internet connections
Solution
Step 1: Understand CIDR notation
CIDR block defines IP range with format like x.x.x.x/y where y is between 0 and 32.Step 2: Check each option for validity
10.0.0.0/16 is valid CIDR (10.0.0.0/16). 255.255.255.0 is a subnet mask, not CIDR. 192.168.1.256/24 has invalid IP (256). 10.0.0.0/33 has invalid prefix length (33).Final Answer:
10.0.0.0/16 -> Option CQuick Check:
CIDR block format = x.x.x.x/y with y ≤ 32 [OK]
- Using subnet mask instead of CIDR
- Using invalid IP numbers like 256
- Using prefix length greater than 32
aws ec2 create-vpc --cidr-block 10.1.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=MyVPC}]'What will be the result?
Solution
Step 1: Analyze the CLI command structure
The command uses 'create-vpc' with a valid CIDR block and correct tag specification syntax.Step 2: Understand the effect of the command
This creates a VPC with the given CIDR and applies the Name tag 'MyVPC' to it.Final Answer:
A VPC with CIDR 10.1.0.0/16 and a Name tag 'MyVPC' will be created -> Option AQuick Check:
Valid CLI command creates VPC with CIDR and tags [OK]
- Incorrect tag syntax causing command failure
- Confusing subnet creation with VPC creation
- Ignoring the CIDR block parameter
Solution
Step 1: Understand DNS hostnames setting in VPC
DNS hostnames is a VPC attribute that can be enabled or disabled after creation.Step 2: Identify how to enable DNS hostnames
You can modify the VPC attribute via AWS console or CLI without deleting the VPC.Final Answer:
Modify the VPC attribute to enable DNS hostnames using AWS console or CLI -> Option AQuick Check:
DNS hostnames can be enabled post-creation [OK]
- Thinking you must delete and recreate the VPC
- Trying to enable DNS hostnames on a subnet instead of VPC
- Believing DNS hostnames are enabled by default always
Solution
Step 1: Create the VPC with chosen CIDR block
The VPC must exist before creating subnets or attaching gateways.Step 2: Enable DNS support and hostnames on the VPC
This ensures resources inside can resolve names properly.Step 3: Create two public subnets in different availability zones
Subnets must be inside the VPC and in separate AZs for high availability.Step 4: Attach an internet gateway to allow internet access
This makes the subnets public.Final Answer:
Create VPC with CIDR, enable DNS support and hostnames, create two public subnets in different AZs, attach internet gateway -> Option BQuick Check:
VPC -> DNS -> Subnets -> Internet Gateway [OK]
- Creating subnets before the VPC exists
- Attaching internet gateway before VPC creation
- Assuming DNS settings are automatic
