Skip to content

Instantly share code, notes, and snippets.

@Z4ck404

Z4ck404/main.tf Secret

Created November 12, 2022 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Z4ck404/b08f72fb7bdcbc47d7beaa1a70ffd229 to your computer and use it in GitHub Desktop.
Save Z4ck404/b08f72fb7bdcbc47d7beaa1a70ffd229 to your computer and use it in GitHub Desktop.
multi region vpc peering
#### variables ####
#accepter
variable "accpeter_vpc_id" {}
variable "accepter_region" {}
#requester
variable "requester_vpc_id" {}
variable "requester_region" {}
#### providers ####
#requester
provider "aws" {
alias = "peer"
region = var.requester_region
}
## accepter
provider "aws" {
alias = "accepter"
region = var.accepter_region
}
data "aws_vpc" "accepter" {
id = var.accpeter_vpc_id
provider = aws.accepter
}
data "aws_route_tables" "accepter" {
vpc_id = var.accpeter_vpc_id
provider = aws.accepter
}
data "aws_vpc" "requester" {
id = var.accpeter_vpc_id
provider = aws.requester
}
data "aws_route_tables" "requester" {
vpc_id = var.requester_vpc_id
provider = aws.requester
}
locals {
requester_route_tables_ids = data.aws_route_tables.requester.ids
accepter_route_tables_ids = data.aws_route_tables.accepter.ids
}
#### peering configuration ####
data "aws_availability_zones" "available" {
provider = aws.peer
}
resource "aws_vpc_peering_connection" "this" {
vpc_id = var.requester_vpc_id
peer_vpc_id = var.accpeter_vpc_id
peer_region = var.accepter_region
auto_accept = false
provider = aws.peer
}
resource "aws_vpc_peering_connection_accepter" "this" {
provider = aws.accepter
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
auto_accept = true
}
resource "aws_vpc_peering_connection_options" "this" {
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
accepter {
allow_remote_vpc_dns_resolution = true
}
provider = aws.accepter
}
#### route tables ####
resource "aws_route" "requester" {
count = length(local.requester_route_tables_ids)
route_table_id = local.requester_route_tables_ids[count.index]
destination_cidr_block = data.aws_vpc.accepter.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
provider = aws.peer
}
resource "aws_route" "accepter" {
count = length(local.accepter_route_tables_ids)
route_table_id = local.accepter_route_tables_ids[count.index]
destination_cidr_block = data.aws_vpc.requester.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
provider = aws.accepter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment