-
Notifications
You must be signed in to change notification settings - Fork 47
/
vpce.tf
113 lines (93 loc) · 3.69 KB
/
vpce.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
locals {
region = var.vpc_endpoints_enabled && var.vpc_id != null ? split(":", data.aws_vpc.selected[0].arn)[3] : data.aws_region.current.name
subnets = var.vpc_endpoints_enabled ? var.subnet_ids != [] ? var.subnet_ids : data.aws_subnet_ids.selected[0].ids : []
}
data "aws_subnet_ids" "selected" {
count = var.vpc_endpoints_enabled ? 1 : 0
vpc_id = var.vpc_id
}
data "aws_route_table" "selected" {
count = var.vpc_endpoints_enabled ? length(local.subnets) : 0
subnet_id = sort(local.subnets)[count.index]
}
# SSM, EC2Messages, and SSMMessages endpoints are required for Session Manager
resource "aws_vpc_endpoint" "ssm" {
count = var.vpc_endpoints_enabled ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = local.subnets
service_name = "com.amazonaws.${local.region}.ssm"
vpc_endpoint_type = "Interface"
security_group_ids = [
aws_security_group.ssm_sg[0].id
]
private_dns_enabled = var.vpc_endpoint_private_dns_enabled
tags = var.tags
}
resource "aws_vpc_endpoint" "ec2messages" {
count = var.vpc_endpoints_enabled ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = local.subnets
service_name = "com.amazonaws.${local.region}.ec2messages"
vpc_endpoint_type = "Interface"
security_group_ids = [
aws_security_group.ssm_sg[0].id,
]
private_dns_enabled = var.vpc_endpoint_private_dns_enabled
tags = var.tags
}
resource "aws_vpc_endpoint" "ssmmessages" {
count = var.vpc_endpoints_enabled ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = local.subnets
service_name = "com.amazonaws.${local.region}.ssmmessages"
vpc_endpoint_type = "Interface"
security_group_ids = [
aws_security_group.ssm_sg[0].id,
]
private_dns_enabled = var.vpc_endpoint_private_dns_enabled
tags = var.tags
}
# To write session logs to S3, an S3 endpoint is needed:
resource "aws_vpc_endpoint" "s3" {
count = var.vpc_endpoints_enabled && var.enable_log_to_s3 ? 1 : 0
vpc_id = var.vpc_id
service_name = "com.amazonaws.${local.region}.s3"
tags = var.tags
}
# Associate S3 Gateway Endpoint to VPC and Subnets
resource "aws_vpc_endpoint_route_table_association" "private_s3_route" {
count = var.vpc_endpoints_enabled && var.enable_log_to_s3 ? 1 : 0
vpc_endpoint_id = aws_vpc_endpoint.s3[0].id
route_table_id = data.aws_vpc.selected[0].main_route_table_id
}
resource "aws_vpc_endpoint_route_table_association" "private_s3_subnet_route" {
count = var.vpc_endpoints_enabled && var.enable_log_to_s3 ? length(data.aws_route_table.selected) : 0
vpc_endpoint_id = aws_vpc_endpoint.s3[0].id
route_table_id = data.aws_route_table.selected[count.index].id
}
# To write session logs to CloudWatch, a CloudWatch endpoint is needed
resource "aws_vpc_endpoint" "logs" {
count = var.vpc_endpoints_enabled && var.enable_log_to_cloudwatch ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = local.subnets
service_name = "com.amazonaws.${local.region}.logs"
vpc_endpoint_type = "Interface"
security_group_ids = [
aws_security_group.ssm_sg[0].id
]
private_dns_enabled = var.vpc_endpoint_private_dns_enabled
tags = var.tags
}
# To Encrypt/Decrypt, a KMS endpoint is needed
resource "aws_vpc_endpoint" "kms" {
count = var.vpc_endpoints_enabled ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = local.subnets
service_name = "com.amazonaws.${local.region}.kms"
vpc_endpoint_type = "Interface"
security_group_ids = [
aws_security_group.ssm_sg[0].id
]
private_dns_enabled = var.vpc_endpoint_private_dns_enabled
tags = var.tags
}