Skip to content

Commit e651aec

Browse files
Merge branch 'release-v2.0.0' into 'main'
Added VPC pering functionality for release 2.0.0 See merge request sq-ia/aws/network!23
2 parents 0c73d85 + 2427774 commit e651aec

File tree

9 files changed

+262
-1
lines changed

9 files changed

+262
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ module "vpc" {
4343
4444
}
4545
```
46-
Refer [examples](https://github.com/squareops/terraform-aws-vpc/tree/main/examples) for all examples.
46+
Refer [this](https://github.com/squareops/terraform-aws-vpc/tree/main/examples) for more examples.
47+
4748

4849
## Important Note
4950
To prevent destruction interruptions, any resources that have been created outside of Terraform and attached to the resources provisioned by Terraform must be deleted before the module is destroyed.
@@ -78,6 +79,7 @@ This module supports three scenarios to create Network resource on AWS. Each wil
7879
- `flow_log_max_aggregation_interval = 60`
7980
- `flow_log_cloudwatch_log_group_retention_in_days = 90`
8081

82+
- **vpc-peering:** VPC peering support is available using submodule `vpc_peering`. Refer [Peering Docs](https://github.com/squareops/terraform-aws-vpc/tree/main/modules/vpc_peering) for more information
8183

8284
# IAM Permissions
8385
The required IAM permissions to create resources from this module can be found [here](https://github.com/squareops/terraform-aws-vpc/blob/main/IAM.md)

examples/peering/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# VPC Peering
2+
3+
Configuration in this directory creates a VPC peering connection between two VPCs.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which can cost money (AWS Elastic IP, for example). Run `terraform destroy` when you don't need these resources.
16+
17+
18+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
19+
## Requirements
20+
21+
No requirements.
22+
23+
## Providers
24+
25+
No providers.
26+
27+
## Modules
28+
29+
| Name | Source | Version |
30+
|------|--------|---------|
31+
| <a name="module_vpc_peering"></a> [vpc\_peering](#module\_vpc\_peering) | squareops/vpc/aws//modules/vpc-peering | n/a |
32+
33+
## Resources
34+
35+
No resources.
36+
37+
## Inputs
38+
39+
No inputs.
40+
41+
## Outputs
42+
43+
| Name | Description |
44+
|------|-------------|
45+
| <a name="output_vpc_peering_accept_status"></a> [vpc\_peering\_accept\_status](#output\_vpc\_peering\_accept\_status) | Accept status for the connection |
46+
| <a name="output_vpc_peering_connection_id"></a> [vpc\_peering\_connection\_id](#output\_vpc\_peering\_connection\_id) | Peering connection ID |
47+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/peering/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
locals {
2+
requester_vpc_region = "us-east-2"
3+
accepter_vpc_region = "us-east-2"
4+
accepter_vpc_id = "vpc-034d30be2f4d1skaf"
5+
requester_vpc_id = "vpc-0fbdbf97efdf3skaf"
6+
}
7+
8+
module "vpc_peering" {
9+
source = "squareops/vpc/aws//modules/vpc-peering"
10+
requester_vpc_region = local.requester_vpc_region
11+
accepter_vpc_region = local.accepter_vpc_region
12+
accepter_vpc_id = local.accepter_vpc_id
13+
requester_vpc_id = local.requester_vpc_id
14+
}

examples/peering/output.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "vpc_peering_connection_id" {
2+
description = "Peering connection ID"
3+
value = module.vpc_peering.vpc_peering_connection_id
4+
}
5+
6+
output "vpc_peering_accept_status" {
7+
description = "Accept status for the connection"
8+
value = module.vpc_peering.vpc_peering_accept_status
9+
}

modules/vpc_peering/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# VPC Peering
2+
3+
Module to create a VPC peering connection between two VPCs. Routes are also added to the route tables of both VPC to establish connection with peered VPC. Public DNS hostnames will be resolved to private IP addresses when queried from instances in the peer VPC.
4+
5+
Supported peering configurations:
6+
* Same account same region
7+
* Same account cross region
8+
9+
## Usage
10+
11+
To run this example you need to execute:
12+
13+
```bash
14+
$ terraform init
15+
$ terraform plan
16+
$ terraform apply
17+
```
18+
19+
Note that this example may create resources which can cost money (AWS Elastic IP, for example). Run `terraform destroy` when you don't need these resources.
20+
21+
22+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
23+
## Requirements
24+
25+
| Name | Version |
26+
|------|---------|
27+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
28+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.23 |
29+
30+
## Providers
31+
32+
| Name | Version |
33+
|------|---------|
34+
| <a name="provider_aws.accepter"></a> [aws.accepter](#provider\_aws.accepter) | >= 4.23 |
35+
| <a name="provider_aws.peer"></a> [aws.peer](#provider\_aws.peer) | >= 4.23 |
36+
37+
## Modules
38+
39+
No modules.
40+
41+
## Resources
42+
43+
| Name | Type |
44+
|------|------|
45+
| [aws_route.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
46+
| [aws_route.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
47+
| [aws_vpc_peering_connection.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_peering_connection) | resource |
48+
| [aws_vpc_peering_connection_accepter.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_peering_connection_accepter) | resource |
49+
| [aws_vpc_peering_connection_options.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_peering_connection_options) | resource |
50+
| [aws_route_tables.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
51+
| [aws_route_tables.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route_tables) | data source |
52+
| [aws_vpc.accepter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
53+
| [aws_vpc.requester](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
54+
55+
## Inputs
56+
57+
| Name | Description | Type | Default | Required |
58+
|------|-------------|------|---------|:--------:|
59+
| <a name="input_accepter_vpc_id"></a> [accepter\_vpc\_id](#input\_accepter\_vpc\_id) | The ID of Acceptor VPC | `string` | `""` | no |
60+
| <a name="input_accepter_vpc_region"></a> [accepter\_vpc\_region](#input\_accepter\_vpc\_region) | The region of Acceptor VPC | `string` | `""` | no |
61+
| <a name="input_requester_vpc_id"></a> [requester\_vpc\_id](#input\_requester\_vpc\_id) | The ID of Requester VPC | `string` | `""` | no |
62+
| <a name="input_requester_vpc_region"></a> [requester\_vpc\_region](#input\_requester\_vpc\_region) | The region Requester VPC | `string` | `""` | no |
63+
64+
## Outputs
65+
66+
| Name | Description |
67+
|------|-------------|
68+
| <a name="output_vpc_peering_accept_status"></a> [vpc\_peering\_accept\_status](#output\_vpc\_peering\_accept\_status) | Status for the connection |
69+
| <a name="output_vpc_peering_connection_id"></a> [vpc\_peering\_connection\_id](#output\_vpc\_peering\_connection\_id) | Peering connection ID |
70+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/vpc_peering/main.tf

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
locals {
2+
requester_route_tables_ids = data.aws_route_tables.requester.ids
3+
accepter_route_tables_ids = data.aws_route_tables.accepter.ids
4+
}
5+
6+
provider "aws" {
7+
alias = "peer"
8+
region = var.requester_vpc_region
9+
}
10+
11+
provider "aws" {
12+
alias = "accepter"
13+
region = var.accepter_vpc_region
14+
}
15+
16+
data "aws_vpc" "accepter" {
17+
id = var.accepter_vpc_id
18+
provider = aws.accepter
19+
}
20+
21+
data "aws_route_tables" "accepter" {
22+
vpc_id = var.accepter_vpc_id
23+
provider = aws.accepter
24+
}
25+
26+
data "aws_vpc" "requester" {
27+
id = var.requester_vpc_id
28+
provider = aws.peer
29+
}
30+
31+
data "aws_route_tables" "requester" {
32+
vpc_id = var.requester_vpc_id
33+
provider = aws.peer
34+
}
35+
36+
resource "aws_vpc_peering_connection" "this" {
37+
vpc_id = var.requester_vpc_id
38+
peer_vpc_id = var.accepter_vpc_id
39+
peer_region = var.accepter_vpc_region
40+
auto_accept = false
41+
provider = aws.peer
42+
}
43+
44+
resource "aws_vpc_peering_connection_accepter" "this" {
45+
depends_on = [aws_vpc_peering_connection.this]
46+
provider = aws.accepter
47+
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
48+
auto_accept = true
49+
}
50+
51+
resource "aws_vpc_peering_connection_options" "this" {
52+
depends_on = [aws_vpc_peering_connection_accepter.this]
53+
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
54+
accepter {
55+
allow_remote_vpc_dns_resolution = true
56+
}
57+
provider = aws.accepter
58+
59+
}
60+
61+
62+
#### route tables ####
63+
64+
resource "aws_route" "requester" {
65+
count = length(local.requester_route_tables_ids)
66+
route_table_id = local.requester_route_tables_ids[count.index]
67+
destination_cidr_block = data.aws_vpc.accepter.cidr_block
68+
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
69+
provider = aws.peer
70+
}
71+
72+
resource "aws_route" "accepter" {
73+
count = length(local.accepter_route_tables_ids)
74+
route_table_id = local.accepter_route_tables_ids[count.index]
75+
destination_cidr_block = data.aws_vpc.requester.cidr_block
76+
vpc_peering_connection_id = aws_vpc_peering_connection.this.id
77+
provider = aws.accepter
78+
}

modules/vpc_peering/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "vpc_peering_connection_id" {
2+
description = "Peering connection ID"
3+
value = aws_vpc_peering_connection.this.id
4+
}
5+
6+
output "vpc_peering_accept_status" {
7+
description = "Status for the connection"
8+
value = aws_vpc_peering_connection_accepter.this.accept_status
9+
}

modules/vpc_peering/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "accepter_vpc_id" {
2+
type = string
3+
description = "The ID of Acceptor VPC"
4+
default = ""
5+
}
6+
7+
variable "accepter_vpc_region" {
8+
type = string
9+
description = "The region of Acceptor VPC"
10+
default = ""
11+
}
12+
13+
variable "requester_vpc_id" {
14+
type = string
15+
description = "The ID of Requester VPC"
16+
default = ""
17+
}
18+
19+
variable "requester_vpc_region" {
20+
type = string
21+
description = "The region Requester VPC"
22+
default = ""
23+
}

modules/vpc_peering/versions.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 4.23"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)