How to Create a VPC in AWS: Step-by-Step Guide
To create a VPC in AWS, use the
aws ec2 create-vpc command or define it in a CloudFormation template. A VPC is a private network in AWS where you can launch resources securely.Syntax
The basic command to create a VPC using AWS CLI is aws ec2 create-vpc --cidr-block <CIDR>. Here, --cidr-block specifies the IP address range for the VPC in CIDR notation, like 10.0.0.0/16.
In CloudFormation, you define a AWS::EC2::VPC resource with properties like CidrBlock and optional tags.
bash
aws ec2 create-vpc --cidr-block 10.0.0.0/16
Output
An object with VPC details including VpcId, CidrBlock, and State
Example
This example shows how to create a VPC with a CIDR block of 10.0.0.0/16 using AWS CLI and a CloudFormation template to create a VPC with tags.
bash / yaml
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --- AWSTemplateFormatVersion: '2010-09-09' Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 Tags: - Key: Name Value: MyVPC
Output
{
"Vpc": {
"VpcId": "vpc-0abcd1234efgh5678",
"State": "available",
"CidrBlock": "10.0.0.0/16",
"IsDefault": false
}
}
Common Pitfalls
- Using an invalid CIDR block format causes errors; always use valid IPv4 CIDR notation like
10.0.0.0/16. - Not tagging your VPC can make it hard to identify in the AWS console.
- Forgetting to create subnets inside the VPC means you cannot launch resources properly.
bash
aws ec2 create-vpc --cidr-block 10.0.0.0 # Wrong: Missing subnet mask aws ec2 create-vpc --cidr-block 10.0.0.0/16 # Correct: Proper CIDR block
Quick Reference
Remember these key points when creating a VPC:
- Use CIDR blocks like
10.0.0.0/16for private IP ranges. - Tag your VPC for easy management.
- Create subnets inside your VPC to launch resources.
- Use AWS CLI or CloudFormation for automation.
Key Takeaways
Use the AWS CLI command
aws ec2 create-vpc --cidr-block with a valid CIDR to create a VPC.Tag your VPC to keep your cloud resources organized and easy to find.
Always create subnets inside your VPC to launch EC2 instances or other resources.
CloudFormation templates help automate VPC creation with reusable code.
Check your CIDR block format carefully to avoid errors during creation.