使用AWS SAM进行本地AWS Lambda测试

时间:2017-11-25 21:13:13

标签: amazon-web-services docker aws-lambda docker-machine aws-sam-cli

我希望使用AWS' SAM在将脚本推送到AWS Lambda之前进行本地开发,测试和打包脚本。我使用Docker Toolbox(17.10.0-ce)和AWS SAM(0.2.2)运行与实际AWS Lambda本身相同的代码 - 我知道它有效。我的MyMainFunction.py如下:

import json
def l_handle(event, context):
    payload = json.loads(event.get('body'))
    op1 = payload['op1']
    op2 = payload['op2']
    operator = payload['op']
    if op1 == 0 or op2 == 0 or operator == None:
        result = 'Please enter valid input data'
        ret_code = 219
    else:
        if operator in {'add', 'plus', '+'}:
            result = op1 + op2
            ret_code = 278
        elif operator in {'subtract', 'minus', '-'}:
            result = op1 - op2
            ret_code = 278
        elif operator in {'multiply', '*'}:
            result = op1 * op2
            ret_code = 278
        elif operator in {'divide', '/'}:
            result = op1 / op2
            ret_code = 278
        else:
            result = 'Please enter valid operator'
            ret_code = 219
    print(result)
    return {
        'isBase64Encoded': False,
        'statusCode': ret_code,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(result)
    }

event.json如下:

{
    "resource": "/{proxy+}",
    "path": "/",
    "httpMethod": "POST",
    "headers": null,
    ...
      },
      "resourcePath": "/{proxy+}",
      "httpMethod": "POST",
      "apiId": "*"
    },
    "body": "{\"op1\":2,\"op2\":7,\"op\":\"divide\"}",
    "isBase64Encoded": false
  }

template.yaml如下:

AWSTemplateFormatVersion : '2010-09-09'
  Transform: AWS::Serverless-2016-10-31
  Description: A simple Calc application.
  Resources:
    MyMainFunction:
      Type: AWS::Serverless::Function
      Properties:
        Handler: l_handle
        Runtime: python3.6

当我尝试在Docker中以$ sam local invoke MyMainFunction --event event.json运行命令时,出现错误,如下所示:

muham@DESKTOP-BS30EMD MINGW64 ~/Documents/Codes/lambdaLocal
$ sam local invoke MyMainFunction --event event.json
2017/11/25 20:52:57 Successfully parsed template.yaml
[34mINFO[0m[0000] Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows
2017/11/25 20:52:57 Connected to Docker 1.34
[34mINFO[0m[0000] Unable to use system certificate pool: crypto/x509: system root pool is not available on Windows
2017/11/25 20:52:57 Fetching lambci/lambda:python3.6 image for python3.6 runtime...
python3.6: Pulling from lambci/lambda
Digest: sha256:3663b89bd1f4c4d1a4f06a77fc543422c1f0cbfc3a2f491c8c7bdc98cf9cf0b6
Status: Image is up to date for lambci/lambda:python3.6
2017/11/25 20:52:59 Invoking l_handle (python3.6)
2017/11/25 20:53:00 Could not invoke function: Error response from daemon: invalid volume specification: 'C:/Users/muham/Documents/Codes/lambdaLocal:/var/task:ro'

这显然与卷有关。然而,不幸的是,我大约五个小时前才开始使用Dockers(并且之后一直在研究卷和安装)所以我不确定为什么Docker在正常目录的末尾附加:/var/task:ro或者如何解决它

任何想法?

0 个答案:

没有答案