简体   繁体   中英

AWS SAM Lambda - Unable to import module 'main': No module named 'requests', 'msal' . pipeline using Azure CI/CD

I created a python project using AWS lambda using SAM template. Created virtual environment and installed all required modules. for example, requests, msal etc. module names mentioned in requirements.txt file and installed. I am able to build, debug and deploy using sam command from my vscode. for example. sam build, sam local invoke, sam deploy --guided command.

As a project repo uses Azure devOps. Now, I created project pipeline and deployed to AWS successfully.

part of pipeline script -(install dependencies)

  • script: | python -m pip install --upgrade pip
    pip install -r src/requirements.txt displayName: 'Install dependencies'

When I tested lambda function from AWS console or call url, It show the following error as per cloud watch log. It shows module not found "request" and "msal". for example. ERROR: Function Logs [ERROR] Runtime.ImportModuleError: Unable to import module 'main': No module named 'requests'

python 3.9

AWS SAM

Azure devOps repo

VScode

I am unable to figure out the issue. I spent time but did not find the cause. I am stuck. Please give me hints/ideas/solution.

Lambda dependencies have to be installed at the root folder. See the "Deployment package with dependencies" Section, Step 5 in this guide .

Create a deployment package with the installed library at the root.

You can do so by going to the root directory, then issue a pip install with a -t (target) flag and specifying the current directory with a dot . .

pip install -r src/requirements.txt -t.

This is the following answer. It might help someone.

- task: AWSShellScript@1
  displayName: 'Build'
  inputs:
    awsCredentials: AwsServiceConnection
    regionName: 'us-east-1'
    scriptType: 'inline'
    inlineScript: |
      sam build --debug \
      --template-file template.yaml

- task: AWSShellScript@1
  displayName: 'Package'
  inputs:
    awsCredentials: AwsServiceConnection
    regionName: us-east-1
    scriptType: 'inline'
    inlineScript: |
      sam package  --resolve-s3  --output-template-file packaged.yaml

- task: AWSShellScript@1
  displayName: 'Deploy Infrastructure'
  inputs:
      awsCredentials: AwsServiceConnection
      regionName: 'us-east-1'
      scriptType: "inline"
      inlineScript: |
        sam deploy \
        --template-file packaged.yaml \
        --no-confirm-changeset \
        --no-fail-on-empty-changeset \
        --capabilities CAPABILITY_IAM  \
        --stack-name sam-test-stack \
        --resolve-s3 \
        --s3-prefix sam-test-stack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM