Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate API Gateway with SQS

Just like in the title. I try to integrate API Gateway method with a SQS using cloud formation. What I am missing is the correct URI for the SQS. If any of you already did that, what should the URI look like?

I came up with something like that, but have no idea where to put the SQS ARN

"arn:aws:apigateway:${AWS::Region}:sqs:action/SendMessage"

Here is the full configuration for the method:

PostMethod:
    Type: "AWS::ApiGateway::Method"
    Properties:
      ApiKeyRequired: "true"
      HttpMethod: "POST"
      ResourceId: !Ref "SomeResource"
      RestApiId: !Ref "SomeRestApi"
      Integration:
        IntegrationHttpMethod: "POST"
        IntegrationResponses:
        - StatusCode: 200
        Type: "AWS"
        Uri: "arn:aws:apigateway:${AWS::Region}:sqs:action/SendMessage"

And here is an example of URI if you integrate with a lambda function:

arn:aws:apigateway:us-west-2:lambda:path//2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:function_name/invocations
-
like image 700
greg Avatar asked Dec 12 '16 09:12

greg


People also ask

Can we connect API gateway with SQS?

Create an Amazon API Gateway HTTP API that integrates with an Amazon SQS queue. In this pattern, called "Queue based leveling", a serverless queue is introduced between your API Gateway and your workers, a Lambda function in this case.

Is Amazon SQS an API?

Welcome to the Amazon SQS API Reference. Amazon SQS is a reliable, highly-scalable hosted queue for storing messages as they travel between applications or microservices. Amazon SQS moves data between distributed application components and helps you decouple these components.

Can Lambda push to SQS?

You can use a Lambda function to process messages in an Amazon Simple Queue Service (Amazon SQS) queue. Lambda event source mappings support standard queues and first-in, first-out (FIFO) queues.


2 Answers

To answer my own question. Here is how you integrate SQS as a Service Proxy in API Gateway:

PostMethod:
    Type: "AWS::ApiGateway::Method"
    Properties:
      AuthorizationType: "NONE"
      ApiKeyRequired: "true"
      HttpMethod: "POST"
      ResourceId: !Ref "SomeResource"
      RestApiId: !Ref "RestApi"
      MethodResponses:
      - StatusCode: 200
      Integration:
        Credentials: !GetAtt "RestApiRole.Arn"
        IntegrationHttpMethod: "POST"
        IntegrationResponses:
        - StatusCode: 200
        Type: "AWS"
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:sqs:action/SendMessage"
        RequestParameters:
          integration.request.querystring.QueueUrl: !Sub "'${SomeQueue}'"
          integration.request.querystring.MessageBody: "method.request.body"

I've finally found all answers to my questions in various documentation. RTFM I guess.

EDIT:

and here the code for RestApiRole:

RestApiRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
        - Action:
          - "sts:AssumeRole"
          Principal:
            Service:
            - "apigateway.amazonaws.com"
          Effect: "Allow"
      Policies:
      - PolicyName: "InvokeLambda"
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
          - Action:
            - "lambda:InvokeFunction"
            Resource: !GetAtt "LambdaFunction.Arn"
            Effect: "Allow"
like image 110
greg Avatar answered Oct 15 '22 08:10

greg


I'm pretty sure the SQS role and policy should look more like this (you seem to have pasted the lambda role instead):

SQSRole:
   Type: AWS::IAM::Role
   Properties:
    AssumeRolePolicyDocument:
     Version: '2012-10-17'
     Statement:
      - Effect: Allow
        Principal:
         Service:
          - apigateway.amazonaws.com
        Action: sts:AssumeRole
    Path: /
  SQSRolePolicy:
    Type: AWS::IAM::Policy
    DependsOn: [SQSRole]
    Description: IAM policy applied to the service role.
    Properties:
      PolicyName: send-messages-sqs
      PolicyDocument:
        Statement:
        - Action:
            - sqs:SendMessage
          Resource:
            - !Sub arn:aws:sqs:${AWS::Region}:${AWS::AccountId}:QUEUE_NAME
          Effect: Allow
      Roles: [!Ref SQSRole]
like image 27
joakim Avatar answered Oct 15 '22 10:10

joakim