【发布时间】:2021-03-29 10:52:12
【问题描述】:
我正在将 Ruby 2.7 Lambda 从 zip 文件部署迁移到容器映像部署。
当我将容器部署到 AWS Lambda 时,容器的行为与我希望的一样。
当我尝试使用 docker run {my-image-name} 在本地测试相同的图像时,我遇到了 2 个问题
- 不能以相同的方式从事件对象访问参数
- 返回标头和状态代码的处理方式与从 Lambda 处理的方式不同
我的问题
- 我是否需要将其他内容捆绑到我的 dockerfile 中以协助 Lambda 模拟?
- 我需要在我的 dockerfile 中使用不同的入口点吗?
- 我找到了“AWS Lambda 运行时接口模拟器”的文档,但我不清楚它是否旨在帮助解决我遇到的问题。
这是我正在尝试测试的简化版本。
Dockerfile
FROM public.ecr.aws/lambda/ruby:2.7
COPY * ./
RUN bundle install
CMD [ "lambda_function.LambdaFunctions::Handler.process" ]
宝石文件
source "https://rubygems.org"
lambda_function.rb
require 'json'
module LambdaFunctions
class Handler
def self.process(event:,context:)
begin
json = {
message: 'This is a placeholder for your lambda code',
event: event
}.to_json
{
headers: {
'Access-Control-Allow-Origin': '*'
},
statusCode: 200,
body: json
}
rescue => e
{
headers: {
'Access-Control-Allow-Origin': '*'
},
statusCode: 500,
body: { error: e.message }.to_json
}
end
end
end
end
使用 AWS Lambda 运行
我们有一个负载均衡器让这个 Lambda 可用
curl -v https://{my-load-balancer-url}/owners?path=owners
响应 - 请注意负载平衡器已将我的 GET 请求转换为 POST 请求
{
"message": "This is a placeholder for your lambda code",
"event": {
"requestContext": {
"elb": {...}
},
"httpMethod": "POST",
"path": "/owners",
"queryStringParameters": {
"path": "owners"
},
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"cache-control": "no-cache",
"connection": "keep-alive",
"content-length": "0",
...
},
"body": "",
"isBase64Encoded": false
}
}
在 docker 中运行
docker run --rm --name lsample -p 9000:8080 -d {my-image-name}
对本地 docker 的 POST 请求
curl -v http://localhost:9000/2015-03-31/functions/function/invocations -d '{"path": "owners"}'
请注意,标头是作为响应的一部分返回的
响应标头
< HTTP/1.1 200 OK
< Date: Fri, 18 Dec 2020 19:06:56 GMT
< Content-Length: 166
< Content-Type: text/plain; charset=utf-8
响应正文(格式化)
{
"headers": {
"Access-Control-Allow-Origin": "*"
},
"statusCode": 200,
"body": "{\"message\":\"This is a placeholder for your lambda code\",\"event\":{\"path\":\"owners\"}}"
}
GET 请求到本地 docker
curl -v http://localhost:9000/2015-03-31/functions/function/invocations?path=owners
没有内容返回
< HTTP/1.1 200 OK
< Date: Fri, 18 Dec 2020 19:10:08 GMT
< Content-Length: 0
【问题讨论】:
-
您似乎正在比较通过 AWS 上的 API 网关/负载均衡器调用的 Lambda 函数与本地运行时接口模拟器。后者,虽然暴露本地服务器没有前者的任何特性,但它所做的只是将 POST 请求的正文内容作为函数的
event参数传递。
标签: amazon-web-services aws-lambda