A Bash script to create and destroy AWS VPCs with public subnets using the AWS CLI.
This script automates the provisioning and cleanup of AWS network infrastructure. It creates a VPC with a public subnet and tags them appropriately for easy identification. Resources are tracked in a state file to enable clean destruction when no longer needed.
- Create mode: Provisions a VPC and public subnet with configurable CIDR blocks
- Destroy mode: Cleans up all created resources using saved state
- Error handling: Validates AWS CLI installation and checks for command failures
- Resource tagging: Automatically tags VPC and subnet with custom names
- State persistence: Saves resource IDs to
~/.aws_vpc_state.txtfor later cleanup - POSIX portable: Compatible with bash and sh shells
- bash or sh shell
- AWS CLI installed and configured with appropriate credentials
- AWS credentials with permissions to:
ec2:CreateVpcec2:CreateSubnetec2:CreateTagsec2:DeleteSubnetec2:DeleteVpc
# macOS (Homebrew)
brew install awscli
# Ubuntu/Debian
sudo apt-get install awscli
# Windows (PowerShell)
choco install awscliConfigure credentials:
aws configureEdit these variables at the top of aws_vpc_create.sh:
VPC_CIDR="10.0.0.0/16" # VPC CIDR block
SUBNET_CIDR="10.0.1.0/24" # Subnet CIDR block
REGION="us-east-1" # AWS region
NAME="demo-vpc" # VPC name tag
SUBNET_NAME="demo-subnet" # Subnet name tag
SUBNET_AVAILABILITY_ZONE="${REGION}a" # Availability zone
STATE_FILE="${HOME}/.aws_vpc_state.txt" # State file locationchmod +x aws_vpc_create.sh./aws_vpc_create.sh createOr simply:
./aws_vpc_create.sh(Defaults to create mode)
./aws_vpc_create.sh destroy./aws_vpc_create.sh helpCreating VPC with CIDR block 10.0.0.0/16...
VPC created with ID: vpc-0abc123def456ghi
VPC tagged with Name: demo-vpc
Creating subnet with CIDR block 10.0.1.0/24 in availability zone us-east-1a...
Subnet created with ID: subnet-0xyz789abc123def
Subnet tagged with Name: demo-subnet
VPC and Subnet creation complete.
Destroying subnet with ID: subnet-0xyz789abc123def...
Subnet destroyed.
Destroying VPC with ID: vpc-0abc123def456ghi...
VPC destroyed.
VPC and Subnet destruction complete.
The script stores created resource IDs in ~/.aws_vpc_state.txt:
vpc-0abc123def456ghi
subnet-0xyz789abc123def
This file is:
- Created during resource provisioning
- Read during resource destruction
- Deleted after successful cleanup
The script validates:
- AWS CLI installation before execution
- VPC creation success before proceeding to subnet creation
- Subnet creation success before saving state
- State file validity before attempting destroy operations
Errors are written to stderr and exit codes are properly set for scripting integration.
Install AWS CLI and ensure it's in your PATH:
which awsCheck your AWS credentials and permissions:
aws ec2 describe-vpcs --region us-east-1Run create mode first to generate the state file, or manually create it with resource IDs.
Ensure the script is executable:
chmod +x aws_vpc_create.sh- Store AWS credentials securely using
aws configure(not hardcoded) - Use IAM roles for EC2 instances instead of long-term credentials
- Limit IAM policies to required EC2 actions
- Review and adjust CIDR blocks for your network requirements
- Clean up resources when no longer needed to avoid unnecessary AWS charges
Open source - free to use and modify.
Suggestions for improvements are welcome. Consider adding:
- Multi-region support
- Internet gateway and route table configuration
- Security group creation
- Configuration file support