Pablito
Pablito

Reputation: 1

AWS SAM Local Invoke Fails with ImportModuleError Despite Successful AWS Deployment

I'm working on a serverless application using AWS SAM (Serverless Application Model) with multiple Lambda functions written in Python 3.12. The deployment to AWS works flawlessly, but when I try to invoke the UploadAudioFunction locally using sam local invoke, I encounter an ImportModuleError. Here's a detailed breakdown of my setup and the issue I'm facing.

Template Configuration (template.yaml):

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  # Create Track Function
  CreateTrackFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: tracks.create_track.lambda_handler
      Runtime: python3.12
      CodeUri: ./tracks
      Policies:
        - DynamoDBCrudPolicy:
            TableName: Tracks

  # Update Track Function
  UpdateTrackFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: tracks.update_track.lambda_handler
      Runtime: python3.12
      CodeUri: ./tracks
      Policies:
        - DynamoDBCrudPolicy:
            TableName: Tracks

  # Delete Track Function
  DeleteTrackFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: tracks.delete_track.lambda_handler
      Runtime: python3.12
      CodeUri: ./tracks
      Policies:
        - DynamoDBCrudPolicy:
            TableName: Tracks

  # List Tracks Function
  ListTracksFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: tracks.list_tracks.lambda_handler
      Runtime: python3.12
      CodeUri: ./tracks
      Policies:
        - DynamoDBReadPolicy:
            TableName: Tracks

  # Upload Audio Function
  UploadAudioFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: upload_audio.lambda_handler
      Runtime: python3.12
      CodeUri: ./audio
      Policies:
        - S3WritePolicy:
            BucketName: wave-loft-audio-bucket

Issue Description: When I attempt to invoke the UploadAudioFunction locally using the following command:

sam local invoke UploadAudioFunction --event events/event.json --debug

I receive the following error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'upload_audio': No module named 'upload_audio'

Project Structure

demo-app/
├── template.yaml
├── tracks/
│   ├── create_track.py
│   ├── update_track.py
│   ├── delete_track.py
│   └── list_tracks.py
├── audio/
│   └── upload_audio.py
└── events/
    └── event.json

Question: Why does the UploadAudioFunction work correctly when deployed to AWS but fails with an ImportModuleError when invoked locally using SAM? How can I resolve this discrepancy to successfully debug the function locally?

Error Log:

[ERROR] Runtime.ImportModuleError: Unable to import module 'upload_audio': No module named 'upload_audio'
Traceback (most recent call last):
14 Dec 2024 18:11:40,432 [ERROR] (rapid) Init failed InvokeID= error=Runtime exited with error: exit status 1

Aso, I'm using Rancher Desktop and can't use Docker Desktop

Upvotes: 0

Views: 54

Answers (0)

Related Questions