Wednesday, November 11, 2020

AWS - Invoke Lambda using CLI

Invoking Lambda using CLI

Let's see how to invoke a Lambda from Command line, the CLI version used in this demonstration is "aws-cli/2.0.57"

  • Create simple Lambda, this is written using NodeJS
 exports.handler = async (event, context) => {  
   const response = {  
     statusCode: 200,  
     body: JSON.stringify('Hello from Lambda!'),  
   };  
   return response;  
 };  
  • Make sure you have setup Client Credential to run CLI commands from your local machine, refer this Blog to setup the same
  • Make sure the Credential you have setup has "AWSLambdaRole" policy assigned which entitle the Credential user to invoke Lambda
  • Execute the following command from Command Prompt
 aws lambda invoke --function-name <LAMBDA_NAME> --payload "{\"name\": \"balatamilmani\"}" --invocation-type RequestResponse --cli-binary-format raw-in-base64-out response.json  
    • --invocation-type -> Value should be "RequestResponse" for Synchronize call
    • --cli-binary-format -> Value should be "raw-in-base64-out" for AWS CLI V2

No comments:

Post a Comment