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
886 stars 656 forks source link

Module creates unusable python pg8000 #473

Closed t-cliff closed 1 year ago

t-cliff commented 1 year ago

Description

Please provide a clear and concise description of the issue you are encountering, and a reproduction of your configuration (see the examples/* directory for references that you can copy+paste and tailor to match your configs if you are unable to copy your exact configuration). The reproduction MUST be executable by running terraform init && terraform apply without any further changes.

If your request is for a new feature, please use the Feature request template.

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

Reproduction Code [Required]

Steps to reproduce the behavior: Terraform Code

module "pg8000_lambda_layer" {
  source = "terraform-aws-modules/lambda/aws"
  version = ">=4.10.2"

  create_layer = true

  layer_name          = "trav-pg8000"
  description         = "Layer for proxy"
  runtime             = "python3"
  compatible_runtimes = ["python3.8", "python3.9","python3.10"]

  source_path = {path = "./Modules/python.zip"}
}
resource "aws_lambda_function" "lambda" {
  function_name    = "hello_lambda"
  layers            = [
    module.pg8000_lambda_layer.lambda_layer_arn,
  ]
  filename         = "${data.archive_file.zip.output_path}"
  source_code_hash = "${data.archive_file.zip.output_base64sha256}"

  role    = "${aws_iam_role.iam_for_lambda.arn}"
  handler = "hello_lambda.lambda_handler"
  runtime = "python3.9"

  environment {
    variables  = {
      greeting = "Hello"
    }
  }
}

Lambda Code

import os

import pg8000

def lambda_handler(event, context):
    return "{} from Lambda!".format(os.environ['greeting'])

Expected behavior

Lambda ran successfully!

Actual behavior

{
  "errorMessage": "Unable to import module 'hello_lambda': No module named 'pg8000'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "150d96c8-63e3-480c-970f-b7b5554d021e",
  "stackTrace": []
}

When I manually and use resource to create the layer the import of pg8000 works as expected, only doesnt work when I use module.

Terminal Output Screenshot(s)

image

Additional context

antonbabenko commented 1 year ago
module "pg8000_lambda_layer" {
  # omitted...
  source_path = {path = "./Modules/python.zip"}
}

source_path is in the incorrect format.

Please look into examples/complete for more examples - https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/master/examples/complete/main.tf

github-actions[bot] commented 11 months 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.