如何使用 Serverless 框架将 sns 的资源策略添加到 sqs

问题描述 投票:0回答:1

我在为我的 sqs 队列分配正确的策略时遇到问题,以便任何 sns 主题都可以在该队列订阅时向它发送消息。我尝试在无服务器中使用

UpdatePolicy
Metadata
,但没有任何效果:

resources:
  Resources:
    DevNull:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: ${self:custom.serviceName}-${self:provider.stage}-dev-null-queue
      UpdatePolicy:
        policy:
          statement:
            - Effect: Allow
              Principal:
                Service: sns.amazonaws.com
                Action: sqs:SendMessage
              Resource: '*'

      Metadata:
        AWS::CloudFormation::CustomResource:
          policyStatements:
            - Effect: Allow
              Principal:
                Service: sns.amazonaws.com
              Action:
                - 'sqs:SendMessage'
              Resource: '*'

部署后,我也无法在 aws 控制台的“访问策略(权限)”选项卡中看到任何更改。 我订阅了一个电子邮件地址,以仔细检查消息是否已发送,我通过电子邮件收到了所有消息,但 sqs 中没有。

我找到了一个 SAM 模板,它可以满足我的需要,但我不知道如何使用无服务器框架编写它,而且我找不到任何相关文档。

amazon-web-services amazon-sqs serverless-framework amazon-sns
1个回答
0
投票

您需要一个允许 SNS 作为 a 的 QueuePolicy 这是我的一个项目中的一个示例,希望它有所帮助:)

Resources:
  MyQueue:
    Type: AWS::SQS::Queue
    Properties: 
      RedrivePolicy: 
        deadLetterTargetArn: 
          Fn::GetAtt:
            - MyQueueDLQ
            - Arn
        maxReceiveCount: 5

  MyQueueDLQ:
    Type: AWS::SQS::Queue

  SNSTopicToMyQueuePolicy:
      Type: AWS::SQS::QueuePolicy
      Properties:
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Sid: 'allow-sns-messages'
              Effect: Allow
              Principal:
                Service: 'sns.amazonaws.com'
              Resource:
                Fn::GetAtt:
                - MyQueue
                - Arn
              Action: 'SQS:SendMessage'
              # COMMENT THIS IN IF YOU WANT IT TO ONLY ALLOW A CERTAIN SNS TOPIC
              # Condition:
                # ArnEquals:
                  # 'aws:SourceArn':
                    # Ref: MySNSTopic
        Queues:
          - Ref: MyQueue

  // If you want the queue to subscribe to a certain topic
  QueueSubscription:
      Type: AWS::SNS::Subscription
      Properties:
        TopicArn:
          Ref: MySNSTopic
        Endpoint:
          Fn::GetAtt:
            - MyQueue
            - Arn
        Protocol: sqs
        RawMessageDelivery: true
© www.soinside.com 2019 - 2024. All rights reserved.