Skip to content

fix: Update package.py #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

ArtPiece55
Copy link

install_pip_requirements: fixing --target section of pip install, following aws documentation (https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html)

Description

Minor fix to pip requirements install path

Motivation and Context

Following AWS documentation regarding Working with layers for Python Lambda functions (https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html), the --target of pip install must have Choose one of the following methods to install pip packages into the required top-level directory (python/):
Without the implemented fix, a test of the lambda function returns an error (Examples below).

Example module usage:

module "lambda_requests_layer" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "7.21.0"

  create_layer = true

  layer_name          = "requests"
  runtime         = "python3.9"
  build_in_docker = true
  source_path = [{
    pip_tmp_dir      = "${path.module}/src/lambda/layer_tmp"
    pip_requirements = "${path.module}/src/lambda/requirements.txt"
  }]
  artifacts_dir = "${path.root}/.terraform/builds/${var.project_name}-requests"
}

module "lambda_fallback" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "7.21.0"

  function_name           = "${var.project_name}-fallbacklambda"
  handler                 = "lambda_function_test.lambda_handler"
  runtime                 = "python3.9"
  source_path             = "${path.module}/src/lambda/lambda_function_test.py"
  layers = [module.lambda_requests_layer.lambda_layer_arn]
  publish                 = true
  ignore_source_code_hash = true
  attach_policies         = true
  number_of_policies      = 1
  policies = [
    "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  ]
  artifacts_dir           = "${path.root}/.terraform/builds/${var.project_name}-fallbacklambda"
}

Example requirements:

requests==2.32.3

Example python code:

import requests

def lambda_handler(event, context):
   response = requests.get("https://httpbin.org/get", timeout=10)
   print(response.json())

Lambda function execution error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function_test': No module named 'requests'
Traceback (most recent call last):

Breaking Changes

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

The master has been pulled, implemeted the proposed change, used new source module source = "../terraform-aws-lambda" on the previously described module usage, returned output:

Status: Succeeded
Test Event Name: TEST_EVENT

Response:
null

Function Logs:
START RequestId: 224fb8c9-8590-41e8-abff-8b995e23115d Version: $LATEST
{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.3', 'X-Amzn-Trace-Id': 'Root=1-6841ccba-632cc83457388d0c4fc43ba5'}, 'origin': '98.83.38.2', 'url': 'https://httpbin.org/get'}
END RequestId: 224fb8c9-8590-41e8-abff-8b995e23115d
REPORT RequestId: 224fb8c9-8590-41e8-abff-8b995e23115d	Duration: 8978.87 ms	Billed Duration: 8979 ms	Memory Size: 128 MB	Max Memory Used: 53 MB	Init Duration: 355.96 ms

Request ID: 224fb8c9-8590-41e8-abff-8b995e23115d

install_pip_requirements: fixing `--target` section of pip install, following aws documentation (https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html)
@ArtPiece55 ArtPiece55 changed the title Update package.py fix: Update package.py Jun 5, 2025
@ArtPiece55 ArtPiece55 closed this Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant