@@ -59,6 +59,7 @@ module "vpc" {
5959
6060 # VPC endpoint for DynamoDB
6161 enable_dynamodb_endpoint = true
62+ dynamodb_endpoint_policy = data. aws_iam_policy_document . dynamodb_endpoint_policy . json
6263
6364 # VPC endpoint for SSM
6465 enable_ssm_endpoint = true
@@ -77,6 +78,7 @@ module "vpc" {
7778
7879 # VPC Endpoint for EC2
7980 enable_ec2_endpoint = true
81+ ec2_endpoint_policy = data. aws_iam_policy_document . generic_endpoint_policy . json
8082 ec2_endpoint_private_dns_enabled = true
8183 ec2_endpoint_security_group_ids = [data . aws_security_group . default . id ]
8284
@@ -87,11 +89,13 @@ module "vpc" {
8789
8890 # VPC Endpoint for ECR API
8991 enable_ecr_api_endpoint = true
92+ ecr_api_endpoint_policy = data. aws_iam_policy_document . generic_endpoint_policy . json
9093 ecr_api_endpoint_private_dns_enabled = true
9194 ecr_api_endpoint_security_group_ids = [data . aws_security_group . default . id ]
9295
9396 # VPC Endpoint for ECR DKR
9497 enable_ecr_dkr_endpoint = true
98+ ecr_dkr_endpoint_policy = data. aws_iam_policy_document . generic_endpoint_policy . json
9599 ecr_dkr_endpoint_private_dns_enabled = true
96100 ecr_dkr_endpoint_security_group_ids = [data . aws_security_group . default . id ]
97101
@@ -142,3 +146,49 @@ module "vpc" {
142146 Endpoint = " true"
143147 }
144148}
149+
150+ # Data source used to avoid race condition
151+ data "aws_vpc_endpoint" "dynamodb" {
152+ vpc_id = module. vpc . vpc_id
153+ service_name = " com.amazonaws.eu-west-1.dynamodb"
154+ }
155+
156+ data "aws_iam_policy_document" "dynamodb_endpoint_policy" {
157+ statement {
158+ effect = " Deny"
159+ actions = [" dynamodb:*" ]
160+ resources = [" *" ]
161+
162+ principals {
163+ type = " *"
164+ identifiers = [" *" ]
165+ }
166+
167+ condition {
168+ test = " StringNotEquals"
169+ variable = " aws:sourceVpce"
170+
171+ values = [data . aws_vpc_endpoint . dynamodb . id ]
172+ }
173+ }
174+ }
175+
176+ data "aws_iam_policy_document" "generic_endpoint_policy" {
177+ statement {
178+ effect = " Deny"
179+ actions = [" *" ]
180+ resources = [" *" ]
181+
182+ principals {
183+ type = " *"
184+ identifiers = [" *" ]
185+ }
186+
187+ condition {
188+ test = " StringNotEquals"
189+ variable = " aws:sourceVpce"
190+
191+ values = [data . aws_vpc_endpoint . dynamodb . id ]
192+ }
193+ }
194+ }
0 commit comments