Skip to content

Commit

Permalink
Update value-changed module version and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKotowick committed Oct 3, 2021
1 parent 03efee3 commit 4603283
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "state" {
source = "Invicton-Labs/get-state/null"
version = "0.2.1"
version = "~> 0.2.1"
}

locals {
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
@@ -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
})
Expand Down

0 comments on commit 4603283

Please # to comment.