Skip to content

Instantly share code, notes, and snippets.

@alexkates
Last active April 19, 2024 20:00
Show Gist options
  • Save alexkates/59e3b6f582d2444e86a723d0d707a8f8 to your computer and use it in GitHub Desktop.
Save alexkates/59e3b6f582d2444e86a723d0d707a8f8 to your computer and use it in GitHub Desktop.
How to Trigger an AWS Lambda from SQS using the AWS CDK
// Import aws-cdk packages
import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';
import * as lambda from '@aws-cdk/aws-lambda';
import * as lambdaEventSources from '@aws-cdk/aws-lambda-event-sources';
export class HowToTriggerLambdaFromSqsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a new SQS Queue
const queue = new sqs.Queue(this, 'OurSqsQueue', {
queueName: 'OurSQSQueue',
});
// Create a new Lambda Function
const lambdaFunction = new lambda.Function(this, 'Function', {
code: lambda.Code.fromAsset('src'),
handler: 'index.handler',
functionName: 'SqsMessageHandler',
runtime: lambda.Runtime.NODEJS_12_X,
});
// Add an SQS Event Source from the SQS Queue to the Lambda Function
const eventSource = new lambdaEventSources.SqsEventSource(queue);
lambdaFunction.addEventSource(eventSource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment