Citricguy.com

AWS API Gateway Directly to SQS FIFO Queue Without Unnecessary Lambda Invocations

Posted January 10, 2020 in Tech

Getting AWS API Gateway to send data directly to a SQS FIFO queue without having to use an intermediary Lambda function can be a bit of a challenge.

Though with a single plugin and a slight modification to your serverless.yml file you can have this up in running in just a few minutes.

Plugin Installation

First you will need to install the serverless-apigateway-service-proxy serverless plugin.

npm install serverless-apigateway-service-proxy --save

Serverless.yml Additions

Some of this may be automatically added during the plugin installation, however this should be enough to get you up and running.

Here is a rough example of what your serverless.yml should include.

plugins:
  - serverless-apigateway-service-proxy

custom:
  apiGatewayServiceProxies:
    - sqs:
        path: /sqs
        method: post
        queueName: { 'Fn::GetAtt': ['SQSQueue', 'QueueName'] }
        cors: true

        requestParameters:
          'integration.request.querystring.MessageGroupId': "'UniqueQueue'"

resources:
  Resources:
    SQSQueue:
      Type: 'AWS::SQS::Queue'
      Properties:
        QueueName: "${self:app}-${self:provider.stage}-queue.fifo"
        ContentBasedDeduplication: true
        FifoQueue: true

A few things to note:

plugins:
  - serverless-apigateway-service-proxy
  • SQS FIFO queues require the MessageGroupId parameter. Here is how we configure it with the plugin we just installed.
requestParameters:
  'integration.request.querystring.MessageGroupId': "'<your-group-identifier>'"
  • Deduplicated SQS FIFO queue names must end with .fifo and must be set to ContentBasedDeduplication: true
SQSQueue:
  Type: 'AWS::SQS::Queue'
  Properties:
    QueueName: "${self:app}-${self:provider.stage}-queue.fifo"
    ContentBasedDeduplication: true
    FifoQueue: true

© 2021 - Josh Sommers