VPC Peering Automation Script
Bash and AWS CLI script that creates bidirectional VPC peering including route table entries and security group rules in both VPCs. Idempotent and safe to re-run.
Bidirectional VPC Peering in One Script
A single bash script that automates the full VPC peering setup — creates the peering connection, adds route table entries in both VPCs, and updates security groups to allow traffic across the peer. Idempotent: safe to re-run without creating duplicates.
What's Included
- Peering connection request and automatic acceptance (same-account)
- Route table entry injection in all route tables of both VPCs
- Security group rule injection for specified ports and protocols
- Idempotency checks — skips resources that already exist
- Cross-account mode: outputs the peering connection ID for manual acceptance in the peer account
- Dry-run flag to preview all changes before applying
What You Get
peer-vpcs.sh— main peering scriptunpeer-vpcs.sh— teardown script to remove routes and the peering connectionREADME.md— usage examples, same-account and cross-account workflows, flags reference
Requirements
- bash 4+, AWS CLI v2, jq
- IAM permissions: ec2:CreateVpcPeeringConnection, ec2:CreateRoute, ec2:AuthorizeSecurityGroupIngress
- Both VPCs must have non-overlapping CIDR blocks