lambci / docker-lambda

Docker images and test runners that replicate the live AWS Lambda environment
MIT License
5.83k stars 431 forks source link

Is it possible to have multiple lambda functions running at once & invoke them based on the path param? #234

Closed marlonbernardes closed 4 years ago

marlonbernardes commented 4 years ago

I have the following directory structure:

.
├── docker-compose.yml
└── lambda
   └── foo.js

lambda/foo.js

exports.handler = async (event) => {
  return { statusCode: 200, body: 'Foo executed' }
}

docker-compose.yml

 lambdanode:
    image: lambci/lambda:nodejs12.x
    container_name: lambdanode
    environment:
      AWS_DEFAULT_REGION: eu-west-1
      AWS_ACCOUNT_ID: 123456789012
      DOCKER_LAMBDA_WATCH: 1
      DOCKER_LAMBDA_STAY_OPEN: 1
    ports:
      - 9001:9001
    volumes:
      - $PWD/lambda:/var/task
    # References the foo handler defined above:    
    command: foo.handler
$ docker-compose up 
# 
$ curl -d '{}' http://localhost:9001/2015-03-31/functions/foo/invocations
{"statusCode":200,"body":"Foo executed"}

Perfect, everything working so far. One thing to note is that when performing the curl, I can change the foo parameter to anything else and I'll still get the same output:

$ curl -d '{}' http://192.168.0.138:9001/2015-03-31/functions/THIS_PARAM_SEEMS_TO_BE_IGNORED/invocations
{"statusCode":200,"body":"Foo executed"}\

So, now let's say I have one more lambda function:

lambda/bar.js

exports.handler = async (event) => {
  return { statusCode: 200, body: 'Bar executed' }
}

Is it possible to run a single docker container and invoke the corresponding lambda function based on the function name provided in the URL?

# would invoke foo
$ curl -d '{}' http://localhost:9001/2015-03-31/functions/foo/invocations
# would invoke bar
$ curl -d '{}' http://localhost:9001/2015-03-31/functions/bar/invocations

I've tried removing the command: foo.handler property from the docker-compose file, but when I do so I always seem to get the following error when trying to invoke the function:

$ curl -d '{}' http://localhost:9001/2015-03-31/functions/foo/invocations
{"errorType":"Runtime.MalformedHandlerName","errorMessage":"Bad handler"}%
mhart commented 4 years ago

No, each function needs to run in its own container. You can change which port it's exposed on though, so you could have one on 9001 and one on 9002.

marlonbernardes commented 4 years ago

Alright, thanks for getting back to me. I'm closing this issue!