2016-05-10 60 views
3

這是我的問題,除了是AWS的新手。我已被賦予複製我們的生產基地的任務,該基地位於US-East-1到US-West-2的DR站點。我遇到了創建SNS警報的問題。以下代碼來自AWS示例並使用我們的JSON導出中的策略。 Whenenver包括我到我的主要PS腳本,我得到了以下錯誤:爲什麼Set-SQSQueueAttribute失敗,出現'InvalidOperationException:參數Policy'錯誤的值無效?

錯誤:

Set-SQSQueueAttribute : Invalid value for the parameter Policy. At line: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 } 
+0

什麼榜樣?鏈接? –

+0

我沒有使用AWS的經驗,但是如果該JSON策略是從某處導出的,那麼在使用它之前應該以某種方式導入它 - 例如,使用'ConvertFrom-JSON'使其成爲PowerShell數據結構? – TessellatingHeckler

回答

4

我只是跑通過PowerShell中給出的例子使用「Get-Help Set-SQSQueueAttribute -Detailed」,並且它沒有問題。

基於PowerShell示例工作,以及您收到的具體錯誤,它會表明您所傳遞的具體策略存在某些不妥之處。我會愚蠢的政策,直到它的工作,然後不斷增加東西,直到它斷裂,找出它不喜歡什麼。

此外: 使用Set-SQSQueueAttribute方法只接受7個動作參數MAX,它不接受任何你在你的代碼中提到的那些的。有效的措施是:

  • 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 } 
相關問題