Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

AWS VPC Creation Script

A Bash script to create and destroy AWS VPCs with public subnets using the AWS CLI.

Overview

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.

Features

  • 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.txt for later cleanup
  • POSIX portable: Compatible with bash and sh shells

Prerequisites

  • bash or sh shell
  • AWS CLI installed and configured with appropriate credentials
  • AWS credentials with permissions to:
    • ec2:CreateVpc
    • ec2:CreateSubnet
    • ec2:CreateTags
    • ec2:DeleteSubnet
    • ec2:DeleteVpc

Install AWS CLI

# macOS (Homebrew)
brew install awscli

# Ubuntu/Debian
sudo apt-get install awscli

# Windows (PowerShell)
choco install awscli

Configure credentials:

aws configure

Configuration

Edit 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 location

Usage

Make Script Executable

chmod +x aws_vpc_create.sh

Create VPC and Subnet

./aws_vpc_create.sh create

Or simply:

./aws_vpc_create.sh

(Defaults to create mode)

Destroy VPC and Subnet

./aws_vpc_create.sh destroy

Display Help

./aws_vpc_create.sh help

Example Output

Create Mode

Creating 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.

Destroy Mode

Destroying subnet with ID: subnet-0xyz789abc123def...
Subnet destroyed.
Destroying VPC with ID: vpc-0abc123def456ghi...
VPC destroyed.
VPC and Subnet destruction complete.

State Management

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

Error Handling

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.

Troubleshooting

"AWS CLI could not be found"

Install AWS CLI and ensure it's in your PATH:

which aws

"Failed to create VPC"

Check your AWS credentials and permissions:

aws ec2 describe-vpcs --region us-east-1

"State file not found"

Run create mode first to generate the state file, or manually create it with resource IDs.

Permission Denied

Ensure the script is executable:

chmod +x aws_vpc_create.sh

Security Notes

  • 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

License

Open source - free to use and modify.

Contributing

Suggestions for improvements are welcome. Consider adding:

  • Multi-region support
  • Internet gateway and route table configuration
  • Security group creation
  • Configuration file support

About

This bash 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.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages