Back to APIs

Manage your AWS databases, storage, and compute resources.

Using the AWS API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your AWS background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Example code using AWS

Below are some working code examples of how you can use AWS with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
2
import { InvokeCommand, LambdaClient, LogType } from "@aws-sdk/client-lambda";
3
import { fromEnv } from "@aws-sdk/credential-providers";
4
import z from "zod";
5
6
// Create a TriggerClient for managing trigger jobs
7
// AWS Lambda setup assumed. For more information on AWS Lambda:
8
// https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html
9
10
// Create an AWS Lambda client using AWS SDK for JavaScript (v3)
11
// For AWS SDK Lambda documentation:
12
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/lambda/
13
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-lambda/Interface/LambdaClientConfig/
14
// AWS SDK Credential Providers:
15
// https://github.com/aws/aws-sdk-js-v3/tree/main/packages/credential-providers
16
const lambdaClient = new LambdaClient({
17
region: process.env.AWS_REGION,
18
credentials: fromEnv(),
19
});
20
21
// Define a Trigger job to invoke the AWS Lambda function
22
client.defineJob({
23
// Job metadata
24
id: "invoke-aws-lambda-function",
25
name: "Invoke AWS Lambda function",
26
version: "1.0.0",
27
28
// Set up a trigger for this job, in this case, an event trigger
29
trigger: eventTrigger({
30
name: "aws",
31
32
// Define the schema for the payload. In this case, it expects a function name and a payload object with length and width.
33
schema: z.object({
34
functionName: z.string(),
35
payloadObject: z.object({ length: z.number(), width: z.number() }),
36
}),
37
}),
38
39
// Define the code to run when the job is triggered
40
run: async (payload, io, ctx) => {
41
// Wrap an SDK call in io.runTask to make it resumable and display it in logs
42
const result = await io.runTask(
43
"Invoke Lambda",
44
async () => {
45
// Create an AWS Lambda invocation command
46
const command = new InvokeCommand({
47
FunctionName: payload.functionName,
48
Payload: JSON.stringify(payload.payloadObject),
49
LogType: LogType.Tail,
50
});
51
52
// Send the command to AWS Lambda
53
const { Payload, LogResult } = await lambdaClient.send(command);
54
55
// Process the Lambda response and logs
56
const result = Buffer.from(Payload).toString();
57
const logs = Buffer.from(LogResult, "base64").toString();
58
59
// Return the computed area and associated logs as task output
60
return { result, logs };
61
},
62
// Add metadata to the task to improve its display in the logs
63
{ name: "Invoke Lambda", icon: "aws" }
64
);
65
},
66
});