DEV Community

Cover image for Simplifying VPC Peering with Terraform
sudh33ra
sudh33ra

Posted on

Simplifying VPC Peering with Terraform

Repo


TL;DR

  • clone the above
  • edit vars.tfvars
  • run the commands, you’re done
terraform init --reconfigure 
terraform plan -var-file=vars.tfvars -out tfplan 
terraform apply "tfplan"

Enter fullscreen mode Exit fullscreen mode

Hey tech wizards! 🧙‍♂️ Ever wanted to connect two VPCs seamlessly? Look no further! This Terraform script is your go-to spell for creating a magical peering connection between two virtual realms.

The Spellbook

Players on the Stage

data "aws_caller_identity" "peer": Meet the peer. We're grabbing its identity details, setting the stage for our mystical connection.

resource "aws_vpc_peering_connection" "peer": The main act. We're weaving the connection, specifying the main VPC, the peer VPC, and other details. Tags act like name tags for our peering connection, keeping everything neatly labeled.

resource "aws_vpc_peering_connection_accepter" "peer": The gracious accepter on the other side. This player automatically nods to the peering connection. No need for manual approvals here!

resource "aws_route" "main_r" and resource "aws_route" "peer_r": Think of these as the traffic conductors. We're setting up routes in the route tables of both VPCs to guide the virtual traffic. It's like placing road signs for data.

The Dance Steps

**Requester's Moves: **We kick off the peering connection with the requester side. We're setting up the connection, specifying who we want to peer with, and tagging it as the "Requester" side.

Accepter's Groove: The accepter side gracefully accepts the connection, all automated. Tags say it's the "Accepter" side.

Routing Rhythms: We create routes in both VPCs to ensure data knows where to go. Think of it as programming the GPS for our virtual traffic.

The Magic Words

terraform init --reconfigure
terraform plan -var-file=vars.tfvars -out tfplan
terraform apply "tfplan"
Enter fullscreen mode Exit fullscreen mode

Just chant these words in your terminal, and watch the magic unfold in your AWS console!

Important Notes

This script is your key to regional connections between VPCs. But remember, the magic words come with a note:

Note: After the spell is cast, make sure to enable DNS resolving on both sides for the full enchantment.

Happy peering,
SD

Top comments (2)

Collapse
 
pwd9000 profile image
Marcel.L

Welcome to the DEV community!
Really liked the style and format of how this post was written! Well done and carry on creating such creative posts and tutorials. Love the fantasy genre! :)
Terraform has many magic words haha!

Collapse
 
sudh33ra profile image
sudh33ra

thanks a lot!