从Cloudformation设置QueuePolicy不会在创建堆栈时生效-但是在创建后设置相同的策略会生效

时间:2019-03-14 00:18:46

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

我正在尝试从(现有)SNS主题创建队列和对其的订阅。所有资源都在同一个帐户中。我知道,为此,队列需要具有一个允许SNS SendMessage进入队列的QueuePolicy。

但是,我发现通过Cloudformation创建的QueuePolicy似乎没有受到尊重-消息未传递到队列,并且Cloudwatch从Topic报告中记录到传递失败是因为权限被拒绝。但是,如果我在创建后重新应用相同的策略,它将生效并传递邮件。

这是我首先尝试的方法:

$ cat template.yaml
---
AWSTemplateFormatVersion: "2010-09-09"

Description:
  ...

Parameters:
  TopicParameter:
    Type: String

Resources:
  Queue:
    Type: AWS::SQS::Queue

  Subscription:
    Type: AWS::SNS::Subscription
    DependsOn: QueuePolicy
    Properties:
      Endpoint:
        Fn::GetAtt:
          - "Queue"
          - "Arn"
      Protocol: "sqs"
      RawMessageDelivery: "true"
      TopicArn: !Ref TopicParameter

  QueuePolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Sid: '1'
            Effect: Allow
            Principal: "*"
            Action: "SQS:SendMessage"
            Resource: !Ref Queue
            Condition:
              ArnEquals:
                aws:SourceArn: !Ref TopicParameter
      Queues:
        - !Ref Queue

Outputs:
  QueueArn:
    Value:
      Fn::GetAtt:
        - "Queue"
        - "Arn"
    Export:
      Name: "QueueArn"
$ aws cloudformation create-stack --stack-name my-test-stack --template-body file://template.yaml --parameters ParameterKey=TopicParameter,ParameterValue=<topicArn>
{
    "StackId": "<stackId>"
}
# ...wait...
$ aws cloudformation describe-stacks --stack-name my-test-stack --query "Stacks[0] | Outputs[0] | OutputValue"
"<queueArn>"
# Do some trivial substitution to get the QueueUrl - it's *probably* possible via the CLI, but I don't think you need me to prove that I can do it
$ aws sqs get-queue-attributes --queue-url <queueUrl> --attribute-names ApproximateNumberOfMessages --query "Attributes.ApproximateNumberOfMessages"
"0"
# The above is consistently true, even if I wait and retry after several minutes. I've confirmed that messages *are* being published from the topic via other subscriptions
$ aws sqs get-queue-attributes --queue-url <queueUrl> --attribute-names Policy --query "Attributes.Policy"
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"1\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"SQS:SendMessage\",\"Resource\":\"<queueUrl>\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"<topicArn>\"}}}]}"
$ aws sqs get-queue-attributes --queue-url <queueUrl> --attribute-names Policy --query "Attributes.Policy" | | perl -pe 's/^.(.*?).$/$1/' | perl -pe 's/\\"/"/g' | python -m json.tool
{
    "Statement": [
        {
            "Action": "SQS:SendMessage",
            "Condition": {
                "ArnEquals": {
                    "aws:SourceArn": "<topicArn>"
                }
            },
            "Effect": "Allow",
            "Principal": "*",
            "Resource": "<queueUrl>",
            "Sid": "1"
        }
    ],
    "Version": "2012-10-17"
}

在这一点上,一切看起来都正确。如果我转到AWS控制台,则在队列上看到的QueuePolicy正是我所期望的-但没有消息。

但是,如果我重新申请 QueuePolicy,

$ aws sqs get-queue-attributes --queue-url <queueUrl> --attribute-names Policy --query "Attributes" > policyInFile
$ cat policyInFile
{
    "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"1\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"SQS:SendMessage\",\"Resource\":\"<queueUrl>\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"<topicArn>\"}}}]}"
}
$ aws sqs set-queue-attributes --queue-url <queueUrl> --attributes policyInFile

然后,几秒钟后,队列开始接收消息。

即使很奇怪,我也可以通过执行以下操作来重现相同的行为:

  • 设置堆栈
  • 进入控制台中的队列
  • 确认队列未收到消息
  • 在队列的策略上点击“编辑”
  • 点击“保存”(即-不更改策略中的任何内容
  • 观察接收消息的队列

如何使Cloudformation堆栈中的QueuePolicy在创建队列时生效?

1 个答案:

答案 0 :(得分:0)

问题是我应该将队列的 ARN 用于resource,而不是URL。我猜想,在为队列设置队列策略时(通过控制台或CLI,但通过Cloudformation 不是),resource字段将被覆盖到相关队列的ARN。