我希望在将脚本推送到 AWS Lambda 之前使用 AWS 的 SAM 在本地开发、测试和打包脚本。我正在使用 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 as 中运行命令时$ 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
在普通目录的末尾或如何解决它。
有任何想法吗?