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:
- First import the
LambdaClient
andInvokeCommand
from the AWS SDK. - Next, we create an async function
invokeLambdaFunction
, which will be responsible for invoking the desired Lambda function. - Inside this function, we construct
LambdaClient
without passing anything in the constructor. - 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. - Now we invoke the Lambda function using the
LambdaClient.send
function, passing in thenew InvokeCommand
along with the parameters. - 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
- If the Lambda function throws an error during execution or if it does not exist, the AWS SDK will throw an exception.
- Any payload provided to the
LambdaClient.send
method must be in the form of a stringified JSON object. - Invocation types can be
RequestResponse
,Event
, orDryRun
. Choosing a wrong type might lead to unintended results or errors.
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
-
What does InvocationType mean in the
invokeParams
?- This refers to the invocation type of the Lambda function. The value can be
Event
,RequestResponse
orDryRun
.Event
means it's an asynchronous execution,RequestResponse
is synchronous andDryRun
is for validation checks only, the function is not invoked.
- This refers to the invocation type of the Lambda function. The value can be
-
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.
- Yes, there are several other parameters that one can pass, such as
-
Can I pass binary data as a payload?
- Yes. But the binary data needs to be base64-encoded before sending and decoded in the lambda function.
- 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.
- If the Lambda function does not exist or if the IAM role doesn't have the correct permissions, invoking