Last active
March 1, 2024 11:57
-
-
Save PrettySolution/7596622299f1ad101c2ab9f48fcb294e to your computer and use it in GitHub Desktop.
sqs to lambda in cdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SQSEvent } from 'aws-lambda' | |
export const handler = async (event: SQSEvent) => { | |
const batchItemFailures: { itemIdentifier: string }[] = [] | |
try { | |
const sqsMessages = event.Records | |
for (const message of sqsMessages) { | |
const messageId = message.messageId | |
try { // your Logic goes here | |
console.log(`OK: Message processed successfully`) | |
} catch (err) { | |
console.log(`FAIL: ${err}`) | |
batchItemFailures.push({ itemIdentifier: messageId }) | |
} | |
} | |
return { | |
statusCode: 200, | |
batchItemFailures: batchItemFailures, | |
body: 'OK: Message processed successfully', | |
} | |
} catch (err) { | |
return { | |
statusCode: 500, | |
// Allow functions to return partially successful responses for a batch of records | |
batchItemFailures: event.Records.map(e => ({ itemIdentifier: e.messageId })), | |
body: err, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment