Skip to content

VpcPeeringConnection

Source: src/AWS/EC2/VpcPeeringConnection.ts

A VPC peering connection links two VPCs so resources in each can communicate using private IP addresses, as if they were on the same network. The two VPCs can be in the same account or different accounts, and the same Region or different Regions. Their CIDR blocks must not overlap.

A peering connection is a two-sided handshake: a requester VPC creates the request and an accepter VPC accepts it. For same-account, same-Region peering alchemy accepts the request for you automatically (autoAccept defaults to true); for cross-account or cross-Region peering the connection is left in pending-acceptance for the peer to accept out of band. Once active, add Routes on both sides pointing the peer CIDR at the connection to actually carry traffic.

Same-Account Peering (auto-accepted)

const vpcA = yield* AWS.EC2.Vpc("VpcA", { cidrBlock: "10.0.0.0/16" });
const vpcB = yield* AWS.EC2.Vpc("VpcB", { cidrBlock: "10.1.0.0/16" });
const peering = yield* AWS.EC2.VpcPeeringConnection("Peering", {
vpcId: vpcA.vpcId,
peerVpcId: vpcB.vpcId,
});

Because both VPCs are in the same account and Region, the request is accepted automatically and the connection reaches the active state.

Cross-Account Peering (accepted out of band)

const peering = yield* AWS.EC2.VpcPeeringConnection("Peering", {
vpcId: myVpc.vpcId,
peerVpcId: "vpc-0abc123",
peerOwnerId: "123456789012",
});

With a different peerOwnerId the connection stays in pending-acceptance until the peer account accepts it.

const peering = yield* AWS.EC2.VpcPeeringConnection("Peering", {
vpcId: vpcA.vpcId,
peerVpcId: vpcB.vpcId,
});
const routeAtoB = yield* AWS.EC2.Route("RouteAtoB", {
routeTableId: vpcARouteTable.routeTableId,
destinationCidrBlock: "10.1.0.0/16",
vpcPeeringConnectionId: peering.vpcPeeringConnectionId,
});

Each side needs a route pointing the other VPC’s CIDR at the peering connection; only then can instances reach each other over private IPs.