Learn to manage AWS Lambda functions using the AWS CLI, including listing, creating, and invoking functions synchronously and asynchronously.
In this lesson, you will learn how to manage AWS Lambda functions using the AWS CLI. We cover listing functions, creating a function, and invoking it both synchronously and asynchronously.
Let’s begin by creating a Lambda function. For demonstration purposes, we will name the function lambdaDemo and use the default settings. The function logs the incoming event and returns it as the response.Below is the final version of our handler code:
You can list all Lambda functions in a specific region using the AWS CLI. For example, to list functions in the US East (N. Virginia) region, run the following command:
Copy
Ask AI
aws lambda list-functions --region us-east-1
This command outputs a JSON with details about each function. A sample output is shown below:
Invoking your Lambda function synchronously returns an immediate response. Use the command below to invoke lambdaDemo with a JSON payload that serves as the event object:
For asynchronous invocation, add the --invocation-type Event flag. In this mode, the function’s response is not returned directly; instead, a status code is provided to indicate that the request was accepted.Run the following command:
The asynchronous invocation returns a status code, for example:
Copy
Ask AI
{ "StatusCode": 202}
Since execution happens in the background, the output is not stored in response.json. To check the results of the function, review the associated CloudWatch logs.
When invoking functions asynchronously, ensure you monitor CloudWatch logs to verify execution and troubleshoot issues.