-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccepter.tf
154 lines (132 loc) · 4.45 KB
/
accepter.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
variable "accepter_aws_assume_role_arn" {
description = "Accepter AWS Assume Role ARN"
type = string
}
variable "accepter_region" {
type = string
description = "Accepter AWS region"
}
variable "accepter_vpc_id" {
type = string
description = "Accepter VPC ID filter"
default = ""
}
variable "accepter_vpc_tags" {
type = map(string)
description = "Accepter VPC Tags filter"
default = {}
}
variable "accepter_allow_remote_vpc_dns_resolution" {
default = "true"
description = "Allow accepter VPC to resolve public DNS hostnames to private IP addresses when queried from instances in the requester VPC"
}
# Accepter's credentials
provider "aws" {
alias = "accepter"
region = var.accepter_region
version = ">= 1.25"
assume_role {
role_arn = var.accepter_aws_assume_role_arn
}
}
locals {
accepter_attributes = concat(var.attributes, ["accepter"])
accepter_tags = merge(
var.tags,
{
"Side" = "accepter"
},
)
}
module "accepter" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
enabled = var.enabled
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
attributes = local.accepter_attributes
tags = local.accepter_tags
}
data "aws_caller_identity" "accepter" {
count = local.count
provider = aws.accepter
}
data "aws_region" "accepter" {
count = local.count
provider = aws.accepter
}
# Lookup accepter's VPC so that we can reference the CIDR
data "aws_vpc" "accepter" {
count = local.count
provider = aws.accepter
id = var.accepter_vpc_id
tags = var.accepter_vpc_tags
}
# Lookup accepter subnets
data "aws_subnet_ids" "accepter" {
count = local.count
provider = aws.accepter
vpc_id = local.accepter_vpc_id
}
locals {
accepter_subnet_ids = distinct(sort(flatten(data.aws_subnet_ids.accepter.*.ids)))
accepter_subnet_ids_count = length(local.accepter_subnet_ids)
accepter_vpc_id = join("", data.aws_vpc.accepter.*.id)
accepter_account_id = join("", data.aws_caller_identity.accepter.*.account_id)
accepter_region = join("", data.aws_region.accepter.*.name)
}
# Lookup accepter route tables
data "aws_route_tables" "accepter" {
count = local.count
provider = aws.accepter
vpc_id = local.accepter_vpc_id
}
locals {
accepter_aws_route_table_ids = distinct(sort(data.aws_route_tables.accepter[0].ids))
accepter_aws_route_table_ids_count = length(local.accepter_aws_route_table_ids)
accepter_cidr_block_associations = flatten(data.aws_vpc.accepter.*.cidr_block_associations)
accepter_cidr_block_associations_count = length(local.accepter_cidr_block_associations)
}
# Create routes from accepter to requester
resource "aws_route" "accepter" {
count = local.enabled ? local.accepter_aws_route_table_ids_count * local.requester_cidr_block_associations_count : 0
provider = aws.accepter
route_table_id = element(
local.accepter_aws_route_table_ids,
ceil(count.index / local.requester_cidr_block_associations_count),
)
destination_cidr_block = local.requester_cidr_block_associations[count.index % local.requester_cidr_block_associations_count]["cidr_block"]
vpc_peering_connection_id = join("", aws_vpc_peering_connection.requester.*.id)
depends_on = [
data.aws_route_tables.accepter,
aws_vpc_peering_connection_accepter.accepter,
aws_vpc_peering_connection.requester,
]
}
# Accepter's side of the connection.
resource "aws_vpc_peering_connection_accepter" "accepter" {
count = local.count
provider = aws.accepter
vpc_peering_connection_id = join("", aws_vpc_peering_connection.requester.*.id)
auto_accept = var.auto_accept
tags = module.accepter.tags
}
resource "aws_vpc_peering_connection_options" "accepter" {
provider = aws.accepter
vpc_peering_connection_id = join("", aws_vpc_peering_connection.requester.*.id)
accepter {
allow_remote_vpc_dns_resolution = var.accepter_allow_remote_vpc_dns_resolution
}
}
output "accepter_connection_id" {
value = join("", aws_vpc_peering_connection_accepter.accepter.*.id)
description = "Accepter VPC peering connection ID"
}
output "accepter_accept_status" {
value = join(
"",
aws_vpc_peering_connection_accepter.accepter.*.accept_status,
)
description = "Accepter VPC peering connection request status"
}