Skip to content

Instantly share code, notes, and snippets.

@PrettySolution
Last active March 1, 2024 11:57
Show Gist options
  • Save PrettySolution/7596622299f1ad101c2ab9f48fcb294e to your computer and use it in GitHub Desktop.
Save PrettySolution/7596622299f1ad101c2ab9f48fcb294e to your computer and use it in GitHub Desktop.
sqs to lambda in cdk
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