To invoke any Lambda function, you need to use the lambda:Invoke
API call. From the AWS CLI, it is called via aws lambda invoke
; from boto3
it is called via boto3.client('lambda').invoke(**kwargs)
.
The most relevant arguments are:
- FunctionName. The name of the function to invoke (on the CLI as
--function-name
)
- InvocationType. Determines whether to invoke the API sync (
RequestResponse
) or async (Event
) (CLI: --invocation-type
)
- Payload. The data that becomes the
event
argument passed to the Lambda handler (CLI: --payload
, and can be provided as a file URL. E.g., --payload file://./relative/path.json
Additionally, when invoking via aws lambda invoke
you need to specify an output file, where the results of the Lambda will get written.
See:
When using Chalice, or any framework that implements "web routing"-like behavior, invoking just the desired route requires you to provide an event that looks like the event generated by API Gateway or an Application Load Balancer. I.e., if you have N routes, the framework will typically deploy just a single Lambda function and use the HTTP information encoded in the event to determine which route to invoke within the function.
See:
A minimal event file (payload.json
) could look like the following. Of particularly relevance are the path
and resource
argument. You may need to make the resource
match the value in the API Gateway Chalice generates for you.
{
"path": "/",
"resource": "/",
"queryStringParameters": {},
"headers": {},
"httpMethod": "GET",
"requestContext": {
"httpMethod": "GET"
},
"body": {}
}
The resulting CLI invocation would look like this:
aws lambda invoke --function-name fn \
--payload file://./payload.json \
--invocation-type RequestResponse \
output.txt
Hope that helps.