Skip to content

Commit ccddccc

Browse files
committed
feat: adding s3 and dynamodb gateway endpoints by default
1 parent d8187b6 commit ccddccc

File tree

6 files changed

+103
-63
lines changed

6 files changed

+103
-63
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ module "vpc" {
138138
}
139139
```
140140

141+
Note, by default `Gateway` endpoints are automatically created for S3 and DynamoDB, though these can be controlled by the `enable_s3_endpoint` and `enable_dynamodb_endpoint` variables.
142+
141143
You can use `enable_ssm` as a shortcut to enable the SSM endpoints.
142144

143145
```hcl
@@ -338,8 +340,10 @@ The `terraform-docs` utility is used to generate this README. Follow the below s
338340
| <a name="input_enable_default_route_table_association"></a> [enable\_default\_route\_table\_association](#input\_enable\_default\_route\_table\_association) | Indicates the transit gateway default route table should be associated with the subnets | `bool` | `true` | no |
339341
| <a name="input_enable_default_route_table_propagation"></a> [enable\_default\_route\_table\_propagation](#input\_enable\_default\_route\_table\_propagation) | Indicates the transit gateway default route table should be propagated to the subnets | `bool` | `true` | no |
340342
| <a name="input_enable_dns_request_logging"></a> [enable\_dns\_request\_logging](#input\_enable\_dns\_request\_logging) | Enable logging of DNS requests | `bool` | `false` | no |
343+
| <a name="input_enable_dynamodb_endpoint"></a> [enable\_dynamodb\_endpoint](#input\_enable\_dynamodb\_endpoint) | Enable DynamoDB VPC Gateway endpoint | `bool` | `false` | no |
341344
| <a name="input_enable_private_endpoints"></a> [enable\_private\_endpoints](#input\_enable\_private\_endpoints) | Indicates the network should provision private endpoints | `list(string)` | `[]` | no |
342345
| <a name="input_enable_route53_resolver_rules"></a> [enable\_route53\_resolver\_rules](#input\_enable\_route53\_resolver\_rules) | Automatically associates any shared route53 resolver rules with the VPC | `bool` | `true` | no |
346+
| <a name="input_enable_s3_endpoint"></a> [enable\_s3\_endpoint](#input\_enable\_s3\_endpoint) | Enable S3 VPC Gateway endpoint | `bool` | `false` | no |
343347
| <a name="input_enable_ssm"></a> [enable\_ssm](#input\_enable\_ssm) | Indicates we should provision SSM private endpoints | `bool` | `false` | no |
344348
| <a name="input_enable_transit_gateway_appliance_mode"></a> [enable\_transit\_gateway\_appliance\_mode](#input\_enable\_transit\_gateway\_appliance\_mode) | Indicates the network should be connected to a transit gateway in appliance mode | `bool` | `false` | no |
345349
| <a name="input_enable_transit_gateway_subnet_natgw"></a> [enable\_transit\_gateway\_subnet\_natgw](#input\_enable\_transit\_gateway\_subnet\_natgw) | Indicates if the transit gateway subnets should be connected to a nat gateway | `bool` | `false` | no |

dns.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Related to the DNS request logging and resolver rules
2+
3+
## Enable DNS request logging if required
4+
resource "aws_cloudwatch_log_group" "dns_query_logs" {
5+
count = var.enable_dns_request_logging ? 1 : 0
6+
7+
name = "/aws/route53/${var.name}/dns-query-logs"
8+
retention_in_days = var.dns_query_log_retention
9+
tags = local.tags
10+
}
11+
12+
## Create the DNS query log config
13+
resource "aws_route53_resolver_query_log_config" "dns_query_log_config" {
14+
count = var.enable_dns_request_logging ? 1 : 0
15+
16+
name = "${var.name}-dns-query-logs"
17+
destination_arn = aws_cloudwatch_log_group.dns_query_logs[0].arn
18+
}
19+
20+
## Associate the DNS query log config with the VPC
21+
resource "aws_route53_resolver_query_log_config_association" "dns_query_log_association" {
22+
count = var.enable_dns_request_logging ? 1 : 0
23+
24+
resolver_query_log_config_id = aws_route53_resolver_query_log_config.dns_query_log_config[0].id
25+
resource_id = module.vpc.vpc_attributes.id
26+
}
27+
28+
## Associate any resolver rules with the vpc if required
29+
resource "aws_route53_resolver_rule_association" "vpc_associations" {
30+
for_each = var.enable_route53_resolver_rules ? toset(local.resolver_rules) : []
31+
32+
resolver_rule_id = each.value
33+
vpc_id = module.vpc.vpc_attributes.id
34+
}

endpoints.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
## Provision the S3 endpoint
3+
resource "aws_vpc_endpoint" "s3" {
4+
count = var.enable_s3_endpoint ? 1 : 0
5+
6+
route_table_ids = local.public_route_table_ids
7+
service_name = "com.amazonaws.${local.region}.s3"
8+
tags = merge(local.tags, { Name = "vpce-s3-${var.name}" })
9+
vpc_endpoint_type = "Gateway"
10+
vpc_id = module.vpc.vpc_attributes.id
11+
}
12+
13+
## Provision the DynamoDB endpoint
14+
resource "aws_vpc_endpoint" "dynamodb" {
15+
count = var.enable_dynamodb_endpoint ? 1 : 0
16+
17+
route_table_ids = local.public_route_table_ids
18+
service_name = "com.amazonaws.${local.region}.dynamodb"
19+
tags = merge(local.tags, { Name = "vpce-dynamodb-${var.name}" })
20+
vpc_endpoint_type = "Gateway"
21+
vpc_id = module.vpc.vpc_attributes.id
22+
}
23+
24+
## Provision the security groups for the private links
25+
module "private_links" {
26+
source = "terraform-aws-modules/security-group/aws"
27+
version = "5.3.0"
28+
count = length(local.enabled_endpoints) > 0 ? 1 : 0
29+
30+
description = "Provides the security groups for the private links access"
31+
ingress_rules = ["https-443-tcp"]
32+
ingress_cidr_blocks = local.private_subnet_cidrs
33+
name = "vpce-${var.name}"
34+
tags = local.tags
35+
vpc_id = module.vpc.vpc_attributes.id
36+
}
37+
38+
## Provision any private endpoints
39+
resource "aws_vpc_endpoint" "vpe_endpoints" {
40+
for_each = toset(local.enabled_endpoints)
41+
42+
private_dns_enabled = true
43+
security_group_ids = [module.private_links[0].security_group_id]
44+
service_name = "com.amazonaws.${local.region}.${each.value}"
45+
subnet_ids = local.private_subnet_ids
46+
tags = merge(local.tags, { Name = "vpce-${each.value}-${var.name}" })
47+
vpc_endpoint_type = "Interface"
48+
vpc_id = module.vpc.vpc_attributes.id
49+
}

locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ locals {
4242
}
4343
} : null
4444

45+
## A collection of all the tags for all the resources
46+
tags = merge(var.tags, {})
4547
# A map of all the subnets by name i.e. private/us-east-1a, public/us-east-1a, etc.
4648
all_subnets = merge(module.vpc.private_subnet_attributes_by_az, module.vpc.public_subnet_attributes_by_az)
4749
## A list of all the names of the subnets

main.tf

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module "vpc" {
77
az_count = var.availability_zones
88
cidr_block = var.vpc_cidr
99
subnets = local.subnets
10-
tags = var.tags
10+
tags = local.tags
1111
transit_gateway_id = local.transit_gateway_id
1212
transit_gateway_routes = local.transit_routes
1313
vpc_instance_tenancy = var.vpc_instance_tenancy
@@ -27,69 +27,8 @@ module "nacls" {
2727
outbound_rules = var.nacl_rules[each.key].outbound_rules
2828
subnet_count = var.availability_zones
2929
subnet_ids = local.all_subnets_by_name[each.key].ids
30-
tags = var.tags
30+
tags = local.tags
3131
vpc_id = module.vpc.vpc_attributes.id
3232

3333
depends_on = [module.vpc]
3434
}
35-
36-
## Enable DNS request logging if required
37-
resource "aws_cloudwatch_log_group" "dns_query_logs" {
38-
count = var.enable_dns_request_logging ? 1 : 0
39-
40-
name = "/aws/route53/${var.name}/dns-query-logs"
41-
retention_in_days = var.dns_query_log_retention
42-
tags = var.tags
43-
}
44-
45-
## Create the DNS query log config
46-
resource "aws_route53_resolver_query_log_config" "dns_query_log_config" {
47-
count = var.enable_dns_request_logging ? 1 : 0
48-
49-
name = "${var.name}-dns-query-logs"
50-
destination_arn = aws_cloudwatch_log_group.dns_query_logs[0].arn
51-
}
52-
53-
## Associate the DNS query log config with the VPC
54-
resource "aws_route53_resolver_query_log_config_association" "dns_query_log_association" {
55-
count = var.enable_dns_request_logging ? 1 : 0
56-
57-
resolver_query_log_config_id = aws_route53_resolver_query_log_config.dns_query_log_config[0].id
58-
resource_id = module.vpc.vpc_attributes.id
59-
}
60-
61-
## Associate any resolver rules with the vpc if required
62-
resource "aws_route53_resolver_rule_association" "vpc_associations" {
63-
for_each = var.enable_route53_resolver_rules ? toset(local.resolver_rules) : []
64-
65-
resolver_rule_id = each.value
66-
vpc_id = module.vpc.vpc_attributes.id
67-
}
68-
69-
## Provision the security groups for the private links
70-
module "private_links" {
71-
source = "terraform-aws-modules/security-group/aws"
72-
version = "5.3.0"
73-
count = length(local.enabled_endpoints) > 0 ? 1 : 0
74-
75-
description = "Provides the security groups for the private links access"
76-
ingress_rules = ["https-443-tcp"]
77-
ingress_cidr_blocks = local.private_subnet_cidrs
78-
name = "private-links-${var.name}"
79-
tags = var.tags
80-
vpc_id = module.vpc.vpc_attributes.id
81-
}
82-
83-
## Provision any private endpoints
84-
resource "aws_vpc_endpoint" "vpe_endpoints" {
85-
for_each = toset(local.enabled_endpoints)
86-
87-
private_dns_enabled = true
88-
security_group_ids = [module.private_links[0].security_group_id]
89-
service_name = "com.amazonaws.${local.region}.${each.value}"
90-
subnet_ids = local.private_subnet_ids
91-
tags = merge(var.tags, { Name = "vpe-${each.value}-${var.name}" })
92-
vpc_endpoint_type = "Interface"
93-
vpc_id = module.vpc.vpc_attributes.id
94-
}
95-

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,15 @@ variable "nacl_rules" {
187187
}))
188188
default = {}
189189
}
190+
191+
variable "enable_s3_endpoint" {
192+
description = "Enable S3 VPC Gateway endpoint"
193+
type = bool
194+
default = false
195+
}
196+
197+
variable "enable_dynamodb_endpoint" {
198+
description = "Enable DynamoDB VPC Gateway endpoint"
199+
type = bool
200+
default = false
201+
}

0 commit comments

Comments
 (0)