Skip to content

Commit 894ed13

Browse files
author
Benjamin Valiente (Contractor)
committed
feat: adding s3 vpc endpoint to private and public subnets
1 parent c0effa9 commit 894ed13

10 files changed

Lines changed: 100 additions & 0 deletions

File tree

aws/network/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ module "public_subnet_routes" {
8787
depends_on = [module.public_subnets, aws_internet_gateway.this]
8888
}
8989

90+
module "s3_endpoint" {
91+
count = var.create_s3_endpoint ? 1 : 0
92+
source = "./modules/vpc-endpoints/s3-vpc-endpoint"
93+
94+
name = var.name
95+
namespace = var.namespace
96+
vpc = local.vpc
97+
tags = var.tags
98+
99+
route_table_ids = concat(
100+
module.private_subnet_routes.route_table_ids,
101+
[module.public_subnet_routes.route_table.id],
102+
)
103+
104+
depends_on = [module.private_subnet_routes, module.public_subnet_routes]
105+
}
106+
90107
resource "aws_internet_gateway" "this" {
91108
count = var.create_internet_gateway ? 1 : 0
92109

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "route_tables" {
2+
description = "Map of per-AZ NAT route tables for private subnets"
3+
value = aws_route_table.nat
4+
}
5+
6+
output "route_table_ids" {
7+
description = "List of route table IDs for private subnets"
8+
value = [for rt in aws_route_table.nat : rt.id]
9+
}

aws/network/modules/vpc-endpoints/README.md

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AWS S3 VPC Endpoint
2+
3+
Module for creating a S3 VPC Endpoint in AWS.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "aws_vpc_endpoint" "s3" {
2+
vpc_id = var.vpc_id
3+
service_name = "com.amazonaws.${var.region}.s3"
4+
vpc_endpoint_type = "Gateway"
5+
6+
route_table_ids = var.route_table_ids
7+
8+
tags = merge(
9+
var.tags,
10+
{
11+
Name = "${var.name}-s3-endpoint"
12+
}
13+
)
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "endpoint_id" {
2+
description = "ID of the S3 Gateway VPC endpoint"
3+
value = aws_vpc_endpoint.s3.id
4+
}
5+
6+
output "prefix_list_id" {
7+
description = "Prefix list ID of the S3 endpoint (useful for security group egress rules)"
8+
value = aws_vpc_endpoint.s3.prefix_list_id
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "name" {
2+
type = string
3+
description = "Name for this network (used in the endpoint's Name tag)"
4+
}
5+
6+
variable "namespace" {
7+
type = list(string)
8+
description = "Prefix to be applied to created resources"
9+
default = []
10+
}
11+
12+
variable "vpc" {
13+
description = "The VPC object in which to create the S3 endpoint"
14+
type = object({
15+
id = string
16+
})
17+
}
18+
19+
variable "route_table_ids" {
20+
type = list(string)
21+
description = "List of route table IDs to associate with the S3 Gateway endpoint"
22+
}
23+
24+
variable "tags" {
25+
type = map(string)
26+
description = "Tags to be applied to the endpoint"
27+
default = {}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.6.2"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 6.0"
7+
}
8+
}
9+
}

aws/network/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ output "vpc_id" {
1212
description = "ID of the AWS VPC"
1313
value = local.vpc.id
1414
}
15+
16+
output "s3_endpoint_id" {
17+
description = "ID of the S3 Gateway VPC endpoint (null if disabled)"
18+
value = try(module.s3_endpoint[0].endpoint_id, null)
19+
}

aws/network/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,10 @@ variable "flow_logs_retention_days" {
9999
description = "Number of days to retain VPC Flow Logs in CloudWatch." # Only applies when enable_flow_logs is true.
100100
type = number
101101
default = null # null = retain forever
102+
}
103+
104+
variable "create_s3_endpoint" {
105+
description = "Set to false to disable creation of the S3 Gateway VPC endpoint"
106+
type = bool
107+
default = true
102108
}

0 commit comments

Comments
 (0)