Chatwithcloud logo

ChatWithCloud

AWS GenAI Tools

Getting Started with AWS SDK V3 Lambda Invoke Using Typescript

Code

Below is an example on how to use AWS SDK V3 to invoke an AWS Lambda function using TypeScript:

import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda"; async function invokeLambdaFunction() { const lambdaClient = new LambdaClient({}); const invokeParams = { FunctionName: 'LambdaFunctionName', InvocationType: 'RequestResponse', Payload: JSON.stringify({ key1: 'value1', key2: 'value2', key3: 'value3' }), }; const response = await lambdaClient.send(new InvokeCommand(invokeParams)); return response.Payload; } invokeLambdaFunction() .then(payload => console.log('Lambda function output:', payload)) .catch(err => console.error(err));

Detailed Code Explanation

In the script above, we:

  1. First import the LambdaClient and InvokeCommand from the AWS SDK.
  2. Next, we create an async function invokeLambdaFunction, which will be responsible for invoking the desired Lambda function.
  3. Inside this function, we construct LambdaClient without passing anything in the constructor.
  4. We then create an object invokeParams, which contains parameters such as the name of the Lambda function we want to invoke, the invocation type, along with any payload that the Lambda function may require.
  5. Now we invoke the Lambda function using the LambdaClient.send function, passing in the new InvokeCommand along with the parameters.
  6. Payload is then logged in the console after promise resolution.

Expected Output Format

The expected output is a string containing the JSON payload as returned by the Lambda function invoked. Here's an example:

"{\"statusCode\": 200, \"body\": \"Hello from Lambda!\"}"

Note: the actual format may vary depending on the specific Lambda function that's being invoked.

Considerations & Caveats

Required IAM permissions and example policy

Here is an example of minimum required IAM policy needed for a user to effectively use LambdaClient to invoke a function:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "InvokeLambdaFunction", "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": "arn:aws:lambda:region:account-id:function:LambdaFunctionName" } ] }

FAQ

  1. What does InvocationType mean in the invokeParams?

    • This refers to the invocation type of the Lambda function. The value can be Event, RequestResponse or DryRun. Event means it's an asynchronous execution, RequestResponse is synchronous and DryRun is for validation checks only, the function is not invoked.
  2. Can we pass any other parameters in the invokeParams?

    • Yes, there are several other parameters that one can pass, such as Qualifier, ClientContext, LogType etc based on your requirements.
  3. Can I pass binary data as a payload?

  1. What happens if the Lambda function does not exist?
    • If the Lambda function does not exist or if the IAM role doesn't have the correct permissions, invoking lambdaClient.send will result in an error. The error message will specify the issue.




Related articles
How To Determine If A S3 Object Exists TypescriptDelete Old and Unused Lambda Function Versions using Typescript