How to Trigger an AWS Lambda Function From an SQS Message

How to Trigger an AWS Lambda Function From an SQS Message

Use the AWS CDK and JavaScript to build an SQS queue that triggers a Lambda function when messages are enqueued.

Alex Kates's photo
Alex Kates
ยทJan 27, 2022ยท

3 min read

Table of contents

  • Setup
  • Create an SQS Queue
  • Create a Lambda
  • Create the Event Source
  • Testing
  • Clean Up

In this post, we are going to use the AWS CDK to build an AWS Lambda Function that is triggered from an AWS SQS message.

All of the code can be found in this repository.

Setup

We need to run a few commands to set up our CDK app.

mkdir how-to-trigger-lambda-from-sqs
cd how-to-trigger-lambda-from-sqs
npx cdk init app --language typescript

This should give you the following directory structure. Directory structure after running the CDK init command

Also, make sure you have your AWS CLI configured. For more information follow the AWS CLI quickstart guide.

Create an SQS Queue

Install the SQS CDK package.

npm i @aws-cdk/aws-sqs

Open lib/how-to-trigger-lambda-from-sqs-stack.ts, add a new SQS queue, and deploy.

import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';

export class HowToTriggerLambdaFromSqsStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const queue = new sqs.Queue(this, 'OurSqsQueue', {
      queueName: 'OurSQSQueue',
    });
  }
}

Nice! Let's deploy the stack.

npm run cdk deploy

Create a Lambda

Install the Lambda CDK package.

npm i @aws-cdk/aws-lambda

Deploying a Lambda function requires bootstrapping your CDK app which gives us an S3 bucket where our Lambda's source code will live. This is a one-time operation.

npm run cdk bootstrap

Create src/index.js and paste the following code

exports.handler = async (event) => {
  event.Records.forEach((record) => {
    console.log('Record: %j', record);
  });
};

Open lib/how-to-trigger-lambda-from-sqs-stack.ts, add a new Lambda function, and deploy.

import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';
import * as lambda from '@aws-cdk/aws-lambda';

export class HowToTriggerLambdaFromSqsStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const queue = new sqs.Queue(this, 'OurSqsQueue', {
      queueName: 'OurSQSQueue',
    });

    const lambdaFunction = new lambda.Function(this, 'Function', {
      code: lambda.Code.fromAsset('src'),
      handler: 'index.handler',
      functionName: 'SqsMessageHandler',
      runtime: lambda.Runtime.NODEJS_12_X,
    });
  }
}

Deploy again ...

npm run cdk deploy

You may need to confirm some IAM changes. IAM changes after deploying stack with Lambda function

Create the Event Source

Install the Lambda Event Sources CDK package.

npm i @aws-cdk/aws-lambda-event-sources

Open lib/how-to-trigger-lambda-from-sqs-stack.ts, add a new SqsEventSource to the Lambda Function.

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);

    const queue = new sqs.Queue(this, 'OurSqsQueue', {
      queueName: 'OurSQSQueue',
    });

    const lambdaFunction = new lambda.Function(this, 'Function', {
      code: lambda.Code.fromAsset('src'),
      handler: 'index.handler',
      functionName: 'SqsMessageHandler',
      runtime: lambda.Runtime.NODEJS_12_X,
    });

    const eventSource = new lambdaEventSources.SqsEventSource(queue);

    lambdaFunction.addEventSource(eventSource);
  }
}

One more deployment ...

npm run cdk deploy

One last time, you may need to approve IAM changes.

IAM changes approval after deploying the SQS event source

Testing

We are going to make use of the AWS CLI to test our stack.

First, we need the Queue URL of our SQS queue, which you can get using the following command

aws sqs get-queue-url --queue-name OurSQSQueue

Next, using the Queue URL from the previous command, use the AWS CLI to send a new message to OurSQSQueue. Your Queue URL may vary, but for me this command looks like this.

aws sqs send-message \
--message-body "Hello, world" \
--queue-url https://sqs.us-east-2.amazonaws.com/472331918655/OurSQSQueue

Verify that the Lambda was executed by looking in CloudWatch. Find the LogGroup named /aws/lambda/SqsMessageHandler and open up the latest LogStream. You should see some log messages that look similar to this.

CloudWatch results after sending a new SQS message

Clean Up

Don't forget to delete your stack when you are finished!

npm run cdk destroy

Thanks for reading! If you found this useful, please follow me here dev.to/thealexkates twitter.com/thealexkates

Did you find this article valuable?

Support Alex Kates by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this