为什么Set-SQSQueueAttribute失败并出现'InvalidOperationException:参数Policy的错误值'错误?

时间:2016-05-10 16:13:34

标签: powershell amazon-web-services amazon-s3 aws-powershell

这是我的问题,除了刚接触AWS之外。我被赋予了复制我们的生产基地的任务,该生产基地位于US-East-1到US-West-2的DR站点。我遇到了创建SNS警报的问题。以下代码来自AWS示例,并使用我们的JSON导出中的策略。当我将其包含在我的主PS脚本中时,我收到以下错误:

错误:

  

Set-SQSQueueAttribute:参数Policy的值无效。   在行:37 char:5   + Set-SQSQueueAttribute -QueueUrl $ qURL -Attribute @ {Policy = $ SNSpo ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~       + CategoryInfo:InvalidOperation :( Amazon.PowerShe ... AttributeCmdlet:SetSQSQ      ueueAttributeCmdlet)[Set-SQSQueueAttribute],InvalidOperationException       + FullyQualifiedErrorId:Amazon.SQS.AmazonSQSException,Amazon.PowerShell.Cmdlets.SQS。      SetSQSQueueAttributeCmdlet

代码:

$qURL = New-SQSQueue -QueueName "Test-Queue"
$topicARN = New-SNSTopic -Name "Test-Topic" -Region "us-west-2"

$SNSpolicy = @"
{
     "Version": "2008-10-17",
     "Id": "__default_policy_ID",
     "Statement": [
          {
           "Sid": "__default_policy_ID",
           "Effect": "Allow",
           "Principal": {
                "AWS": "*"
          },
           "Action": [
                "SNS:Subscribe",
                "SNS:ListSubscriptionsByTopic",
                "SNS:DeleteTopic",
                "SNS:GetTopicAttributes",
                "SNS:Publish",
                "SNS:RemovePermission",
                "SNS:AddPermission",
                "SNS:Receive",
                "SNS:SetTopicAttributes"
           ],
           "Resource": "arn:aws:sqs:us-west-2:123456789012:Test-Queue",
           "Condition": {
                "StringEquals": {
                     "AWS:SourceOwner": $topicARN
                }
           }
     ]
}
"@

# set the policy
Set-SQSQueueAttribute -QueueUrl $qURL -Attribute @{ Policy=$SNSpolicy }

1 个答案:

答案 0 :(得分:4)

我刚刚使用" Get-Help Set-SQSQueueAttribute -Detailed"来运行powershell给出的示例,并且它没有问题。

根据PowerShell示例的工作情况以及您收到的具体错误,它会表明您传递的具体政策存在问题。我会贬低你的政策直到它起作用,然后继续逐步添加东西,直到它破裂,找出它不喜欢的东西。

<强> 此外: Set-SQSQueueAttribute方法只接受最多7个动作参数,并且它不接受您在代码中提到的任何动作参数。有效的行动是:

  • SendMessage
  • ReceiveMessage
  • DeleteMessage可以
  • ChangeMes​​sageVisibility
  • GetQueueAttributes
  • GetQueueUrl

我注意到有一件事与你的例子不同,而下面的例子对我有用:

工作示例代码:

    "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "$topicarn"
          }
      }

您的代码:

       "Condition": {
            "StringEquals": {
                 "AWS:SourceOwner": $topicARN
            }
       }

对我有用的例子:

$qurl = New-SQSQueue -QueueName "myQueue" -Region 'us-east-1' -AccessKey 'accesskey' -SecretKey 'secretkey'
$topicarn = New-SNSTopic -Name "myTopic"

$qarn = (Get-SQSQueueAttribute -QueueUrl $qurl -AttributeName "QueueArn").QueueARN

# construct the policy and inject arns
$policy = @"
{
  "Version": "2008-10-17",
  "Id": "$qarn/SQSPOLICY",
  "Statement": [
      {
      "Sid": "1",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "SQS:SendMessage",
      "Resource": "$qarn",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "$topicarn"
          }
      }
    }
  ]
}
"@

Set-SQSQueueAttribute -QueueUrl $qurl -Attribute @{ Policy=$policy }