Skip to content

ModuleNotFoundError in Lambda after Redeployment without Configuration Changes

0

Description

I am experiencing a ModuleNotFoundError when executing an AWS Lambda function. The error implies that the Lambda runtime cannot locate or recognize the handler module as a Python package. Notably, this issue appeared after redeploying the function without any modifications to the previously working path or configuration.

Error Details

  • Timestamp: 2024-05-13T12:24:28.530Z
  • File: /opt/.../exec_wrapper.py
  • Error Message:
    [ERROR] Runtime.ImportModuleError: Unable to import module 'app/agents/mainAgent/lambda_function.handler': No module named 'app.agents'; 'app' is not a package
    Traceback (most recent call last):
    ...
    ModuleNotFoundError: No module named 'app.agents'; 'app' is not a package
    

Serverless.yml

mainAgent:
    description: Handle various type of text requests
    role: platformMainAgentRole
    handler: app/agents/mainAgent/lambda_function.handler
    architecture: arm64
    timeout: 60
    runtime: python3.9
  events:
      - schedule:
          rate: ${self:custom.rates.${self:provider.stage}}
          enabled: true
          inputTransformer:
            inputTemplate: '{}'
    layers:
      - { Ref: OpenaiLambdaLayer }
      - { Ref: TiktokenLambdaLayer }

Expected Behavior

The function should be able to find and import the handler module as it did before the redeployment, without any issues.

Actual Behavior

The function fails to start, throwing an error that it cannot find the specified module. This is puzzling as there were no changes in the Lambda's configuration or handler path that had previously been functional.

Steps to Reproduce

  1. Setup the Lambda function with the handler path specified as app/agents/mainAgent/lambda_function.handler.
  2. Deploy the function using the typical deployment process.
  3. Attempt to invoke the function, triggering the error.

Additional Information

The sudden appearance of this issue in a stable setup raises concerns about potential changes or inconsistencies in the Lambda environment or deployment processes. Any insights or similar experiences shared by the community could help in pinpointing and resolving this issue.

asked 2 years ago296 views
1 Answer
0

Hi,

I'll share my valid Lambda definition from the SAM YAML file.

  • Target code: ./src/api/create_order.py
src
├── api
│   ├── __init__.py
│   ├── create_order.py
└── layers
   └── utils.py
  ### Integrates the 🟠Lambda function
  AddOrderFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/api
      Handler: create_order.lambda_handler
      Runtime: python3.9
      Tracing: Active
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref OrdersTable
      Environment:
        Variables:
          TABLE_NAME: !Ref OrdersTable
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /orders
            Method: post
            RestApiId: !Ref WorkshopApiGateway
Key point of properties
CodeUriThe local path to the function
e.g., src/api (the path from current directory)
HandlerThe function within your code that is called to begin execution.
e.g., FILE_NAME(without .py).lambda_handler (Not lambda_function.handler)

Note: AWS SAM template anatomy

I hope this helps!

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.