API gateway integration with SQS

I have a small Serverless application which is an API gateway with lambda events, all good.

I now have some requirements that means that I need an SQS resource in front of one of the lambda functions as there is a need for replays.

I would like to be able to get my serverless app to create an api gateway endpoint that forwards on the request to SQS (the queue would then be added as an event for the lambda function. All possible through the AWS console, but I don’t know if this can be done through serverless.

I know through serverless I could have an intermediate lambda function, that has an apigateway event, that then forwards the request to SQS, which in turn calls the proper lambda function, but this is an extra step that I don’t think should be needed.

Can anyone advise on the best way to achieve this?

4 Likes

We are in the process of migrating raw CloudFormation templates to Serverless framework, and we will be having the same need soon enough. I didn’t see another integration for API Gateway than “lambda”. Is there hope that a “awsService” integration would be supported soon enough? Or should we handle that with a plugin?

Thanks in advance for your help!

Had anyone sorted this out?

1 Like

Did anyone manage to find a solution for this?

I am also trying to put an SQS queue behind an API Gateway endpoint.

I have the same issue, I need make integration api gateway-sqs.

I found plugins like:

However, they are incompatible with versions major than >1.8 of Serverless Framework and im using version major 2.

I know that this is possible by cloudformation in the Serverless Framework, but the configuration is not intuitive for me.

If anyone found a simple way or just a way for this integration api gateway-sqs by Serverless Framework, please help us.

2 Likes

Hi, I found a solution.

resources:
  Resources:
    # Define an AWS API Gateway named MyApi
    MyApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: MyApi

    # Define a resource named serverless under MyApi
    ProxyResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId: !GetAtt MyApi.RootResourceId
        PathPart: serverless
        RestApiId: !Ref MyApi

    # Define a POST method for the serverless resource
    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        AuthorizationType: NONE
        ResourceId: !Ref ProxyResource
        RestApiId: !Ref MyApi
        HttpMethod: POST

        # Integration details for the method
        Integration:
          Credentials: arn:aws:iam::${param:account, 'your_account_id'}:role/system.role
          IntegrationHttpMethod: POST
          Type: AWS
          Uri: !Sub 'arn:aws:apigateway:${param:region, 'your_region'}:sqs:path/your_account_id/your_queue'
          IntegrationResponses:
            - StatusCode: 200
          RequestTemplates:
            application/json: |
              Action=SendMessage&MessageBody=$input.body
          RequestParameters:
            integration.request.querystring.MessageBody: "method.request.body"
            integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"

        # Method response details
        MethodResponses:
          - ResponseModels: { "application/json": "Empty" }
            StatusCode: 200
        RequestParameters:
          method.request.header.AnotherHeader: false

    # Define an API Gateway stage named serverless
    ApiGatewayStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        StageName: serverless
        RestApiId: !Ref MyApi
        DeploymentId: !GetAtt ApiGatewayDeployment.DeploymentId

    # Define an API Gateway deployment
    ApiGatewayDeployment:
      Type: AWS::ApiGateway::Deployment
      DependsOn: ProxyMethod
      Properties:
        RestApiId: !Ref MyApi

${param:account, 'your_account_id'} : Replace ‘your_account_id’ with your AWS account ID.
${param:region, 'your_region'} : Replace ‘your_region’ with the AWS region you want to use.
Replace ‘your_queue’ with the name of your SQS queue.