Skip to content
Latchkey

aws lambda invoke: Call a Lambda from CI

aws lambda invoke runs a Lambda function synchronously or asynchronously and writes its response to a file, returning the status code and any FunctionError.

Smoke tests and post-deploy checks invoke the function directly. The classic trap is that a handler exception still returns HTTP 200; you must inspect the response body and FunctionError, not just the exit code.

What it does

aws lambda invoke executes --function-name, sending --payload as the event and writing the function output to the trailing outfile argument. The metadata response includes StatusCode and, if the handler threw, FunctionError set to "Unhandled" or "Handled".

Common usage

Terminal
aws lambda invoke \
  --function-name my-fn \
  --payload '{"ping":true}' \
  --cli-binary-format raw-in-base64-out \
  --log-type Tail \
  --query 'LogResult' --output text out.json | base64 -d
cat out.json

Options

FlagWhat it does
--function-name <name>Function name, ARN, or partial ARN
--payload <json|file://>Event passed to the handler
--cli-binary-format raw-in-base64-outAWS CLI v2: treat --payload as raw JSON
--invocation-type RequestResponse|EventSync vs async (async returns 202)
--log-type TailReturn last 4KB of logs in LogResult (base64)
--qualifier <version|alias>Invoke a specific version or alias

In CI

On AWS CLI v2 you must pass --cli-binary-format raw-in-base64-out, or --payload '{"k":1}' is interpreted as base64 and the handler receives garbage. A handler exception returns StatusCode 200 with FunctionError set, so gate on --query FunctionError being null, not on the CLI exit code alone.

Common errors in CI

"Invalid base64: ..." means you omitted --cli-binary-format raw-in-base64-out on CLI v2. "An error occurred (ResourceNotFoundException) when calling the Invoke operation: Function not found" means a wrong name/region/alias. "AccessDeniedException" means missing lambda:InvokeFunction. A 200 with "FunctionError":"Unhandled" in the output means the handler itself threw; read out.json for the stack trace.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →