Created
October 27, 2023 11:42
-
-
Save ashishjullia/5e297dda6eed0b0c3f9c8cd2b9c4c06f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
terraform.tfvars