SBN

How to send and receive messages between AWS Lambda and Amazon SQS

2 minutes read


POSTED Apr, 2022

dot

IN
Serverless

How to send and receive messages between AWS Lambda and Amazon SQS

Emin Bilgic

Written by Emin Bilgic

Software Engineer at Thundra

linkedin-share

 X

Amazon SQS can trigger an AWS Lambda function by sending a request and can be triggered by an AWS Lambda function by receiving a message from it. We will demonstrate a scenario that realizes those two cases for an educational purpose for beginners.

In this brief article, we will learn how to trigger an AWS Lambda function from an Amazon SQS queue and send a message to an Amazon SQS queue from an AWS Lambda function. To manage our stack we will use AWS Cloud Development Kit (CDK) written in Typescript language because of its popularity. You can check the completed project from my GitHub.

Creating and deploying the app step by step

1. Initializing the CDK template:

Run the command below in an empty directory and your app will be created in a few seconds. $ cdk init app --language=typescript

2. Creating the ExampleLambda function

We will deploy the function as an asset, so let’s create a Lambda function. I have created the function inside the lib/src directory in my project. It is a pretty simple Nodejs code that sends the given event as a message to the SQS resource that has the URL that comes from environment variables.


var AWS = require('aws-sdk');
const QUEUE_URL = process.env.SECOND_QUEUE_URL
exports.handler = function (event, callback) {
 
 var sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
 
 var params = {
   DelaySeconds: 2,
 
   MessageBody: JSON.stringify(event),
   QueueUrl: QUEUE_URL
 };
 
 sqs.sendMessage(params, function (err, data) {
   if (err) {
     console.log("Error", err);
   } else {
     console.log("Success", data.MessageId);
   }
 });
};

Info: Do not use ES6 for your Lambda function because AWS Lambda does not support this yet.

3. Creating other resources:

To create the other resources of the project, go to the stack constructor file in the lib directory. Here we have two SQS queues, one Lambda Functions, and some helper resources to run them properly.

    1. First, create the SQS resources like below:

const queue1 = new sqs.Queue(this, 'FirstQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheFirstQueue"
   });
   const queue2 = new sqs.Queue(this, 'SecondQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheSecondQueue"
   });

There is nothing more to do as an extra work here.

Secondly, create the Lambda function and set the directory of your code handler. But before all, set the SQS URL that we will send a message from the Lambda function.


const LambdaFunction = new aws_Lambda.Function(this, 'LambdaFunction1', {
     code: aws_Lambda.Code.fromAsset('lib/src'),
     handler: index.handler',
     functionName: 'DemoLambdaFunction',
     runtime: aws_Lambda.Runtime.NODEJS_12_X,
     environment: {
       SECOND_QUEUE_URL: queue2.queueUrl
     }
   });

At this point, the ExampleLambda function is created and it communicated with the SecondQueue SQS queue successfully.

But we need to give permission to send messages to this queue otherwise there will occur a permission error.


LambdaFunction.addToRolePolicy(new aws_iam.PolicyStatement({
     effect: aws_iam.Effect.ALLOW,
     resources: [queue2.queueArn],
     actions: ["*"],
   }))

Finally, we will add the FirstQueue queue as an event source to our Lambda function to receive messages from it. Here we don’t need to give any permission to our Lambda function because the event source object assigns related permissions to it.


const LambdaEventSource = new aws_Lambda_event_sources.SqsEventSource(queue1, {
     batchSize: 1
   });
  
   LambdaFunction.addEventSource(LambdaEventSource);

Also, we set batchSize to 1, so each message we sent to the FirstQueue queue will reach the Lambda function without waiting for other messages.

Validating the flow

That’s all, now that we created all the required resources let’s deploy the stack and test if the messages will reach the SecondQueue.

$ cdk deploy

After deployment, we will send a message to the FirstQueue queue from our terminal:


$ aws sqs send-message --queue-url <Your Queue URL> --message-body "Information about the largest city in Any Region." --delay-seconds 2

Let’s check if our message reached the SecondQueue.

I am using Thundra APM to monitor my serverless resources but you can also simply check the monitoring section from the AWS console.

I like the way how I can trace the whole flow of my application in the Thundra APM console.

If you are using the AWS console, you will see the invocations on the monitoring section of the Lambda Function page or received messages on the monitoring section of the SQS page.

×

SUBSCRIBE TO OUR BLOG

Get our new blogs delivered straight to your inbox.

 

THANKS FOR SIGNING UP!

We’ll make sure to share the best materials crafted for you!

2 minutes read


POSTED Apr, 2022

dot

IN
Serverless

How to send and receive messages between AWS Lambda and Amazon SQS

Emin Bilgic

Written by Emin Bilgic

Software Engineer at Thundra

linkedin-share

 X

Amazon SQS can trigger an AWS Lambda function by sending a request and can be triggered by an AWS Lambda function by receiving a message from it. We will demonstrate a scenario that realizes those two cases for an educational purpose for beginners.

In this brief article, we will learn how to trigger an AWS Lambda function from an Amazon SQS queue and send a message to an Amazon SQS queue from an AWS Lambda function. To manage our stack we will use AWS Cloud Development Kit (CDK) written in Typescript language because of its popularity. You can check the completed project from my GitHub.

Creating and deploying the app step by step

1. Initializing the CDK template:

Run the command below in an empty directory and your app will be created in a few seconds. $ cdk init app --language=typescript

2. Creating the ExampleLambda function

We will deploy the function as an asset, so let’s create a Lambda function. I have created the function inside the lib/src directory in my project. It is a pretty simple Nodejs code that sends the given event as a message to the SQS resource that has the URL that comes from environment variables.


var AWS = require('aws-sdk');
const QUEUE_URL = process.env.SECOND_QUEUE_URL
exports.handler = function (event, callback) {
 
 var sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
 
 var params = {
   DelaySeconds: 2,
 
   MessageBody: JSON.stringify(event),
   QueueUrl: QUEUE_URL
 };
 
 sqs.sendMessage(params, function (err, data) {
   if (err) {
     console.log("Error", err);
   } else {
     console.log("Success", data.MessageId);
   }
 });
};

Info: Do not use ES6 for your Lambda function because AWS Lambda does not support this yet.

3. Creating other resources:

To create the other resources of the project, go to the stack constructor file in the lib directory. Here we have two SQS queues, one Lambda Functions, and some helper resources to run them properly.

    1. First, create the SQS resources like below:

const queue1 = new sqs.Queue(this, 'FirstQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheFirstQueue"
   });
   const queue2 = new sqs.Queue(this, 'SecondQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheSecondQueue"
   });

There is nothing more to do as an extra work here.

Secondly, create the Lambda function and set the directory of your code handler. But before all, set the SQS URL that we will send a message from the Lambda function.


const LambdaFunction = new aws_Lambda.Function(this, 'LambdaFunction1', {
     code: aws_Lambda.Code.fromAsset('lib/src'),
     handler: index.handler',
     functionName: 'DemoLambdaFunction',
     runtime: aws_Lambda.Runtime.NODEJS_12_X,
     environment: {
       SECOND_QUEUE_URL: queue2.queueUrl
     }
   });

At this point, the ExampleLambda function is created and it communicated with the SecondQueue SQS queue successfully.

But we need to give permission to send messages to this queue otherwise there will occur a permission error.


LambdaFunction.addToRolePolicy(new aws_iam.PolicyStatement({
     effect: aws_iam.Effect.ALLOW,
     resources: [queue2.queueArn],
     actions: ["*"],
   }))

Finally, we will add the FirstQueue queue as an event source to our Lambda function to receive messages from it. Here we don’t need to give any permission to our Lambda function because the event source object assigns related permissions to it.


const LambdaEventSource = new aws_Lambda_event_sources.SqsEventSource(queue1, {
     batchSize: 1
   });
  
   LambdaFunction.addEventSource(LambdaEventSource);

Also, we set batchSize to 1, so each message we sent to the FirstQueue queue will reach the Lambda function without waiting for other messages.

Validating the flow

That’s all, now that we created all the required resources let’s deploy the stack and test if the messages will reach the SecondQueue.

$ cdk deploy

After deployment, we will send a message to the FirstQueue queue from our terminal:


$ aws sqs send-message --queue-url <Your Queue URL> --message-body "Information about the largest city in Any Region." --delay-seconds 2

Let’s check if our message reached the SecondQueue.

I am using Thundra APM to monitor my serverless resources but you can also simply check the monitoring section from the AWS console.

I like the way how I can trace the whole flow of my application in the Thundra APM console.

If you are using the AWS console, you will see the invocations on the monitoring section of the Lambda Function page or received messages on the monitoring section of the SQS page.

Amazon SQS can trigger an AWS Lambda function by sending a request and can be triggered by an AWS Lambda function by receiving a message from it. We will demonstrate a scenario that realizes those two cases for an educational purpose for beginners.

In this brief article, we will learn how to trigger an AWS Lambda function from an Amazon SQS queue and send a message to an Amazon SQS queue from an AWS Lambda function. To manage our stack we will use AWS Cloud Development Kit (CDK) written in Typescript language because of its popularity. You can check the completed project from my GitHub.

Creating and deploying the app step by step

1. Initializing the CDK template:

Run the command below in an empty directory and your app will be created in a few seconds. $ cdk init app --language=typescript

2. Creating the ExampleLambda function

We will deploy the function as an asset, so let’s create a Lambda function. I have created the function inside the lib/src directory in my project. It is a pretty simple Nodejs code that sends the given event as a message to the SQS resource that has the URL that comes from environment variables.


var AWS = require('aws-sdk');
const QUEUE_URL = process.env.SECOND_QUEUE_URL
exports.handler = function (event, callback) {
 
 var sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
 
 var params = {
   DelaySeconds: 2,
 
   MessageBody: JSON.stringify(event),
   QueueUrl: QUEUE_URL
 };
 
 sqs.sendMessage(params, function (err, data) {
   if (err) {
     console.log("Error", err);
   } else {
     console.log("Success", data.MessageId);
   }
 });
};

Info: Do not use ES6 for your Lambda function because AWS Lambda does not support this yet.

3. Creating other resources:

To create the other resources of the project, go to the stack constructor file in the lib directory. Here we have two SQS queues, one Lambda Functions, and some helper resources to run them properly.

    1. First, create the SQS resources like below:

const queue1 = new sqs.Queue(this, 'FirstQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheFirstQueue"
   });
   const queue2 = new sqs.Queue(this, 'SecondQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheSecondQueue"
   });

There is nothing more to do as an extra work here.

Secondly, create the Lambda function and set the directory of your code handler. But before all, set the SQS URL that we will send a message from the Lambda function.


const LambdaFunction = new aws_Lambda.Function(this, 'LambdaFunction1', {
     code: aws_Lambda.Code.fromAsset('lib/src'),
     handler: index.handler',
     functionName: 'DemoLambdaFunction',
     runtime: aws_Lambda.Runtime.NODEJS_12_X,
     environment: {
       SECOND_QUEUE_URL: queue2.queueUrl
     }
   });

At this point, the ExampleLambda function is created and it communicated with the SecondQueue SQS queue successfully.

But we need to give permission to send messages to this queue otherwise there will occur a permission error.


LambdaFunction.addToRolePolicy(new aws_iam.PolicyStatement({
     effect: aws_iam.Effect.ALLOW,
     resources: [queue2.queueArn],
     actions: ["*"],
   }))

Finally, we will add the FirstQueue queue as an event source to our Lambda function to receive messages from it. Here we don’t need to give any permission to our Lambda function because the event source object assigns related permissions to it.


const LambdaEventSource = new aws_Lambda_event_sources.SqsEventSource(queue1, {
     batchSize: 1
   });
  
   LambdaFunction.addEventSource(LambdaEventSource);

Also, we set batchSize to 1, so each message we sent to the FirstQueue queue will reach the Lambda function without waiting for other messages.

Validating the flow

That’s all, now that we created all the required resources let’s deploy the stack and test if the messages will reach the SecondQueue.

$ cdk deploy

After deployment, we will send a message to the FirstQueue queue from our terminal:


$ aws sqs send-message --queue-url <Your Queue URL> --message-body "Information about the largest city in Any Region." --delay-seconds 2

Let’s check if our message reached the SecondQueue.

I am using Thundra APM to monitor my serverless resources but you can also simply check the monitoring section from the AWS console.

I like the way how I can trace the whole flow of my application in the Thundra APM console.

If you are using the AWS console, you will see the invocations on the monitoring section of the Lambda Function page or received messages on the monitoring section of the SQS page.

Amazon SQS can trigger an AWS Lambda function by sending a request and can be triggered by an AWS Lambda function by receiving a message from it. We will demonstrate a scenario that realizes those two cases for an educational purpose for beginners.

In this brief article, we will learn how to trigger an AWS Lambda function from an Amazon SQS queue and send a message to an Amazon SQS queue from an AWS Lambda function. To manage our stack we will use AWS Cloud Development Kit (CDK) written in Typescript language because of its popularity. You can check the completed project from my GitHub.

Creating and deploying the app step by step

1. Initializing the CDK template:

Run the command below in an empty directory and your app will be created in a few seconds. $ cdk init app --language=typescript

2. Creating the ExampleLambda function

We will deploy the function as an asset, so let’s create a Lambda function. I have created the function inside the lib/src directory in my project. It is a pretty simple Nodejs code that sends the given event as a message to the SQS resource that has the URL that comes from environment variables.


var AWS = require('aws-sdk');
const QUEUE_URL = process.env.SECOND_QUEUE_URL
exports.handler = function (event, callback) {
 
 var sqs = new AWS.SQS({ apiVersion: '2012-11-05' });
 
 var params = {
   DelaySeconds: 2,
 
   MessageBody: JSON.stringify(event),
   QueueUrl: QUEUE_URL
 };
 
 sqs.sendMessage(params, function (err, data) {
   if (err) {
     console.log("Error", err);
   } else {
     console.log("Success", data.MessageId);
   }
 });
};

Info: Do not use ES6 for your Lambda function because AWS Lambda does not support this yet.

3. Creating other resources:

To create the other resources of the project, go to the stack constructor file in the lib directory. Here we have two SQS queues, one Lambda Functions, and some helper resources to run them properly.

    1. First, create the SQS resources like below:

const queue1 = new sqs.Queue(this, 'FirstQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheFirstQueue"
   });
   const queue2 = new sqs.Queue(this, 'SecondQueue', {
     visibilityTimeout: Duration.seconds(30),
     queueName: "TheSecondQueue"
   });

There is nothing more to do as an extra work here.

Secondly, create the Lambda function and set the directory of your code handler. But before all, set the SQS URL that we will send a message from the Lambda function.


const LambdaFunction = new aws_Lambda.Function(this, 'LambdaFunction1', {
     code: aws_Lambda.Code.fromAsset('lib/src'),
     handler: index.handler',
     functionName: 'DemoLambdaFunction',
     runtime: aws_Lambda.Runtime.NODEJS_12_X,
     environment: {
       SECOND_QUEUE_URL: queue2.queueUrl
     }
   });

At this point, the ExampleLambda function is created and it communicated with the SecondQueue SQS queue successfully.

But we need to give permission to send messages to this queue otherwise there will occur a permission error.


LambdaFunction.addToRolePolicy(new aws_iam.PolicyStatement({
     effect: aws_iam.Effect.ALLOW,
     resources: [queue2.queueArn],
     actions: ["*"],
   }))

Finally, we will add the FirstQueue queue as an event source to our Lambda function to receive messages from it. Here we don’t need to give any permission to our Lambda function because the event source object assigns related permissions to it.


const LambdaEventSource = new aws_Lambda_event_sources.SqsEventSource(queue1, {
     batchSize: 1
   });
  
   LambdaFunction.addEventSource(LambdaEventSource);

Also, we set batchSize to 1, so each message we sent to the FirstQueue queue will reach the Lambda function without waiting for other messages.

Validating the flow

That’s all, now that we created all the required resources let’s deploy the stack and test if the messages will reach the SecondQueue.

$ cdk deploy

After deployment, we will send a message to the FirstQueue queue from our terminal:


$ aws sqs send-message --queue-url <Your Queue URL> --message-body "Information about the largest city in Any Region." --delay-seconds 2

Let’s check if our message reached the SecondQueue.

I am using Thundra APM to monitor my serverless resources but you can also simply check the monitoring section from the AWS console.

I like the way how I can trace the whole flow of my application in the Thundra APM console.

If you are using the AWS console, you will see the invocations on the monitoring section of the Lambda Function page or received messages on the monitoring section of the SQS page.

*** This is a Security Bloggers Network syndicated blog from Thundra blog authored by Emin Bilgic. Read the original post at: https://blog.thundra.io/how-to-send-and-receive-messages-between-aws-lambda-and-amazon-sqs

Secure Guardrails