Skip to content

Commit fc22015

Browse files
committed
fix: update VPC endpoint checks and outputs for existing endpoints to use single ID instead of list
1 parent 51795c2 commit fc22015

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

main.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
# backend "s3" {}
2+
backend "s3" {}
33
}
44

55
# =============================================================================
@@ -112,13 +112,13 @@ locals {
112112

113113
# VPC endpoint existence checks (only valid when checking is enabled)
114114
existing_ssm_endpoint_exists = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? (
115-
length(data.aws_vpc_endpoint.existing_ssm[0].ids) > 0
115+
data.aws_vpc_endpoint.existing_ssm[0].id != null
116116
) : false
117117
existing_ec2messages_endpoint_exists = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? (
118-
length(data.aws_vpc_endpoint.existing_ec2messages[0].ids) > 0
118+
data.aws_vpc_endpoint.existing_ec2messages[0].id != null
119119
) : false
120120
existing_ssmmessages_endpoint_exists = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? (
121-
length(data.aws_vpc_endpoint.existing_ssmmessages[0].ids) > 0
121+
data.aws_vpc_endpoint.existing_ssmmessages[0].id != null
122122
) : false
123123

124124
# Determine if we need to create security group for VPC endpoints
@@ -437,29 +437,29 @@ data "aws_vpc_endpoint" "existing_ssmmessages" {
437437
# Get VPC endpoint service data for SSM (only if we need to create endpoints)
438438
data "aws_vpc_endpoint_service" "ssm" {
439439
count = var.create_vpc_endpoints && (
440-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssm[0].ids) == 0 : true
440+
var.check_for_existing_vpc_endpoints ? !local.existing_ssm_endpoint_exists : true
441441
) ? 1 : 0
442442
service = "ssm"
443443
}
444444

445445
data "aws_vpc_endpoint_service" "ec2messages" {
446446
count = var.create_vpc_endpoints && (
447-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ec2messages[0].ids) == 0 : true
447+
var.check_for_existing_vpc_endpoints ? !local.existing_ec2messages_endpoint_exists : true
448448
) ? 1 : 0
449449
service = "ec2messages"
450450
}
451451

452452
data "aws_vpc_endpoint_service" "ssmmessages" {
453453
count = var.create_vpc_endpoints && (
454-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssmmessages[0].ids) == 0 : true
454+
var.check_for_existing_vpc_endpoints ? !local.existing_ssmmessages_endpoint_exists : true
455455
) ? 1 : 0
456456
service = "ssmmessages"
457457
}
458458

459459
# SSM VPC Endpoint
460460
resource "aws_vpc_endpoint" "ssm" {
461461
count = var.create_vpc_endpoints && (
462-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssm[0].ids) == 0 : true
462+
var.check_for_existing_vpc_endpoints ? !local.existing_ssm_endpoint_exists : true
463463
) ? 1 : 0
464464
vpc_id = data.aws_vpc.selected.id
465465
service_name = data.aws_vpc_endpoint_service.ssm[0].service_name
@@ -478,7 +478,7 @@ resource "aws_vpc_endpoint" "ssm" {
478478
# EC2Messages VPC Endpoint (only create if it doesn't exist)
479479
resource "aws_vpc_endpoint" "ec2messages" {
480480
count = var.create_vpc_endpoints && (
481-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ec2messages[0].ids) == 0 : true
481+
var.check_for_existing_vpc_endpoints ? !local.existing_ec2messages_endpoint_exists : true
482482
) ? 1 : 0
483483
vpc_id = data.aws_vpc.selected.id
484484
service_name = data.aws_vpc_endpoint_service.ec2messages[0].service_name
@@ -497,7 +497,7 @@ resource "aws_vpc_endpoint" "ec2messages" {
497497
# SSMMessages VPC Endpoint (only create if it doesn't exist)
498498
resource "aws_vpc_endpoint" "ssmmessages" {
499499
count = var.create_vpc_endpoints && (
500-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssmmessages[0].ids) == 0 : true
500+
var.check_for_existing_vpc_endpoints ? !local.existing_ssmmessages_endpoint_exists : true
501501
) ? 1 : 0
502502
vpc_id = data.aws_vpc.selected.id
503503
service_name = data.aws_vpc_endpoint_service.ssmmessages[0].service_name

outputs.tf

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -208,47 +208,47 @@ output "private_route_table_association_id" {
208208
# VPC ENDPOINT OUTPUTS
209209
# =============================================================================
210210

211-
output "existing_ssm_endpoint_ids" {
212-
description = "IDs of existing SSM VPC endpoints found in the VPC"
213-
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ssm[0].ids : []
211+
output "existing_ssm_endpoint_id" {
212+
description = "ID of existing SSM VPC endpoint found in the VPC"
213+
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ssm[0].id : null
214214
}
215215

216-
output "existing_ec2messages_endpoint_ids" {
217-
description = "IDs of existing EC2Messages VPC endpoints found in the VPC"
218-
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ec2messages[0].ids : []
216+
output "existing_ec2messages_endpoint_id" {
217+
description = "ID of existing EC2Messages VPC endpoint found in the VPC"
218+
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ec2messages[0].id : null
219219
}
220220

221-
output "existing_ssmmessages_endpoint_ids" {
222-
description = "IDs of existing SSMMessages VPC endpoints found in the VPC"
223-
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ssmmessages[0].ids : []
221+
output "existing_ssmmessages_endpoint_id" {
222+
description = "ID of existing SSMMessages VPC endpoint found in the VPC"
223+
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? data.aws_vpc_endpoint.existing_ssmmessages[0].id : null
224224
}
225225

226226
output "created_ssm_endpoint_id" {
227227
description = "ID of the SSM VPC endpoint created by this module (if any)"
228228
value = var.create_vpc_endpoints && (
229-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssm[0].ids) == 0 : true
229+
var.check_for_existing_vpc_endpoints ? !local.existing_ssm_endpoint_exists : true
230230
) ? aws_vpc_endpoint.ssm[0].id : null
231231
}
232232

233233
output "created_ec2messages_endpoint_id" {
234234
description = "ID of the EC2Messages VPC endpoint created by this module (if any)"
235235
value = var.create_vpc_endpoints && (
236-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ec2messages[0].ids) == 0 : true
236+
var.check_for_existing_vpc_endpoints ? !local.existing_ec2messages_endpoint_exists : true
237237
) ? aws_vpc_endpoint.ec2messages[0].id : null
238238
}
239239

240240
output "created_ssmmessages_endpoint_id" {
241241
description = "ID of the SSMMessages VPC endpoint created by this module (if any)"
242242
value = var.create_vpc_endpoints && (
243-
var.check_for_existing_vpc_endpoints ? length(data.aws_vpc_endpoint.existing_ssmmessages[0].ids) == 0 : true
243+
var.check_for_existing_vpc_endpoints ? !local.existing_ssmmessages_endpoint_exists : true
244244
) ? aws_vpc_endpoint.ssmmessages[0].id : null
245245
}
246246

247247
output "vpc_endpoints_reused" {
248248
description = "Boolean indicating if existing VPC endpoints were reused"
249249
value = var.create_vpc_endpoints && var.check_for_existing_vpc_endpoints ? (
250-
length(data.aws_vpc_endpoint.existing_ssm[0].ids) > 0 ||
251-
length(data.aws_vpc_endpoint.existing_ec2messages[0].ids) > 0 ||
252-
length(data.aws_vpc_endpoint.existing_ssmmessages[0].ids) > 0
250+
data.aws_vpc_endpoint.existing_ssm[0].id != null ||
251+
data.aws_vpc_endpoint.existing_ec2messages[0].id != null ||
252+
data.aws_vpc_endpoint.existing_ssmmessages[0].id != null
253253
) : false
254254
}

0 commit comments

Comments
 (0)