Skip to content

Instantly share code, notes, and snippets.

@ashishjullia
Created October 27, 2023 11:42
Show Gist options
  • Save ashishjullia/5e297dda6eed0b0c3f9c8cd2b9c4c06f to your computer and use it in GitHub Desktop.
Save ashishjullia/5e297dda6eed0b0c3f9c8cd2b9c4c06f to your computer and use it in GitHub Desktop.
provider "aws" {
region = var.aws_region
}
variable "aws_region" {
description = "The AWS region to deploy resources in."
default = "us-west-2"
}
variable "vpc_a_id" {
description = "ID for existing VPC A"
type = string
}
variable "vpc_b_id" {
description = "ID for existing VPC B"
type = string
}
variable "vpc_a_route_table_ids" {
description = "List of route table IDs for VPC A"
type = list(string)
default = []
}
variable "vpc_b_route_table_ids" {
description = "List of route table IDs for VPC B"
type = list(string)
default = []
}
resource "aws_vpc_peering_connection" "peer_a_to_b" {
vpc_id = var.vpc_a_id
peer_vpc_id = var.vpc_b_id
auto_accept = true
tags = {
Name = "vpc_a_to_vpc_b"
}
}
resource "aws_route" "route_a_to_b" {
count = length(var.vpc_a_route_table_ids)
route_table_id = var.vpc_a_route_table_ids[count.index]
destination_cidr_block = var.vpc_b_cidr # We would need the CIDR for this to work, or can be made variable.
vpc_peering_connection_id = aws_vpc_peering_connection.peer_a_to_b.id
}
resource "aws_route" "route_b_to_a" {
count = length(var.vpc_b_route_table_ids)
route_table_id = var.vpc_b_route_table_ids[count.index]
destination_cidr_block = var.vpc_a_cidr # Same comment as above.
vpc_peering_connection_id = aws_vpc_peering_connection.peer_a_to_b.id
}
# Optional: If you don't know the CIDRs of the VPCs by heart,
# you can use data blocks to fetch them based on VPC ID
data "aws_vpc" "vpc_a_data" {
id = var.vpc_a_id
}
data "aws_vpc" "vpc_b_data" {
id = var.vpc_b_id
}
@ashishjullia
Copy link
Author

terraform.tfvars

aws_region             = "us-east-1"
vpc_a_id               = "vpc-0123456789abcdef0"
vpc_b_id               = "vpc-abcdef0123456789"
vpc_a_route_table_ids  = ["rtb-0123456789abcdef0", "rtb-0123456789abcdef1"]
vpc_b_route_table_ids  = ["rtb-abcdef0123456789", "rtb-abcdef012345678a"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment