AWS Lambda: Invoke a Function from CLI
Invoke an AWS Lambda function using aws lambda invoke with JSON payload, invocation types (sync, async, dry-run), and log tailing.
Detailed Explanation
Invoking Lambda Functions from the CLI
The aws lambda invoke command triggers a Lambda function and captures its response. It supports synchronous and asynchronous invocation, payload input, and execution log tailing.
Basic Synchronous Invocation
aws lambda invoke \
--function-name my-processor \
--payload '{"action": "process", "id": 42}' \
response.json
The response payload is written to response.json. The command itself returns metadata like status code and executed version.
Invocation Types
| Type | Flag Value | Behavior |
|---|---|---|
| Synchronous | RequestResponse (default) |
Waits for function to complete, returns response |
| Asynchronous | Event |
Queues the invocation, returns immediately (status 202) |
| Dry Run | DryRun |
Validates permissions without executing (status 204) |
Async Invocation
aws lambda invoke \
--function-name my-processor \
--invocation-type Event \
--payload '{"action": "batch-process"}' \
/dev/null
Invoke with Log Tailing
aws lambda invoke \
--function-name my-function \
--payload '{"key": "value"}' \
--log-type Tail \
--query 'LogResult' \
--output text response.json | base64 --decode
The --log-type Tail flag returns the last 4KB of execution logs, base64-encoded. Piping through base64 --decode reveals the actual log output.
Invoke a Specific Version or Alias
aws lambda invoke \
--function-name my-function \
--qualifier prod \
response.json
Payload from File
aws lambda invoke \
--function-name my-function \
--payload file://event.json \
response.json
Using file:// reads the payload from a local file, which is cleaner for complex JSON inputs.
Use Case
Testing Lambda functions during development, triggering batch processing jobs, running one-off data transformations, or debugging function behavior with specific event payloads.