AWS_EN

Triggering Lambda function from SQS queue

Triggering Lambda function from SQS queue

In the following page, which is an introduction to SQS, we introduced a configuration that uses SQS queues to link data between Lambda functions.

In the above page, function that receives messages from the queue were executed manually.

In this page, we will consider automatically invoking Lambda functions as discussed in the following official AWS page.

You can use an AWS Lambda function to process messages in an Amazon SQS queue. Lambda polls the queue and invokes your Lambda function synchronously with an event that contains queue messages.

Configuring a queue to trigger an AWS Lambda function (console)

Environment

Diagram of triggering Lambda function from SQS queue

The environment is generally similar to the page discussed at the beginning of this report.

In Lambda function 2, poll the SQS queue and receive and process any messages in the queue.

CloudFormation template files

The above configuration is built with CloudFormation.
The CloudFormation templates are placed at the following URL

awstut-fa/114 at main · awstut-an-r/awstut-fa
Contribute to awstut-an-r/awstut-fa development by creating an account on GitHub.

Explanation of key points of template files

This page shows how to trigger a Lambda function on an SQS queue.

For other matters, please refer to the following pages.

Lambda

Function

Resources:
  Function2:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          def lambda_handler(event, context):
            print(event)
      FunctionName: !Sub "${Prefix}-Function2"
      Handler: !Ref Handler
      MemorySize: !Ref MemorySize
      Runtime: !Ref Runtime
      Role: !GetAtt LambdaRole2.Arn
      Timeout: !Ref Timeout
Code language: YAML (yaml)

Lambda function to poll the SQS queue.

The code to be executed by the Lambda function in inline format.
For more information, please refer to the following page.

The code to be executed is a simple one that outputs the contents of the event object.

In the page introduced at the beginning, the code polls the SQS queue, but such content is omitted.
Polling is defined in the event source mapping described below, so there is no need to implement it on the Lambda function side.

Event Source Mapping

Resources:
  EventSourceMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties: 
      BatchSize: !Ref BatchSize
      Enabled: true
      EventSourceArn: !Ref QueueArn
      FunctionName: !Ref Function2
Code language: YAML (yaml)

Create an event source mapping to associate an SQS queue with a Lambda function.
Specify the queue in the EventSourceArn property and the function in the FunctionName property.

The BatchSize property allows you to specify the maximum number of times the function will poll.
In this case, “1” is specified for verification.
In other words, one poll will retrieve a maximum of one message at a time.

IAM Role

Resources:
  LambdaRole2:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service:
                - lambda.amazonaws.com
      Policies:
        - PolicyName: GetSSMParameter
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - sqs:ReceiveMessage
                  - sqs:DeleteMessage
                  - sqs:GetQueueAttributes
                Resource:
                  - !Ref QueueArn
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Code language: YAML (yaml)

As shown below, the AWS official page mentions the permissions required to trigger the function.

The Lambda execution role must include the following permissions:
・sqs:DeleteMessage
・sqs:GetQueueAttributes
・sqs:ReceiveMessage

Configuring a queue to trigger an AWS Lambda function (console)

Following the above quote, grant three permissions to the IAM role for the Lambda function.

Architecting

Use CloudFormation to build this environment and check its actual behavior.

Create a CloudFormation stacks and check the resources in the stacks

Create CloudFormation stacks.
For information on how to create stacks and check each stack, please refer to the following pages.

After reviewing the resources in each stack, information on the main resources created in this case is as follows

  • SQS queue: fa-114-Queue
  • Lambda function 1: fa-114-Function1
  • Lambda function 2: fa-114-Function2

Check each resource from the AWS Management Console.

Check the SQS queue.

Detail of SQS 1.

The queue is successfully created.

Lambda function 2 (described below) is specified as the Lambda trigger.
This means that if a message is stored in this queue, function 2 will automatically receive the message.

Check the Lambda function.

Detail of Lambda 1.
Detail of Lambda 2.

Two functions are created.
The first function sends the current date and time data to the SQS queue as described above.
The second function outputs an event object.

Operation Check

Now that we are ready, we execute function 1.

Detail of Lambda 3.

It worked fine.

The message should now be sent from function 1 to the SQS queue, which will be received by function 2 and automatically executed.

Check the execution log of function 2.

Detail of Lambda 4.

Indeed, function 2 was executed automatically.
We can see that the event object contains the message sent to the SQS queue.

From the above, by associating a Lambda function with an SQS queue, the function could be automatically executed each time a message is stored in the queue.

Summary

We have shown you how to trigger a Lambda function from an SQS queue.

タイトルとURLをコピーしました