AWS Lambda | Azure Python Library Intergration

1.1k views Asked by At

I am trying to make Azure API calls using AWS Lambda using Python. So I decided to create a Lambda Layer for Azure Compute Management Library. I downloaded the azure-mgmt-compute 17.0.0 from this link. Then added the zip to Lambda Layers, When I am trying to import the azure library, I am facing the following error :

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'azure'",
  "errorType": "Runtime.ImportModuleError"
}

Then I decided to create a zip package using a virtual environment using the following commands:

virtualenv v-env;
source v-env/bin/activate;
pip install azure-mgmt-compute;
deactivate;
cd v-env/lib/python3.8/site-packages;
zip -r9 ${OLDPWD}/function.zip .;

Still, no luck, does anybody has implemented something like this before?

1

There are 1 answers

0
Vinayak Shinde On

You can use serverless to accomplish this. Create requirements.txt and add all dependent package list. In your case "azure-mgmt-compute". In serverless.yml under custom section add below and refer lambda layer in function. And run sls deploy --stage dev. This will create lambda layer and will add layer in lambda. You can directly import dependent library in lambda.

functions:
  azure_container_instance:
    handler: azure_container_instance/handler.lambda_handler
    layers:
      - Ref: PythonRequirementsLambdaLayer
    timeout: 300

custom:
  pythonRequirements:
    dockerizePip: non-linux
    slim: true
    strip: false
    fileName: ./requirements.txt
    layer:
      name: ${self:provider.stage}-layerName
      description: Python requirements lambda layer
      compatibleRuntimes:
        - python3.8
      licenseInfo: GPLv3
      allowedAccounts:
        - '*'