Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable multi account vpc peering #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions examples/vpc-with-peering/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ locals {
}

module "vpc_peering" {
source = "squareops/vpc/aws//modules/vpc_peering"
accepter_name = local.accepter_name
accepter_vpc_id = local.accepter_vpc_id
accepter_vpc_region = local.accepter_region
requester_name = local.requester_name
requester_vpc_id = local.requester_vpc_id
requester_vpc_region = local.requester_region
source = "squareops/vpc/aws//modules/vpc_peering"
accepter_name = local.accepter_name
accepter_vpc_id = local.accepter_vpc_id
accepter_vpc_region = local.accepter_region
requester_name = local.requester_name
requester_vpc_id = local.requester_vpc_id
requester_vpc_region = local.requester_region
multi_account_enabled = false
requester_aws_profile = ""
accepter_aws_profile = ""
}
27 changes: 17 additions & 10 deletions modules/vpc_peering/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ locals {
}

provider "aws" {
alias = "peer"
region = var.requester_vpc_region
alias = "peer"
region = var.requester_vpc_region
profile = var.multi_account_enabled ? var.requester_aws_profile : "default"
}

provider "aws" {
alias = "accepter"
region = var.accepter_vpc_region
alias = "accepter"
region = var.accepter_vpc_region
profile = var.multi_account_enabled ? var.accepter_aws_profile : "default"
}

data "aws_vpc" "accepter" {
Expand All @@ -33,13 +35,18 @@ data "aws_route_tables" "requester" {
provider = aws.peer
}

data "aws_caller_identity" "accepter" {
provider = aws.accepter
}

resource "aws_vpc_peering_connection" "this" {
count = var.peering_enabled ? 1 : 0
vpc_id = var.requester_vpc_id
peer_vpc_id = var.accepter_vpc_id
peer_region = var.accepter_vpc_region
auto_accept = false
provider = aws.peer
count = var.peering_enabled ? 1 : 0
vpc_id = var.requester_vpc_id
peer_vpc_id = var.accepter_vpc_id
peer_region = var.multi_account_enabled ? var.accepter_vpc_region : null
auto_accept = false
peer_owner_id = var.multi_account_enabled ? data.aws_caller_identity.accepter.id : null
provider = aws.peer
tags = {
Name = format("%s-%s-%s", var.requester_name, "to", var.accepter_name)
}
Expand Down
18 changes: 18 additions & 0 deletions modules/vpc_peering/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ variable "peering_enabled" {
description = "Set this variable to true if you want to create the VPC peering connection. Set it to false if you want to skip the creation process."
default = true
}

variable "multi_account_enabled" {
type = bool
description = "Set this variable to true if you want to create the VPC peering connection between reagions. Set it to false if you want to skip the creation process."
default = true
}

variable "requester_aws_profile" {
type = string
description = "Provide the AWS profile where the requester VPC is located."
default = ""
}

variable "accepter_aws_profile" {
type = string
description = "Provide the AWS profile where the accepter VPC is located."
default = ""
}