# devops/aws-management/organization-alarms-bus.yml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Creates a CloudTrail Organization trail

Parameters:
  DevOpsCloudWatchAlarmsBusArn:
    Type: String
  DevOpsAccountId:
    Type: String

Conditions:
  IsNotDevOpsAccount: !Not [!Equals [!Ref DevOpsAccountId, !Ref "AWS::AccountId"]]

Resources:
  EventRuleRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: events.amazonaws.com
        Version: "2012-10-17"
      Policies:
        - PolicyDocument:
            Statement:
              - Action: events:PutEvents
                Effect: Allow
                Resource: !Ref DevOpsCloudWatchAlarmsBusArn
            Version: "2012-10-17"
          PolicyName: CloudWatchAlarmsBusPolicy
      Tags:
        - { Key: prx:meta:tagging-version, Value: "2021-04-07" }
        - { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
        - { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
        - { Key: prx:ops:environment, Value: Production }
        - { Key: prx:dev:application, Value: DevOps }
  EventRule:
    Type: AWS::Events::Rule
    Condition: IsNotDevOpsAccount
    Properties:
      EventPattern:
        detail-type:
          - CloudWatch Alarm State Change
        source:
          - aws.cloudwatch
      State: ENABLED
      Targets:
        - Arn: !Ref DevOpsCloudWatchAlarmsBusArn
          Id: !Sub ${AWS::StackName}-cw-alarm-bus-
          InputPath: $
          RoleArn: !GetAtt EventRuleRole.Arn

Add CloudWatch Alarms event bus rule template
Add CloudWatch Alarms event bus rule template
