Home > OS >  Serverless framework - Use multiple runtimes (TS / Python)
Serverless framework - Use multiple runtimes (TS / Python)

Time:10-10

I've spent quite a lot of time trying to find a workaround for this without success. I have a big project which is working fine, using the serverless framework. We would like to add one lambda, using python (because we want to embed the AWS CLI in this one, and I believe that's the only way to do so).

However, my .py file is never embedded in the packaged zip file.

Please find a part of my serverless.yml below:

service: salameche

custom:
  stages:
    - staging
    - production
  serverless-offline:
    httpPort: 3333

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, 'staging'}
  region: eu-west-1
  vpc: ${file(vpc_env.yml):${self:provider.stage}}

...

  function1:
    handler: ./pdf/handler.handler
    timeout: 60
  function2:
    handler: ./database/dump.handler
    timeout: 60
  syncS3ContractModels:
    handler: ./database/syncbucket.hello
    runtime: python3.9

plugins:
  - serverless-stage-manager
  - serverless-plugin-typescript
  - serverless-dotenv-plugin
  - serverless-offline
  - serverless-step-functions
  - serverless-python-requirements

I purposefully simplified it, but that's the main gesture. Below the syncbucket.py file (all of it):

def hello(event, context):
    print('This is working!!')
    return 0

And below my dependencies, extracted from my package.json:

"dependencies": {
    "axios": "^0.19.2",
    "canvas": "^2.10.1",
    "lambda-multipart-parser": "^1.0.1",
    "mysql": "^2.18.1",
    "pdfjs-dist": "2.5.207",
    "tslib": "^1.10.0",
    "typeorm": "^0.2.21",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.71",
    "@types/aws-sdk": "^2.7.0",
    "@types/node": "^14.14.22",
    "@types/pdfjs-dist": "2.1.5",
    "@types/uuid": "^8.3.0",
    "nodemon": "^2.0.2",
    "prettier": "^2.6.1",
    "serverless-dotenv-plugin": "^3.1.0",
    "serverless-offline": "^6.0.0-alpha.64",
    "serverless-plugin-aws-alerts": "^1.4.0",
    "serverless-plugin-typescript": "^2.1.4",
    "serverless-python-requirements": "^5.4.0",
    "serverless-stage-manager": "^1.0.5",
    "serverless-step-functions": "^3.10.0",
    "typescript": "^3.7.5"
  },

My lambda is created with correct runtime python 3.9, but when I test it I face the following error (from cloudwatch):

[ERROR] TypeError: the 'package' argument is required to perform a relative import for '..database.syncbucket'
Traceback (most recent call last):
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 122, in import_module
    raise TypeError(msg.format(name))[ERROR] TypeError: the 'package' argument is required to perform a relative import for '..database.syncbucket'
Traceback (most recent call last):
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 122, in import_module
    raise TypeError(msg.format(name))START RequestId: 9926fa36-468c-488f-8209-07544d0e4bbb Version: $LATEST
END RequestId: 9926fa36-468c-488f-8209-07544d0e4bbb
REPORT RequestId: 9926fa36-468c-488f-8209-07544d0e4bbb  Duration: 297.05 ms Billed Duration: 298 ms Memory Size: 512 MB Max Memory Used: 10 MB  
Unknown application error occurred

Within my local .serverless folder, created after the sls deployment, I try to unzip the package, and the python file is not there! There are my other functions as expected, but not the syncbucket.py

Would you have any idea why, and what am I missing?

Thank you!

CodePudding user response:

Set the include option in the package section of your sls configuration.

Also update the handler value to one that is compatible for the importlib.importmodule.

package:
  include:
    - ./database/*

  function1:
    handler: ./pdf/handler.handler
    timeout: 60
  function2:
    handler: ./database/dump.handler
    timeout: 60
  syncS3ContractModels:
    handler: database.syncbucket.hello
    runtime: python3.9
#...

When you run the following command to package your functions.

sls package

You should see the python sources included in the archive.

zipinfo .serverless/salameche.zip
  • Related