Search code examples
amazon-web-servicesaws-cloudformationaws-event-bridge

Amazon EventBridge Policies for AWS Services as targets using CF/SAM


I'm using AWS CloudFormation to setup an EventBridge Bus + Rules + Targets (say SNS). For SNS as a target, per the doc at https://docs.aws.amazon.com/eventbridge/latest/userguide/resource-based-policies-eventbridge.html#sns-permissions, I need to apply resource policies outside of CloudFormation and I don't think CF supports this yet? For CW Logs Group as a target, Im using the aws logs put-resource-policy to set this up in a script. Is there a better way to automate this?


Solution

  • Here is a snippet from my SAM:

    {
      "MyDevQueue": {
        "Properties": {
          "QueueName": "my-dev-queue",
          "ReceiveMessageWaitTimeSeconds": 20,
          "Tags": [
            {
              "Key": "env",
              "Value": "dev"
            }
          ],
          "VisibilityTimeout": 300
        },
        "Type": "AWS::SQS::Queue"
      },
      "MyDevQueuePolicy": {
        "Properties": {
          "PolicyDocument": {
            "Statement": [
              {
                "Action": [
                  "SQS:SendMessage"
                ],
                "Condition": {
                  "ArnEquals": {
                    "aws:SourceArn": "arn:aws:events:<region>:<AccountID>:rule/my-dev-queue/my-dev-queue"
                  }
                },
                "Effect": "Allow",
                "Principal": {
                  "Service": [
                    "events.amazonaws.com"
                  ]
                },
                "Resource": [
                  {
                    "Fn::GetAtt": [
                      "MyDevQueue",
                      "Arn"
                    ]
                  }
                ]
              }
            ]
          },
          "Queues": [
            "MyDevQueue"
          ]
        },
        "Type": "AWS::SQS::QueuePolicy"
      }
    }