diff --git a/.gitignore b/.gitignore index a5ecc30..c95edfb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ .build-harness build-harness/ + +# IDE files +.vscode +.history diff --git a/main.tf b/main.tf index 7c6f352..aec7486 100644 --- a/main.tf +++ b/main.tf @@ -1,29 +1,61 @@ resource "aws_vpc_peering_connection" "default" { + provider = aws.requestor count = module.this.enabled ? 1 : 0 vpc_id = join("", data.aws_vpc.requestor[*].id) peer_vpc_id = join("", data.aws_vpc.acceptor[*].id) + peer_owner_id = data.aws_caller_identity.acceptor[0].account_id + peer_region = data.aws_region.acceptor[0].name + auto_accept = false - auto_accept = var.auto_accept + tags = module.this.tags - accepter { - allow_remote_vpc_dns_resolution = var.acceptor_allow_remote_vpc_dns_resolution + timeouts { + create = var.create_timeout + update = var.update_timeout + delete = var.delete_timeout } +} + +# Options must be added after PCX is active +resource "aws_vpc_peering_connection_options" "default" { + provider = aws.requestor + count = module.this.enabled ? 1 : 0 + vpc_peering_connection_id = aws_vpc_peering_connection.default[0].id requester { allow_remote_vpc_dns_resolution = var.requestor_allow_remote_vpc_dns_resolution } - tags = module.this.tags + depends_on = [aws_vpc_peering_connection_accepter.default] +} - timeouts { - create = var.create_timeout - update = var.update_timeout - delete = var.delete_timeout +# Accepter's side of the connection. +resource "aws_vpc_peering_connection_accepter" "default" { + provider = aws.acceptor + count = module.this.enabled ? 1 : 0 + vpc_peering_connection_id = aws_vpc_peering_connection.default[0].id + auto_accept = var.auto_accept + + accepter { + allow_remote_vpc_dns_resolution = var.acceptor_allow_remote_vpc_dns_resolution } + + tags = module.this.tags +} + +data "aws_region" "acceptor" { + count = module.this.enabled ? 1 : 0 + provider = aws.acceptor +} + +data "aws_caller_identity" "acceptor" { + count = module.this.enabled ? 1 : 0 + provider = aws.acceptor } # Lookup requestor VPC so that we can reference the CIDR data "aws_vpc" "requestor" { + provider = aws.requestor count = module.this.enabled ? 1 : 0 id = var.requestor_vpc_id tags = var.requestor_vpc_tags @@ -31,18 +63,21 @@ data "aws_vpc" "requestor" { # Lookup acceptor VPC so that we can reference the CIDR data "aws_vpc" "acceptor" { - count = module.this.enabled ? 1 : 0 - id = var.acceptor_vpc_id - tags = var.acceptor_vpc_tags + provider = aws.acceptor + count = module.this.enabled ? 1 : 0 + id = var.acceptor_vpc_id + tags = var.acceptor_vpc_tags } data "aws_route_tables" "requestor" { + provider = aws.requestor count = module.this.enabled ? 1 : 0 vpc_id = join("", data.aws_vpc.requestor[*].id) tags = var.requestor_route_table_tags } data "aws_route_tables" "acceptor" { + provider = aws.acceptor count = module.this.enabled ? 1 : 0 vpc_id = join("", data.aws_vpc.acceptor[*].id) tags = var.acceptor_route_table_tags @@ -59,6 +94,7 @@ locals { # Create routes from requestor to acceptor resource "aws_route" "requestor" { + provider = aws.requestor count = module.this.enabled ? length(distinct(sort(data.aws_route_tables.requestor[0].ids))) * length(local.acceptor_cidr_blocks) : 0 route_table_id = element(distinct(sort(data.aws_route_tables.requestor[0].ids)), ceil(count.index / length(local.acceptor_cidr_blocks))) destination_cidr_block = local.acceptor_cidr_blocks[count.index % length(local.acceptor_cidr_blocks)] @@ -68,6 +104,7 @@ resource "aws_route" "requestor" { # Create routes from acceptor to requestor resource "aws_route" "acceptor" { + provider = aws.acceptor count = module.this.enabled ? length(distinct(sort(data.aws_route_tables.acceptor[0].ids))) * length(local.requestor_cidr_blocks) : 0 route_table_id = element(distinct(sort(data.aws_route_tables.acceptor[0].ids)), ceil(count.index / length(local.requestor_cidr_blocks))) destination_cidr_block = local.requestor_cidr_blocks[count.index % length(local.requestor_cidr_blocks)] diff --git a/versions.tf b/versions.tf index 29ec41d..1c18412 100644 --- a/versions.tf +++ b/versions.tf @@ -5,6 +5,7 @@ terraform { aws = { source = "hashicorp/aws" version = ">= 5.0" + configuration_aliases = [ aws.requestor, aws.acceptor ] } } }