【问题标题】:How to integrate API Gateway with SQS如何将 API Gateway 与 SQS 集成
【发布时间】:2017-04-27 02:49:17
【问题描述】:

就像标题一样。我尝试使用云形成将 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
-

【问题讨论】:

标签: amazon-web-services aws-api-gateway amazon-cloudformation


【解决方案1】:

回答我自己的问题。以下是将 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"

【讨论】:

  • 感谢这个例子,它是最接近我想做的事情,我可以在网上找到。我在这里遇到的一个问题是 RestApiRole 是什么样的?在我的云形成模板中,我正在创建要使用的 sqs 队列。有没有办法可以在模板中创建角色以便它可以访问该资源?
  • @fantapop 我已经用 RestApiRole 编辑了我的答案。它与api网关和sqs在同一个模板中。
  • 对于RestApiRole,如果这only发布到SQS,lambda Policy是不必要的,对吧?
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 2021-07-27
  • 2018-01-29
  • 2016-12-25
  • 2022-11-10
  • 2019-06-22
  • 2020-11-20
  • 2018-12-25
  • 2021-07-27
  • 1970-01-01
相关资源
最近更新 更多