1. Answers
  2. Building an AWS EC2 VPC Peering Connection

How do I build an AWS EC2 VPC peering connection?

To create a VPC peering connection on AWS using Pulumi, you’ll need to define two VPCs and establish a peering connection between them. This involves creating the VPCs, subnets, and the peering connection resource. Below is a detailed explanation followed by the code to accomplish this.

Explanation

  1. Define VPCs: Create two VPCs, vpcA and vpcB, each with their own CIDR blocks.
  2. Subnets: Create subnets within each VPC.
  3. VPC Peering Connection: Establish a peering connection between the two VPCs.
  4. Route Tables: Update the route tables of each VPC to allow traffic to flow between them via the peering connection.

Code

import * as aws from "@pulumi/aws";

// Create VPC A
const vpcA = new aws.ec2.Vpc("vpcA", {
    cidrBlock: "10.0.0.0/16",
    tags: { Name: "vpcA" },
});

// Create a subnet in VPC A
const subnetA = new aws.ec2.Subnet("subnetA", {
    vpcId: vpcA.id,
    cidrBlock: "10.0.1.0/24",
    availabilityZone: "us-west-2a",
    tags: { Name: "subnetA" },
});

// Create VPC B
const vpcB = new aws.ec2.Vpc("vpcB", {
    cidrBlock: "10.1.0.0/16",
    tags: { Name: "vpcB" },
});

// Create a subnet in VPC B
const subnetB = new aws.ec2.Subnet("subnetB", {
    vpcId: vpcB.id,
    cidrBlock: "10.1.1.0/24",
    availabilityZone: "us-west-2b",
    tags: { Name: "subnetB" },
});

// Create VPC peering connection
const vpcPeeringConnection = new aws.ec2.VpcPeeringConnection("vpcPeeringConnection", {
    vpcId: vpcA.id,
    peerVpcId: vpcB.id,
    autoAccept: true,
    tags: { Name: "vpcA-vpcB-peering" },
});

// Update route table for VPC A to allow traffic to VPC B
const routeTableA = new aws.ec2.RouteTable("routeTableA", {
    vpcId: vpcA.id,
    routes: [
        {
            cidrBlock: vpcB.cidrBlock,
            vpcPeeringConnectionId: vpcPeeringConnection.id,
        },
    ],
    tags: { Name: "routeTableA" },
});

// Update route table for VPC B to allow traffic to VPC A
const routeTableB = new aws.ec2.RouteTable("routeTableB", {
    vpcId: vpcB.id,
    routes: [
        {
            cidrBlock: vpcA.cidrBlock,
            vpcPeeringConnectionId: vpcPeeringConnection.id,
        },
    ],
    tags: { Name: "routeTableB" },
});

// Associate the route tables with the subnets
new aws.ec2.RouteTableAssociation("routeTableAssociationA", {
    subnetId: subnetA.id,
    routeTableId: routeTableA.id,
});

new aws.ec2.RouteTableAssociation("routeTableAssociationB", {
    subnetId: subnetB.id,
    routeTableId: routeTableB.id,
});

Key Points

  • VPCs: Two separate VPCs are created with distinct CIDR blocks.
  • Subnets: Each VPC has a subnet in a different availability zone.
  • VPC Peering Connection: A peering connection is established between the two VPCs.
  • Route Tables: Route tables are updated to allow traffic to flow between the VPCs through the peering connection.

Conclusion

This code sets up two VPCs and establishes a peering connection between them, allowing instances in one VPC to communicate with instances in the other. The route tables are configured to route traffic through the peering connection, enabling seamless networking between the VPCs.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up