I'm having a huge headache figuring this out, I just want to emulate a local AWS Lambda service to easily test integration without writing custom clients and such
I have this monorepo:
| lambda-service-emulator/
---| app.py
---| Dockerfile
| lambda-function/
---| Dockerfile
| docker-compose.yml
lambda-service-emulator/app.py its a very simple FastAPI, with a single endpoint to simulate Lambda Service API:
from fastapi import FastAPI, Body, Header
import os
import requests
app = FastAPI()
@app.post("/2015-03-31/functions/{function_name}/invocations")
def invoke_lambda(
function_name: str,
payload: dict = Body({})
):
f_endpoint = os.environ['FUNCTION_ENDPOINT']
r = requests.post(f'{f_endpoint}/2015-03-31/functions/function/invocations', json=payload)
try:
data = r.json()
except Exception:
return {
'statusCode': 400
}
return data
lambda-function/Dockerfile its an image that is compliant with AWS Lambda
FROM public.ecr.aws/lambda/provided as runtime
# installing stuff
CMD ["handler.handler"]
And the docker-compose:
version: '3.8'
services:
lambda-service:
build: ./lambda-service-emulator
ports:
- 8081:8080
environment:
FUNCTION_ENDPOINT: http://lambda-function:8080
lambda-function:
volumes:
- ./lambda-function:/var/task
build: ./lambda-function
ports:
- 8082:8080
Here's the problem, when I try to invoke the lambda through compose, the code goes through normally, but before it can return the response, it gives this error:
[ERROR] (rapid) Failed to reserve: AlreadyReserved
I think its because this image is not meant to keep running, it should teardown after every invocation, but I'm not sure how to solve this while maintaining the docker compose strutcture?
It works if I run the container as a one off, as per this docs: https://docs.aws.amazon.com/lambda/latest/dg/images-test.html