Running and Testing Functions Locally¶
Simple¶
Using Docker ¶
This method builds and runs evaluation functions in the same way they are deployed on AWS as Lambda functions. Extending a pre-built and AWS-maintained base python image, the container contains a HTTP client which can be used to locally simulate Lambda execution events.
Note that this is different from the simple method proposed, in that it gives access to all the functionality provided by the base layer. This means that commands such as docs
and healthcheck
can be tested.
-
Install Docker on your machine
-
Navigate to the root directory of your function
-
Build the image. This will pull our base image from Dockerhub, extend it with files specific to your evaluation function and name it
eval-tmp
.docker image build -t eval-tmp app
-
Spin up a container using the image built in the previous step.
docker run --rm -d --name eval-function -p 9000:8080 eval-tmp
-
You can now simulate requests to the function using any request client (like Insomnia or Postman). By default, the url you can hit is:
http://localhost:9000/2015-03-31/functions/function/invocations
Warning
When deployed, our Lambda functions are triggered by calls made through an AWS API Gateway. This means that when testing locally, events sent should follow the structure of events triggered by that resource. That is, if you want to simulate what it would be like to make web requests to the deployed function.
Specifically, this means structuring requests in the following way:
{ "headers": { "command": "eval" }, "body": { "response": "a", "answer": "a", "params": { "garlic": "moreish" } } }
The main difference is that
headers
andbody
are sent as keys in the main body of the local request. When hitting the deployed function through the API Gateway, thecommand
field would instead be passed in the actual HTTP headers of the request - and the actual request body would only contain theresponse
,answer
andparams
fields. -
(Optional) The
run
command specifies the -d flag, which spins up the container in detached mode. If you want to inspect the logs of the function, you can run:docker container logs -f eval-function
Tip
You will very rarely need this, but you can peek into the running container by opening a shell within it using:
docker exec -it eval-function bash