SimonB
SimonB

Reputation: 1055

aws_lambda_invocation > Conditional

I am passing some values to a lambda function, and then invoking it. It works great.

I have several of these depending on each other, and some resources in the depends chain too.

Invoke_1 > Invoke_2 > aws_glue_catalog_database > Invoke_3

However, the lambdas are listed out as 'read' in every plan, end executed on every apply.

Firstly, this makes the plan harder to read. The lengthy output (listing all the values I have passed) means it is harder to spot real changes to other resources.

Secondly, it's running a function that does not need to be run and that's just not clean.

So ..

Is there a way to make these invocations conditional in a way that does not mess up my dependency chain?

// zips the python file in order to be used by lambda
data "archive_file" "lf_settings_1_zip" {
  type        = "zip"
  source_dir  = "${path.module}/scripts/lf_settings_s1/"
  output_path = "${path.module}/scripts/s1deploy.zip"
}

resource "aws_lambda_function" "lakeformation_settings_1" {
  function_name    = "dlsettings_1"
  filename         = data.archive_file.lf_settings_1_zip.output_path
  source_code_hash = data.archive_file.lf_settings_1_zip.output_base64sha256
  role             = aws_iam_role.myRole.arn
  handler          = "main.main"
  runtime          = "python3.8"
}

locals {
  lf1_json = <<JSON
  {
            "account":"${local.account_id}",
            "principals":[
              "arn:aws:iam::${local.account_id}:role/Role1",
              "arn:aws:iam::${local.account_id}:role/Role2"
            ]
        }
  JSON
}

data "aws_lambda_invocation" "invo1" {
  function_name = "dlsettings_1"
  input         = local.lf1_json
  
  depends_on = [
    aws_lambda_invocation.invo0,
    aws_lambda_function.lakeformation_settings_1
  ]
}

Upvotes: 0

Views: 1960

Answers (1)

aldarisbm
aldarisbm

Reputation: 407

Data sources still allow for the count parameter. I'm not exactly sure how you would implement it with your current terraform code, but this is the idea. If the var.boolean_youcreate is set to false then it doesn't invoke it:

data "aws_lambda_invocation" "example" {
  count         = var.boolean_youcreate ? "1" : "0"
  function_name = aws_lambda_function.lambda_function_test.function_name

  input = <<JSON
{
  "key1": "value1",
  "key2": "value2"
}
JSON
}

output "result_entry" {
  value = jsondecode(data.aws_lambda_invocation.example.result)["key1"]
}

Upvotes: 2

Related Questions