Skip to content

Commit

Permalink
fix: Correct for_each map on VPC endpoints to propagate endpoint ma…
Browse files Browse the repository at this point in the history
…ps correctly (#729)
  • Loading branch information
bryantbiggs authored Jan 11, 2022
1 parent 5f5d877 commit 19fcf0d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 107 deletions.
1 change: 1 addition & 0 deletions examples/complete-vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Note that this example may create resources which can cost money (AWS Elastic IP

| Name | Type |
|------|------|
| [aws_security_group.vpc_tls](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
| [aws_iam_policy_document.dynamodb_endpoint_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.generic_endpoint_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_security_group.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group) | data source |
Expand Down
19 changes: 19 additions & 0 deletions examples/complete-vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module "vpc_endpoints" {
service = "ssm"
private_dns_enabled = true
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.vpc_tls.id]
},
ssmmessages = {
service = "ssmmessages"
Expand All @@ -127,6 +128,7 @@ module "vpc_endpoints" {
service = "ec2"
private_dns_enabled = true
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.vpc_tls.id]
},
ec2messages = {
service = "ec2messages"
Expand All @@ -149,6 +151,7 @@ module "vpc_endpoints" {
service = "kms"
private_dns_enabled = true
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.vpc_tls.id]
},
codedeploy = {
service = "codedeploy"
Expand Down Expand Up @@ -232,3 +235,19 @@ data "aws_iam_policy_document" "generic_endpoint_policy" {
}
}
}

resource "aws_security_group" "vpc_tls" {
name_prefix = "${local.name}-vpc_tls"
description = "Allow TLS inbound traffic"
vpc_id = module.vpc.vpc_id

ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [module.vpc.vpc_cidr_block]
}

tags = local.tags
}
8 changes: 2 additions & 6 deletions modules/vpc-endpoints/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
locals {
endpoints = var.create ? var.endpoints : tomap({})
}

################################################################################
# Endpoint(s)
################################################################################

data "aws_vpc_endpoint_service" "this" {
for_each = local.endpoints
for_each = { for k, v in var.endpoints : k => v if var.create }

service = lookup(each.value, "service", null)
service_name = lookup(each.value, "service_name", null)
Expand All @@ -19,7 +15,7 @@ data "aws_vpc_endpoint_service" "this" {
}

resource "aws_vpc_endpoint" "this" {
for_each = local.endpoints
for_each = { for k, v in var.endpoints : k => v if var.create }

vpc_id = var.vpc_id
service_name = data.aws_vpc_endpoint_service.this[each.key].service_name
Expand Down
Loading

0 comments on commit 19fcf0d

Please sign in to comment.