如何将 API 网关与 SQS 集成

How to integrate API Gateway with SQS

就像标题一样。我尝试使用云形成将 API 网关方法与 SQS 集成。我缺少的是 SQS 的正确 URI。如果你们中有人已经这样做了,那么 URI 应该是什么样的?

我想到了类似的东西,但不知道将 SQS ARN 放在哪里

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

这里是方法的完整配置:

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"

如果您与 lambda 函数集成,这里是一个 URI 示例:

arn:aws:apigateway:us-west-2:lambda:path//2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:function_name/invocations
-

回答我自己的问题。以下是如何将 SQS 作为服务代理集成到 API 网关中:

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"

我终于在各种文档中找到了我的问题的所有答案。我猜是 RTFM。

编辑:

这里是 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"

我很确定 SQS 角色和策略应该看起来更像这样(您似乎粘贴了 lambda 角色):

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]