🦋

Sending & Receiving SQS Messages from Lambda

This article was automatically translated from theJapanese original by AI. It may contain translation errors.

This post was published over 2 years ago. Its content may be outdated.

I did Lambda->SQS->Lambda. Built with the SDK for JavaScript (v3). There’s a lot of v2 information out there on the web, so it took a while to find what I needed… Doing this ate up half a day…orz

What I Wanted to Do

Have Lambda1 publish a large number of queue messages, and have Lambda2 receive them and process them regardless of order.

Prerequisites

  • Lambda is available
  • An SQS queue has been created
  • An execution role and access permissions have been set on the Lambda (for SQS, etc.)

The Actual Code

Publishing an SQSMessage from Lambda

import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
 
// This is issued when you create an SQS queue
const SQSQueueUrl = "https://sqs.ap-northeast-1.amazonaws.com/0000222224444/xxxxxxxxxx";

const region = 'ap-northeast-1'
const sqsClient = new SQSClient({
  region,
});

export const handler = async (event) => {
  const queueMessage = JSON.stringify({foo: "bar"})
  const sendedSQSOutput = await sqsClient.send(
    new SendMessageCommand({
      QueueUrl: SQSQueueUrl,
      // DelaySeconds: 3,
      // Two ways to do it?
      MessageAttributes: {
        foo: {
          DataType: "String",
          StringValue: "bar",
        },
      },
      MessageBody: queueMessage,
    })  
  )

  return {
    statusCode: 200,
    body: "success",
  };
}

:::message You can put values in both MessageAttributes and MessageBody. If you don’t grant the Lambda access to SQS, you’ll get an error. :::

The Receiving Lambda

 export const handler = async (event) => {
 
   // Response
  console.log(event.Records[0]);
  
  const response = {
    statusCode: 200,
    body: "success",
  };
  return response;
};

Contents of the response event.Records[0]

{
  messageId: '06c893f9-xxxx-47f6-xxxx-01c0e29c4ab5',
  receiptHandle: 'xxxxxxx',
  body: '{"foo":"bar"}',
  attributes: {
    ApproximateReceiveCount: '1',
    AWSTraceHeader: 'Root=1-xxxxxxx-xxx;Parent=xx;Sampled=0;Lineage=xxx:0',
    SentTimestamp: '1707641273576',
    SenderId: 'xxxx:xxxxxxx',
    ApproximateFirstReceiveTimestamp: '1707641273584'
  },
  messageAttributes: {
    foo: {
      stringValue: 'bar',
      stringListValues: [],
      binaryListValues: [],
      dataType: 'String'
    }
  },
  md5OfMessageAttributes: 'xxxxxxxxxxxxxxxxx',
  md5OfBody: 'xxxxxxxxxxxxxxxxx',
  eventSource: 'aws:sqs',
  eventSourceARN: 'arn:aws:sqs:ap-northeast-1:xxxxxxxx:xxxxxxxxxxx',
  awsRegion: 'ap-northeast-1'
}

Wrapping Up

This was my first time using SQS. If there are any mistakes, please leave a comment! (=^x^=)

Resources

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sqs.html https://docs.aws.amazon.com/ja_jp/sdk-for-javascript/v3/developer-guide/javascript_sqs_code_examples.html

Recent Articles

Network(beta)

Drag to move / Ctrl+wheel to zoom