Search code examples
python-3.xamazon-web-servicesazure-devopssam

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.


Solution

  • 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