Amazon AWS SQS - 将QueuePolicy应用于现有队列

时间:2017-10-09 14:02:24

标签: amazon-web-services amazon-cloudformation amazon-sqs

如果我通过Cloudformation创建SQS队列,您是否可以在创建SQS队列后附加第二个QueuePolicy?

如果我按照以下配置运行:

Resources:
  SQSQueue:
    Properties:
      QueueName: !Ref SQSQueuename
    Type: 'AWS::SQS::Queue'
  QueuePolicy:
    Type: 'AWS::SQS::QueuePolicy'
    Properties:
      PolicyDocument:
        Id: !Ref SQSQueuename
        Statement:
          - Sid: QueuePolicy2-SendMessage-To-Queue-From-SNS-Topic
            Effect: Allow
            Principal:
              AWS: !Ref AccountID
            Action:
              - 'sqs:*'
            Resource: 'arn:aws:sqs:eu-central-1:123456789010:${SQSQueuename}'
      Queues:
        - !Ref SQSQueue 
   DependsOn:
     - SQSQueue

我是否能够创建附加到创建的队列的另一个QueuePolicy?我怎么附上它?通过ARN?

Resources:
  SecondQueuePolicy:
    Type: 'AWS::SQS::QueuePolicy'
    Properties:
      PolicyDocument:
        Id: !Ref SQSQueuename
        Statement:
          - Sid: QueuePolicy2-SendMessage-To-Queue-From-SNS-Topic
            Effect: Allow
            Principal:
              AWS: !Ref AccountID
            Action:
              - 'sqs:*'
            Resource: 'arn:aws:sqs:eu-central-1:123456789010:${SQSQueuename}'
      Queues:
        - !Ref SQSQueue <-- how do i ref to the Queue ?
   DependsOn:
     - SQSQueue

1 个答案:

答案 0 :(得分:1)

在第一个模板中,确保导出队列URL和名称:

Outputs: 
  QueueURL: 
    Value: !Ref SQSQueue
    Export:
      Name: ExampleStack-QueueURL
  QueueName: 
    Value: !GetAtt SQSQueue.QueueName
    Export:
      Name: ExampleStack-QueueName

在第二个模板中导入新导出的值(不需要DependsOn):

  SecondQueuePolicy:
    Type: 'AWS::SQS::QueuePolicy'
    Properties:
      PolicyDocument:
        Id: !Ref SQSQueuename
        Statement:
          - Sid: QueuePolicy2-SendMessage-To-Queue-From-SNS-Topic
            Effect: Allow
            Principal:
              AWS: !Ref AccountID
            Action:
              - 'sqs:*'
            Resource:
              Fn::Sub:
                - 'arn:aws:sqs:eu-central-1:123456789010:${QueueName}'
                - QueueName:
                    Fn::ImportValue: ExampleStack-QueueName
      Queues:
        - Fn::ImportValue: ExampleStack-QueueURL