diff --git a/terraform-modules/aws/vpc-peering/README.md b/terraform-modules/aws/vpc-peering/README.md new file mode 100644 index 000000000..0e6be4619 --- /dev/null +++ b/terraform-modules/aws/vpc-peering/README.md @@ -0,0 +1,43 @@ +## Requirements + +No requirements. + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | n/a | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [aws_route_table_association.association](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | +| [aws_vpc_endpoint_route_table_association.endpoint_association](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_endpoint_route_table_association) | resource | +| [aws_vpc_peering_connection.peering_connection](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_peering_connection) | resource | +| [aws_route_table.route_table](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_table) | data source | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [accepter\_allow\_remote\_vpc\_dns\_resolution](#input\_accepter\_allow\_remote\_vpc\_dns\_resolution) | Specifies whether DNS resolution is enabled for the VPC peering connection | `bool` | `true` | no | +| [auto\_accept](#input\_auto\_accept) | Specifies whether the peering connection should be automatically accepted | `bool` | `true` | no | +| [peer\_vpc\_id](#input\_peer\_vpc\_id) | The ID of the VPC with which you are creating the VPC Peering Connection. | `string` | n/a | yes | +| [requester\_allow\_remote\_vpc\_dns\_resolution](#input\_requester\_allow\_remote\_vpc\_dns\_resolution) | Specifies whether DNS resolution is enabled for the VPC peering connection | `bool` | `true` | no | +| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs for which to retrieve the associated route tables | `list(string)` | n/a | yes | +| [tags](#input\_tags) | A map of tags to apply to the VPC peering connection | `map(any)` | `{}` | no | +| [vpc\_id](#input\_vpc\_id) | The ID of the VPC in which the subnets are located | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [aws\_route\_table\_association\_id](#output\_aws\_route\_table\_association\_id) | The ID of the association | +| [aws\_vpc\_endpoint\_route\_table\_association\_id](#output\_aws\_vpc\_endpoint\_route\_table\_association\_id) | A hash of the EC2 Route Table and VPC Endpoint identifiers. | +| [vpc\_peering\_connection\_accept\_status](#output\_vpc\_peering\_connection\_accept\_status) | The status of the VPC Peering Connection request | +| [vpc\_peering\_connection\_id](#output\_vpc\_peering\_connection\_id) | The ID of the VPC Peering Connection | diff --git a/terraform-modules/aws/vpc-peering/main.tf b/terraform-modules/aws/vpc-peering/main.tf new file mode 100644 index 000000000..fb8e1804e --- /dev/null +++ b/terraform-modules/aws/vpc-peering/main.tf @@ -0,0 +1,36 @@ +# Create peering connection between VPC A and VPC B +resource "aws_vpc_peering_connection" "peering_connection" { + vpc_id = var.vpc_id + peer_vpc_id = var.peer_vpc_id + auto_accept = var.auto_accept + + accepter { + allow_remote_vpc_dns_resolution = var.accepter_allow_remote_vpc_dns_resolution + } + + requester { + allow_remote_vpc_dns_resolution = var.requester_allow_remote_vpc_dns_resolution + } + tags = var.tags +} + +data "aws_route_table" "route_table" { + for_each = { for id in var.subnet_ids : id => id } + subnet_id = each.value + vpc_id = var.vpc_id +} + +# Assign route table to corresponding subnets in VPC B +resource "aws_route_table_association" "association" { + for_each = { for id in var.subnet_ids : id => id } + subnet_id = each.value + route_table_id = data.aws_route_table.route_table[each.key].id +} + +# Create endpoint access policy for VPC B +resource "aws_vpc_endpoint_route_table_association" "endpoint_association" { + for_each = { for id in var.subnet_ids : id => id } + vpc_endpoint_id = var.vpc_endpoint_id + route_table_id = data.aws_route_table.route_table[each.key].id +} + diff --git a/terraform-modules/aws/vpc-peering/outputs.tf b/terraform-modules/aws/vpc-peering/outputs.tf new file mode 100644 index 000000000..f51c18d27 --- /dev/null +++ b/terraform-modules/aws/vpc-peering/outputs.tf @@ -0,0 +1,9 @@ +output "vpc_peering_connection_id" { + description = "The ID of the VPC Peering Connection" + value = aws_vpc_peering_connection.peering_connection.id +} + +output "vpc_peering_connection_accept_status" { + description = "The status of the VPC Peering Connection request" + value = aws_vpc_peering_connection.peering_connection.accept_status +} diff --git a/terraform-modules/aws/vpc-peering/variables.tf b/terraform-modules/aws/vpc-peering/variables.tf new file mode 100644 index 000000000..f4e3af193 --- /dev/null +++ b/terraform-modules/aws/vpc-peering/variables.tf @@ -0,0 +1,43 @@ +variable "vpc_id" { + type = string + description = "The ID of the VPC in which the subnets are located" +} + +variable "peer_vpc_id" { + description = "The ID of the VPC with which you are creating the VPC Peering Connection." + type = string +} + +variable "auto_accept" { + description = "Specifies whether the peering connection should be automatically accepted" + type = bool + default = true +} + +variable "accepter_allow_remote_vpc_dns_resolution" { + description = "Specifies whether DNS resolution is enabled for the VPC peering connection" + type = bool + default = true +} + +variable "requester_allow_remote_vpc_dns_resolution" { + description = "Specifies whether DNS resolution is enabled for the VPC peering connection" + type = bool + default = true +} + +variable "subnet_ids" { + type = list(string) + description = "A list of subnet IDs for which to retrieve the associated route tables" +} + +variable "vpc_endpoint_id" { + type = string + description = "VPC endpoint Id where you want to point" +} + +variable "tags" { + description = "A map of tags to apply to the VPC peering connection" + type = map(any) + default = {} +} \ No newline at end of file