Robert Griffith
Robert Griffith

Reputation: 61

Serverless wsgi unable to import werkzeug

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.

Upvotes: 6

Views: 2410

Answers (2)

ivansabik
ivansabik

Reputation: 613

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

Upvotes: 1

Panic
Panic

Reputation: 91

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.

Upvotes: 0

Related Questions