0

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

Mojimi
  • 2,561
  • 9
  • 52
  • 116
  • Lambda compatible docker images are not designed to be torn down after every invocation. Even in AWS the image will be kept running and reused for many invocations if you have active requests coming in. So that is not the issue. – Mark B Mar 28 '23 at 20:06
  • Possible duplicate: https://stackoverflow.com/questions/71699358/aws-lambda-error-rapid-failed-to-reserve-alreadyreserved – Mark B Mar 28 '23 at 20:06
  • Hmm, maybe its a memory limit issue then? I just noticed it printed `REPORT RequestId: 04d5802e-7931-4f21-a962-470d9d235312 Init Duration: 0.38 ms Duration: 72223.98 ms Billed Duration: 72224 ms Memory Size: 3008 MB Max Memory Used: 3008 MB` at the end – Mojimi Mar 29 '23 at 15:15

0 Answers0