无服务器python无法导入模块处理程序,并且可能无法通过sls deploy正确进行dockerizing

时间:2019-01-23 02:34:21

标签: python amazon-web-services aws-lambda serverless

我得到的错误:

{
    "errorMessage": "Unable to import module 'handler'"
}

  Error --------------------------------------------------

  Invoked function failed

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           10.6.0
     Serverless Version:     1.28.0

按照https://serverless.com/blog/serverless-python-packaging/上的步骤进行操作不会导入必要的包装。

在Mac上为python3

激活的虚拟环境。安装了requirements.txt文件。 sls部署。似乎没有任何作用。每次尝试调用该函数时,都会显示无法导入模块处理程序。

我也尝试通过遵循https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-aws-integrations.html#es-aws-integrations-s3-lambda-es-deployment-package的方法在aws控制台中创建类似的内容,但是没有用。它产生相同的错误。搜寻了几天。任何帮助表示赞赏。

运行macOS High Sierra,Python 3.7.2;



provider:
  name: aws
  runtime: python3.6

custom:
  pythonRequirements:
    dockerizePip: non-linux
    slim: true
    usePipenv: false
    zip: true

package:
  exclude:
  - venv/**
  - .idea/**
  - .git/**

  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "es:ESHttpGet"
      Resource: "arn:aws:es:***"




functions:
  providerSearch:
    handler: handler.providerSearch

    events:
      - http:
          path: providers/search
          method: get
          cors: true
plugins:
  - serverless-python-requirements
  - serverless-plugin-include-dependencies
resources:
  Resources:
    GatewayResponseDefault4XX:
      Type: 'AWS::ApiGateway::GatewayResponse'
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseType: DEFAULT_4XX
        RestApiId:
          Ref: 'ApiGatewayRestApi'

如果您查看上传的文件大小太小

Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /Users/****/providerSearch2/requirements.txt in /Users/****/providerSearch2/.serverless/requirements.txt...
Serverless: Installing requirements from /Users/****/providerSearch2/.serverless/requirements/requirements.txt ...
Serverless: Docker Image: lambci/lambda:build-python3.6
Serverless: Zipping required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (525.17 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...

这是handler.py代码

import boto3
import json
import requests
from requests_aws4auth import AWS4Auth

region = 'us-east-1'  # For example, us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(
    credentials.access_key, credentials.secret_key, region,
    service, session_token=credentials.token
)

host = 'https://***s.amazonaws.com'
# For example, search-mydomain-id.us-west-1.es.amazonaws.com
index = 'providerprofile'
url = 'https://' + host + '/' + index + '/_search'


# Lambda execution starts here
def providerSearch(event, context):

    # Put the user query into the query DSL for more accurate search results.
    # Note that certain fields are boosted (^).
    query = {
        "size": 10,
        "query": {
            "multi_match": {
                "query": event['queryStringParameters']['q'],
                "fields": [
                    "Insurance^4",
                    "LicensedStates^2",
                    "ProviderName",
                    "ClientConcerns",
                    "CostOfService",
                    "Description",
                    "Specialty^2",
                    "TypesOfTherapy",
                    "Education",
                ]
            }
        }
    }

    # ES 6.x requires an explicit Content-Type header
    headers = {"Content-Type": "application/json"}

    # Make the signed HTTP request
    r = requests.get(
        url, auth=awsauth, headers=headers, data=json.dumps(query))

    # Create the response and add some extra content to support CORS
    response = {
        "statusCode": 200,
        "headers": {
            "Access-Control-Allow-Origin": '*'
        },
        "isBase64Encoded": False
    }

    # Add the search results to the response
    response['body'] = r.text
    return response

预期结果应该是正确地对容器进行docker化,然后上载到aws lambda以用作搜索触发器。

0 个答案:

没有答案