I am migrating a Ruby 2.7 Lambda from zip file deployment to container image deployment.
When I deploy my container to AWS Lambda, the container behaves as I hope that it would.
When I attempt to test the same image locally using docker run {my-image-name}, I am encountering 2 issues
- The parameters are not accessible from the event object in the same manner
- The return headers and status code are not honored in the same way as they are handled from Lambda
My Questions
- Do I need to bundle something else into my dockerfile to assist with Lambda simulation?
- Do I need to use a different entrypoint in my dockerfile?
- I found documentation for the "AWS Lambda Runtime Interface Emulator", but I am unclear if it is designed to assist with the problems I am encountering.
Here is a simplified version of what I am attempting to test.
Dockerfile
FROM public.ecr.aws/lambda/ruby:2.7
COPY * ./
RUN bundle install
CMD [ "lambda_function.LambdaFunctions::Handler.process" ]
Gemfile
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
Running with AWS Lambda
We have a load balancer making this Lambda available
curl -v https://{my-load-balancer-url}/owners?path=owners
Response - note that the load balancer has converted my GET request to a POST request
{
"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
}
}
Running in docker
docker run --rm --name lsample -p 9000:8080 -d {my-image-name}
POST request to local docker
curl -v http://localhost:9000/2015-03-31/functions/function/invocations -d '{"path": "owners"}'
Note that the headers are returned as part of the respose
Response Headers
< HTTP/1.1 200 OK
< Date: Fri, 18 Dec 2020 19:06:56 GMT
< Content-Length: 166
< Content-Type: text/plain; charset=utf-8
Response Body (formatted)
{
"headers": {
"Access-Control-Allow-Origin": "*"
},
"statusCode": 200,
"body": "{\"message\":\"This is a placeholder for your lambda code\",\"event\":{\"path\":\"owners\"}}"
}
GET request to local docker
curl -v http://localhost:9000/2015-03-31/functions/function/invocations?path=owners
No content is returned
< HTTP/1.1 200 OK
< Date: Fri, 18 Dec 2020 19:10:08 GMT
< Content-Length: 0
eventparameter of the function.