From 4603283904a87a83ff8f04d25daa035acde1a49a Mon Sep 17 00:00:00 2001 From: Kyle Kotowick Date: Sun, 3 Oct 2021 10:53:23 -0400 Subject: [PATCH] Update value-changed module version and usage --- main.tf | 32 ++++++++++++++++++++------------ variables.tf | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/main.tf b/main.tf index 76e2ed2..f604433 100644 --- a/main.tf +++ b/main.tf @@ -1,6 +1,6 @@ module "state" { source = "Invicton-Labs/get-state/null" - version = "0.2.1" + version = "~> 0.2.1" } locals { @@ -10,29 +10,37 @@ locals { } } +// This module tracks when the trigger has changed, which it uses to determine if the create command should be run module "trigger_changed_create" { - source = "Invicton-Labs/value-changed/null" - version = "0.1.1" - value = local.trigger - depends = [ + source = "Invicton-Labs/value-changed/null" + version = "~> 0.2.0" + value = local.trigger + consider_first_use_as_changed = true + save_depends_on = [ data.aws_lambda_invocation.shell_create ] } +// This module also tracks when the trigger has changed, which it uses to determine if the destroy command should be run module "trigger_changed_destroy" { source = "Invicton-Labs/value-changed/null" - version = "0.1.1" + version = "~> 0.2.0" value = local.trigger - depends = [ + // Don't try to destroy anything if there's no value in state yet + consider_first_use_as_changed = false + save_depends_on = [ data.aws_lambda_invocation.shell_destroy ] } locals { create = module.trigger_changed_create.changed - // Only run the destroy if the inputs have changed since the last successful run AND the old value wasn't null (which would imply that this is the first create; there's nothing to destroy) AND a destroy command has been provided AND the last successful trigger is the same as the last stored trigger - destroy = module.trigger_changed_destroy.changed && module.trigger_changed_destroy.old_value != null && var.destroy_command != null && (module.state_keeper.existing_value != null ? module.state_keeper.existing_value.trigger == module.trigger_changed_destroy.old_value : false) - // The uuid() function waits for the apply phase before it knows the value + // Only run the destroy if the inputs have changed since the last successful run AND a destroy command has been provided AND the last successful trigger is the same as the last stored trigger + destroy = module.trigger_changed_destroy.changed && var.destroy_command != null && (module.state_keeper.existing_value != null ? module.state_keeper.existing_value.trigger == module.trigger_changed_destroy.old_value : false) + // The uuid() function waits for the apply phase before it knows the value. + // We only want to force it to wait for the apply phase if something actually needs + // to be created/destroyed. Otherwise, run it during the plan phase (with a fake value) + // so it doesn't appear as "will be read" in the plan. wait_for_apply_create = local.create ? uuid() : true wait_for_apply_destroy = local.destroy ? uuid() : true } @@ -43,7 +51,7 @@ data "aws_lambda_invocation" "shell_create" { depends_on = [ var.lambda_shell_module, data.aws_lambda_invocation.shell_destroy, - module.trigger_changed_destroy + module.trigger_changed_destroy, ] // Force it to wait for the apply step, and also wait for the destroy to complete if the destroy should be done function_name = local.wait_for_apply_create == null ? var.lambda_shell_module.invicton_labs_lambda_shell_arn : var.lambda_shell_module.invicton_labs_lambda_shell_arn @@ -83,7 +91,7 @@ locals { // This keeps the output value in state until it's re-created module "state_keeper" { source = "Invicton-Labs/state-keeper/null" - version = "0.1.1" + version = "~> 0.1.1" // This convoluted expression just forces the shell_destroy data source to actually execute. Since the value isn't being returned anywhere, // the data source won't be executed unless you tell it to use the output value in order to evaluate a conditional expression like this ternary one. input = local.destroy ? (data.aws_lambda_invocation.shell_create.result == null ? local.create_result : local.create_result) : local.create_result diff --git a/variables.tf b/variables.tf index 2f0a08b..e1ef207 100644 --- a/variables.tf +++ b/variables.tf @@ -1,5 +1,5 @@ variable "lambda_shell_module" { - description = "An `Invicton-Labs/lambda-shell/aws` module (https://registry.terraform.io/modules/Invicton-Labs/request-signer/aws). Pass the entire module in as this parameter (e.g. `lambda_shell_module = module.lambda-shell`)." + description = "An `Invicton-Labs/lambda-shell/aws` module (https://registry.terraform.io/modules/Invicton-Labs/lambda-shell/aws). Pass the entire module in as this parameter (e.g. `lambda_shell_module = module.lambda-shell`)." type = object({ invicton_labs_lambda_shell_arn = string })