Serverless wsgi unable to import werkzeug
Asked Answered
F

2

6

I'm having issues deploying my serverless application to AWS. In AWS the logs show:

Unable to import module 'wsgi_handler': No module named 'werkzeug'

I have explicitly specified werkzeug in my requirements.txt but it seems that when I run sls deploy the packages specified are not being put inside the zip file that is uploaded to my S3 bucket.

Below is a copy of my serverless.yml file:

service: serverless-flask
plugins:
  - serverless-python-requirements
  - serverless-wsgi
  - serverless-dynamodb-local
custom:
  tableName: 'transactions-table-${self:provider.stage}'
  wsgi:
    app: app.app # entrypoint is app.app, which means the app object in the app.py module.
    packRequirements: false
  pythonRequirements:
    dockerizePip: true
  dynamodb:
    stages:
      - test
      - dev
    start:
      migrate: true

provider:
  name: aws
  runtime: python3.6
  stage: dev
  region: us-east-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
      Resource:
        - { "Fn::GetAtt": ["TransactionsDynamoDBTable", "Arn" ] }
  environment:
    TRANSACTIONS_TABLE: ${self:custom.tableName}

functions:
  app:
    handler: wsgi_handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
resources:
  Resources:
    TransactionsDynamoDBTable:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        AttributeDefinitions:
          -
            AttributeName: transactionId
            AttributeType: S
          -
            AttributeName: timestamp
            AttributeType: S
        KeySchema:
          -
            AttributeName: transactionId
            KeyType: HASH
          -
            AttributeName: timestamp
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.tableName}

Here is my requirements.tx:

boto3==1.11.17
botocore==1.14.17
Click==7.0
docutils==0.15.2
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.1
jmespath==0.9.4
MarkupSafe==1.1.1
python-dateutil==2.8.1
s3transfer==0.3.3
six==1.14.0
urllib3==1.25.8
Werkzeug==1.0.0

As far as I know, using the serverless-wsgi plugin should handle the installation of this package automatically but I'm seeing no .requirements folder being created in the .serverless folder or in the zipfile that serveless creates.

The requirements.txt file contained inside the zip faile only contains the following:

-i https://pypi.org/simple

I'm not sure what I'm doing wrong but the only solution so far has been to tear down the infrastructure and redeploy with a new url which isn't ideal.

Fatling answered 20/2, 2020 at 13:3 Comment(0)
A
1

Adding a reference to the lambda layer did the trick for me (see the layers section):

  api:
    timeout: 30
    handler: wsgi_handler.handler
    layers:
      - {Ref: PythonRequirementsLambdaLayer}
    events:
      - http: ANY /

enter image description here

Afton answered 16/12, 2021 at 21:6 Comment(0)
G
0

You need add your files manually to the package. In your serverless.yml, add this

package:
  individually: true
  exclude:
    - ./**
  include:
    - requirements.txt
    - <other files>

Once you deploy, you can download the packaged zip from AWS and verify if your files are there.

Glialentn answered 25/8, 2021 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.