terraform-aws-modules / terraform-aws-lambda

Terraform module, which takes care of a lot of AWS Lambda/serverless tasks (build dependencies, packages, updates, deployments) in countless combinations 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws
Apache License 2.0
928 stars 688 forks source link

Lambda update layer version when managed externally when I expected to ignore it #273

Closed lays147 closed 2 years ago

lays147 commented 2 years ago

Description

Hi, I'm using this module to set up a lambda function and layer. The lambda and layer are updated in a separated pipeline. However, when running terraform pipeline again, the plan changes the lambda layer version back to the version in the terraform state, and I would like to ignore changes in the layer version. How does one do that? I know that can be done using lifecycle rules, but as I can't set it dynamically. Is there a workaround for this?

Versions

Reproduction

Steps to reproduce the behavior:

  1. Create a lambda layer
  2. Create a lambda function
  3. Run terraform plan & apply
  4. Update lambda layer version externally
  5. Update lambda function to use new lambda layer
  6. Run terraform plan
  7. The plan changes the lambda layer version to the one registered on the state

Code Snippet to Reproduce

module "lambda" {
  source                            = "terraform-aws-modules/lambda/aws"
  version                           = "v2.34.0"
  attach_policies                   = true
  cloudwatch_logs_retention_in_days = local.log_retention_days
  cloudwatch_logs_tags              = local.tags
  create_package                    = false
  function_name                     = local.project
  handler                           = local.handler
  ignore_source_code_hash           = true
  local_existing_package            = data.archive_file.dummy.output_path
  memory_size                       = local.lambda_memory
  runtime                           = "python3.9"
  number_of_policies                = 2
  policies = [
    aws_iam_policy.write_bucket.arn,
    aws_iam_policy.read_dynamo.arn
  ]
  publish               = true
  tags                  = local.tags
  environment_variables = { for env in local.environment : env.name => env.value }
  layers = [
    module.lambda_layer.lambda_layer_arn
  ]
}

module "lambda_layer" {
  source       = "terraform-aws-modules/lambda/aws"
  version      = "v2.34.0"
  create_layer = true

  layer_name               = "${local.project}-layer"
  compatible_runtimes      = ["python3.8", "python3.9"]
  compatible_architectures = ["x86_64"]
  create_package           = false
  ignore_source_code_hash  = true
  local_existing_package   = data.archive_file.dummy.output_path
}

Expected behavior

Lambda layer version in function is ignored.

Actual behavior

Lambda layer in function is changed back to the version in the state.

Terminal Output Screenshot(s)

In this case, I need my lambda to use the version 3 of the layer instead of the 4. And as you can see image

Additional context

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

antonbabenko commented 2 years ago

Hi @lays147 !

You can use data-source aws_lambda_layer_version to get latest available version and pass it to a function instead of:

layers = [
    module.lambda_layer.lambda_layer_arn
]

It is the only solution that comes to my mind since you are using different pipelines for layer and for function.

github-actions[bot] commented 2 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.