Skip to content
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

Cannot import Python modules while using a SAM image. #6592

Closed
devmuch opened this issue Jan 22, 2024 · 3 comments
Closed

Cannot import Python modules while using a SAM image. #6592

devmuch opened this issue Jan 22, 2024 · 3 comments
Labels

Comments

@devmuch
Copy link

devmuch commented Jan 22, 2024

I am unable to import modules in Python.

Steps to reproduce:

  1. Launch sam init
  2. Enter the following choices:
1 - AWS Quick Start Templates

1 - Hello World Example

Use the most popular runtime and package type? (Python and zip) [y/N]: N

Which runtime would you like to use?
  16 - python3.9

What package type would you like to use?
  2 - Image

(defaults for remaining prompts)

Create a ./sam-app/modules/my_module.py with the following contents:

def create_message():
    return "Hello World"

Modify ./sam-app/app.py to contain the following:

from modules import my_module

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": my_module.hello_world()
    }

Build the function with sam build

Run sam local invoke

Results in:

{"errorMessage": "Unable to import module 'app': No module named 'modules'", "errorType": "Runtime.ImportModuleError", "requestId": "7eac3ff7-f94a-469e-9fa4-96d42b99eaea", "stackTrace": []}

Observed result:

2024-01-22 12:09:26,770 | Config file location: C:\Users\interim\Desktop\sam-app\samconfig.toml
2024-01-22 12:09:26,773 | Loading configuration values from [default.['local', 'invoke'].parameters] (env.command_name.section) in config file at   
'C:\Users\interim\Desktop\sam-app\samconfig.toml'...
2024-01-22 12:09:26,774 | Configuration values successfully loaded.
2024-01-22 12:09:26,774 | Configuration values are: {'stack_name': 'sam-app'}
2024-01-22 12:09:26,779 | Using SAM Template at C:\Users\interim\Desktop\sam-app\.aws-sam\build\template.yaml
2024-01-22 12:09:26,797 | Using config file: samconfig.toml, config environment: default
2024-01-22 12:09:26,798 | Expand command line arguments to:
2024-01-22 12:09:26,799 | --template_file=C:\Users\interim\Desktop\sam-app\.aws-sam\build\template.yaml --no_event
--layer_cache_basedir=C:\Users\interim\AppData\Roaming\AWS SAM\layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2024-01-22 12:09:26,801 | local invoke command is called
2024-01-22 12:09:26,804 | No Parameters detected in the template
2024-01-22 12:09:26,828 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is
HelloWorldFunction
2024-01-22 12:09:26,830 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id
as the resource id
2024-01-22 12:09:26,832 | 0 stacks found in the template
2024-01-22 12:09:26,833 | No Parameters detected in the template
2024-01-22 12:09:26,849 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is
HelloWorldFunction
2024-01-22 12:09:26,850 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id
as the resource id
2024-01-22 12:09:26,852 | 2 resources found in the stack
2024-01-22 12:09:26,853 | Found Serverless function with name='HelloWorldFunction' and ImageUri='helloworldfunction:python3.9-v1'
2024-01-22 12:09:26,855 | --base-dir is not presented, adjusting uri C:\Users\interim\Desktop\sam-app\hello_world relative to
C:\Users\interim\Desktop\sam-app\.aws-sam\build\template.yaml
2024-01-22 12:09:26,856 | --base-dir is not presented, adjusting uri . relative to C:\Users\interim\Desktop\sam-app\.aws-sam\build\template.yaml    
2024-01-22 12:09:26,870 | Found one Lambda function with name 'HelloWorldFunction'
2024-01-22 12:09:26,871 | Invoking Container created from helloworldfunction:python3.9-v1
2024-01-22 12:09:26,872 | No environment variables found for function 'HelloWorldFunction'
2024-01-22 12:09:26,873 | Loading AWS credentials from session with profile 'None'
2024-01-22 12:09:26,882 | Code None is not a zip/jar file
Building image.................
2024-01-22 12:09:27,273 | Checking free port on 127.0.0.1:7543
2024-01-22 12:09:27,279 | Using local image: helloworldfunction:rapid-x86_64.

2024-01-22 12:09:28,107 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
START RequestId: 3a445e67-259f-41b0-a6a8-e2d3f9b8d579 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'modules'
Traceback (most recent call last):
END RequestId: ff9d8eed-5ce7-44af-ac2b-27bbeb3fa574
REPORT RequestId: ff9d8eed-5ce7-44af-ac2b-27bbeb3fa574  Init Duration: 0.46 ms  Duration: 63.04 ms      Billed Duration: 64 ms  Memory Size: 128 MBMax Memory Used: 128 MB
{"errorMessage": "Unable to import module 'app': No module named 'modules'", "errorType": "Runtime.ImportModuleError", "requestId": "ff9d8eed-5ce7-44af-ac2b-27bbeb3fa574", "stackTrace": []}
2024-01-22 12:09:29,190 | [Container state] OOMKilled False
2024-01-22 12:09:29,425 | Cleaning all decompressed code dirs
2024-01-22 12:09:29,427 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-01-22 12:09:29,429 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-01-22 12:09:29,429 | Unable to find Click Context for getting session_id.
2024-01-22 12:09:29,431 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics

Expected result:

The function will run.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

# Paste the output of `sam --info` here
{
  "version": "1.107.0",
  "system": {
    "python": "3.11.7",
    "os": "Windows-10-10.0.22631-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.7",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}
@devmuch devmuch added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jan 22, 2024
@devmuch devmuch changed the title Bug: TITLE Bug: Cannot import Python modules while using a SAM image. Jan 22, 2024
@hnnasit
Copy link
Contributor

hnnasit commented Jan 22, 2024

Hi @devmuch, thanks for opening the issue. The modules folder needs to be copied to the image as well. Here is a simple project that I have created based on the details you provided:
template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  python3.9

  Sample SAM Template for sam-app-6592

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      Dockerfile: ./hello_world/Dockerfile
      DockerContext: .
      DockerTag: python3.9-v1

app.py

from modules.my_module import create_message

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": create_message()
    }

Dockerfile

FROM public.ecr.aws/lambda/python:3.9

COPY ./hello_world/requirements.txt ./
RUN python3.9 -m pip install -r requirements.txt -t .

COPY ./hello_world/app.py ./
ADD ./modules ./modules

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

directory structure

.
├── README.md
├── __init__.py
├── events
│   └── event.json
├── hello_world
│   ├── Dockerfile
│   ├── __init__.py
│   ├── app.py
│   └── requirements.txt
├── modules
│   └── my_module.py
├── samconfig.toml
├── template.yaml

Let us know if you still have questions.

@hnnasit hnnasit added type/question area/build sam build command and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jan 22, 2024
@devmuch devmuch changed the title Bug: Cannot import Python modules while using a SAM image. Cannot import Python modules while using a SAM image. Jan 23, 2024
@devmuch
Copy link
Author

devmuch commented Jan 24, 2024

Thanks for this. I naively assumed it copy the folder contents, but RTFM proves me wrong. :-)

@devmuch devmuch closed this as completed Jan 24, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants