首页 文章

如何将API网关与SQS集成

提问于
浏览
8

就像在 Headers 中一样 . 我尝试使用 Cloud 形成将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
-

2 回答

  • 11

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

    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"
    
  • 3

    我很确定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]
    

相关问题