Search code examples
amazon-web-servicesamazon-iamamazon-sqsaws-cli

Adding SQS Permissions with conditions using AWS CLI Command


How can I add the below listed SQS permission using AWS CLI command?

    "Statement": [
    {
      "Sid": "Sid8390000202",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "SQS:*",
      "Resource": "arn:aws:sqs:us-east-1:12345678:example-queue",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "arn:aws:sns:us-east-1:73628827939:MySNS"
        }
      }
    }
  ]

Solution

  • You can save the file locally as set-queue-attributes.json with the following policy.

    {
      "Id": "Policy1564523767951",
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Stmt1564523766749",
          "Action": "sqs:*",
          "Effect": "Allow",
          "Resource": "arn:aws:sqs:us-east-1:12345678:example-queue",
          "Condition": {
            "ArnEquals": {
              "aws:SourceArn": "arn:aws:sns:us-east-1:73628827939:MySNS"
            }
          },
          "Principal": "*"
        }
      ]
    }
    

    Then execute the following CLI command.

    aws sqs set-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/12345678/example-queue --attributes file://set-queue-attributes.json