Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

break out s3 endpoint service into tag based calls for interface and … #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@ data "aws_vpc_endpoint_service" "this" {
service_name = length(regexall(data.aws_region.selected.name, each.key)) == 1 ? each.key : "com.amazonaws.${data.aws_region.selected.name}.${each.key}"
}

data "aws_vpc_endpoint_service" "s3_gateway" {
service_name = "com.amazonaws.${data.aws_region.selected.name}.s3"

tags = {
"vpc-endpoint-type" : "gateway"
}
}

data "aws_vpc_endpoint_service" "s3_interface" {
service_name = "com.amazonaws.${data.aws_region.selected.name}.s3"

tags = {
"vpc-endpoint-type" : "interface"
}
}

locals {
vpc_id = join("", data.aws_subnet.selected.*.vpc_id)

# Split Endpoints by their type
gateway_endpoints = toset([for e in data.aws_vpc_endpoint_service.this : e.service_name if e.service_type == "Gateway"])
interface_endpoints = toset([for e in data.aws_vpc_endpoint_service.this : e.service_name if e.service_type == "Interface"])
service_gateway_endpoints = toset([for e in data.aws_vpc_endpoint_service.this : e.service_name if e.service_type == "Gateway"])
service_interface_endpoints = toset([for e in data.aws_vpc_endpoint_service.this : e.service_name if e.service_type == "Interface"])

s3_gateway_endpoint = local.s3_service_found ? [data.aws_vpc_endpoint_service.s3_gateway] : []
s3_interface_endpoint = local.s3_service_found ? [data.aws_vpc_endpoint_service.s3_interface] : []

gateway_endpoints = concat(local.service_gateway_endpoints, local.s3_gateway_endpoint)
interfrace_endpoints = concat(local.service_interface_endpoints, local.s3_gateway_endpoint)

# Only Interface Endpoints support SGs
security_groups = toset(var.create_vpc_endpoints ? var.create_sg_per_endpoint ? local.interface_endpoints : ["shared"] : [])
Expand Down
17 changes: 17 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ variable "tags" {
default = {}
}

locals {
# if list contains s3

s3_service_found = contains(var.vpc_endpoint_services, "s3")

# slice out everything but the s3 bit

s3_service_index = local.s3_service_found ? index(var.vpc_endpoint_services, "s3") : -1

# assumes s3 index not at ends
pre_s3_part = local.s3_service_found ? slice(var.vpc_endpoint_services, 0, local.s3_service_index) : []
post_s3_part = local.s3_service_found ? slice(var.vpc_endpoint_services, local.s3_service_index + 1, length(var.vpc_endpoint_services) - 1) : var.vpc_endpoint_services

# merge non-s3 pieces together

vpc_endpoint_services = concat(local.pre_s3_part, local.post_s3_part)
}