为什么 Set-SQSQueueAttribute 失败并出现“InvalidOperationException:参数策略值无效”错误?

Don*_*Don 8 powershell amazon-s3 amazon-web-services aws-powershell

这是我的问题,除了对 AWS 不熟悉之外。我接到的任务是将位于 US-East-1 到 US-West-2 的生产站点复制为灾难恢复站点。我在创建 SNS 警报时遇到问题。以下代码来自 AWS 示例,并使用我们的 JSON 导出中的策略。当我将其包含到我的主 PS 脚本中时,出现以下错误:

错误:

Set-SQSQueueAttribute:参数策略值无效。在行:37 字符:5 + Set-SQSQueueAttribute -QueueUrl $qURL -Attribute @{ Policy=$SNSpo ... + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息:无效操作:(亚马逊。 PowerShe...AttributeCmdlet:SetSQSQ ueueAttributeCmdlet) [Set-SQSQueueAttribute]、InvalidOperationException + ExcellentQualifiedErrorId :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 }
Run Code Online (Sandbox Code Playgroud)

Jim*_* P. 4

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

根据运行的 PowerShell 示例以及您收到的具体错误,这表明您传递的具体策略存在问题。我会简化你的策略直到它起作用,然后不断地增加东西直到它崩溃以找出它不喜欢的东西。

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

  • 发信息
  • 接收消息
  • 删除留言
  • 更改消息可见性
  • 获取队列属性
  • 获取队列地址

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

工作示例代码:

    "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "$topicarn"
          }
      }
Run Code Online (Sandbox Code Playgroud)

你的代码:

       "Condition": {
            "StringEquals": {
                 "AWS:SourceOwner": $topicARN
            }
       }
Run Code Online (Sandbox Code Playgroud)

对我有用的例子:

$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 }
Run Code Online (Sandbox Code Playgroud)