Skip to main content

Invoke a function

The runtime implements the AWS Lambda™ Invoke API on port 8080: POST /2015-03-31/functions/{name}/invocations. {name} is function, or the value of AWS_LAMBDA_FUNCTION_NAME. The examples use the container fn and the credentials of Run in production.

curl, with a bearer token​

curl -s -H "Authorization: Bearer $TOKEN" \
localhost:8080/2015-03-31/functions/function/invocations -d '{"name":"curl"}'

A function error still answers 200, with the header X-Amz-Function-Error: Unhandled and the error as body. Add -i to see it.

AWS CLI, with SigV4​

From a container on the function's network:

docker run --rm --network fn-net \
-e AWS_ACCESS_KEY_ID="$KEY_ID" -e AWS_SECRET_ACCESS_KEY="$KEY_SECRET" -e AWS_DEFAULT_REGION=us-east-1 \
amazon/aws-cli:2.39.5 lambda invoke --endpoint-url http://fn:8080 --function-name function \
--payload '{"name":"cli"}' --cli-binary-format raw-in-base64-out /dev/stdout

Any region works. Add --log-type Tail to get the last 4 KB of the invocation logs.

AWS SDK​

Point the Lambda client at the runtime with the endpoint option and the SigV4 pair. AWS SDK v3 for JavaScript:

import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';

const client = new LambdaClient({ endpoint: 'http://fn:8080', region: 'us-east-1' });
const out = await client.send(new InvokeCommand({
FunctionName: 'function',
Payload: Buffer.from(JSON.stringify({ name: 'sdk' })),
}));
console.log(out.FunctionError ?? 'ok', Buffer.from(out.Payload).toString());

The SDK reads AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the environment. It retries 429 answers by itself.

From inside the container​

invoke reads the event on stdin and invokes through the loopback, without credential:

echo '{"name":"exec"}' | docker exec -i fn /loch/lambda-runtime invoke --log-tail

It prints one JSON envelope: statusCode, functionError, requestId, durationMs, payload (base64) and logTail (base64). Exit codes: 0 delivered (whatever the function returned), 1 transport error or caller gone, 2 unusable event.

Options: Command line reference.

Good to know​

  • One invocation runs at a time. The others wait in the queue; a full queue answers 429 with Retry-After.
  • Asynchronous invocations (--invocation-type Event) get 400. DryRun gets 204.
  • The request id is always generated by the runtime: read it in x-amzn-RequestId.
  • Every status code and error: Invoke API reference.

"AWS" and "AWS Lambda" are trademarks of Amazon.com, Inc. or its affiliates. This project is not affiliated with or endorsed by Amazon. It implements the publicly documented AWS Lambda Runtime and Invoke APIs and contains no code from the AWS Lambda Runtime Interface Emulator.