Skip to content

Commit bfcda3f

Browse files
committed
Avoids short-circuit issue where fileexists returns inconsistent results
In old versions of terraform, the ternary operator did not short-circuit, and so the `was_missing` logic was a hack to workaround both paths executing on every execution. The module needed the hack to avoid failing When the package existed previously vs when it did not and was being created by the module itself. In recent-ish version of terraform, including the module min version 1.5.7, the ternary operator will shortcircuit, so it will only execute the true OR false logic, rather than both. This makes the `was_missing` logic unnecessary. Further, in most recent versions of terraform, at least since 1.13.0, the `was_missing` logic resulted in a failure when separately building the package and then attempting to use the package in the same workflow. The failure occurred because terraform became more strict at enforcing that the return values of the `fileexists()` function be consistent between plan and apply phases. Fixes #698
1 parent 177ee12 commit bfcda3f

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

main.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ locals {
77

88
archive_filename = try(data.external.archive_prepare[0].result.filename, null)
99
archive_filename_string = local.archive_filename != null ? local.archive_filename : ""
10-
archive_was_missing = try(data.external.archive_prepare[0].result.was_missing, false)
1110

1211
# Use a generated filename to determine when the source code has changed.
1312
# filename - to get package from local
14-
filename = var.local_existing_package != null ? var.local_existing_package : (var.store_on_s3 ? null : local.archive_filename)
15-
was_missing = var.local_existing_package != null ? !fileexists(var.local_existing_package) : local.archive_was_missing
13+
filename = var.local_existing_package != null ? var.local_existing_package : (var.store_on_s3 ? null : local.archive_filename)
1614

1715
# s3_* - to get package from S3
1816
s3_bucket = var.s3_existing_package != null ? try(var.s3_existing_package.bucket, null) : (var.store_on_s3 ? var.s3_bucket : null)
@@ -55,7 +53,7 @@ resource "aws_lambda_function" "this" {
5553
}
5654

5755
filename = local.filename
58-
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
56+
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) ? filebase64sha256(local.filename) : null
5957

6058
s3_bucket = local.s3_bucket
6159
s3_key = local.s3_key
@@ -182,7 +180,7 @@ resource "aws_lambda_layer_version" "this" {
182180
skip_destroy = var.layer_skip_destroy
183181

184182
filename = local.filename
185-
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
183+
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) ? filebase64sha256(local.filename) : null
186184

187185
s3_bucket = local.s3_bucket
188186
s3_key = local.s3_key

0 commit comments

Comments
 (0)