{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Metadata": {
        "AWS::CloudFormation::Designer": {
            "e4c172cd-faef-4b7c-8a2e-b23ac5502681": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": -100,
                    "y": 10
                },
                "z": 0,
                "embeds": []
            },
            "9b4f5524-6385-4f48-9ea1-149b85259796": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": 110,
                    "y": 50
                },
                "z": 0,
                "embeds": [],
                "dependson": [
                    "9c12c2c7-112f-4cc5-9fe7-160a3c237bad",
                    "0a5ebe0c-b493-4bcf-b1ad-1c2eaa6813da"
                ]
            },
            "9c12c2c7-112f-4cc5-9fe7-160a3c237bad": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": 220,
                    "y": 50
                },
                "z": 0,
                "embeds": []
            },
            "0a5ebe0c-b493-4bcf-b1ad-1c2eaa6813da": {
                "size": {
                    "width": 60,
                    "height": 60
                },
                "position": {
                    "x": 350,
                    "y": 90
                },
                "z": 0,
                "embeds": [],
                "dependson": [
                    "9c12c2c7-112f-4cc5-9fe7-160a3c237bad"
                ]
            }
        }
    },
    "Resources": {
        "ForwardingRule": {
            "Type": "AWS::Events::Rule",
            "Properties": {
                "Description": "A rule to forward events to OpsCompass",
                "EventPattern": {
                    "account": [
                        {
                            "Ref": "AWS::AccountId"
                        }
                    ],
                    "source": [
                        "aws.events",
                        "aws.codecommit",
                        "aws.codedeploy",
                        "aws.codepipeline",
                        "aws.apigateway",
                        "aws.codestar",
                        "aws.autoscaling",
                        "aws.batch",
                        "aws.acm",
                        "aws.chime",
                        "aws.clouddirectory",
                        "aws.cloudformation",
                        "aws.cloudfront",
                        "aws.cloudhsm",
                        "aws.cloudsearch",
                        "aws.cloudtrail",
                        "aws.events",
                        "aws.logs",
                        "aws.codebuild",
                        "aws.cognito-identity",
                        "aws.cofgnito-sync",
                        "aws.cognito-idp",
                        "aws.config",
                        "aws.dlm",
                        "aws.datapipeline",
                        "aws.datasync",
                        "aws.dms",
                        "aws.directoconnect",
                        "aws.ds",
                        "aws.dynamodb",
                        "aws.ec2",
                        "aws.ssm",
                        "aws.emr",
                        "aws.elasticache",
                        "aws.elasticbeanstalk",
                        "aws.ecr",
                        "aws.ecs",
                        "aws.elasticfilesystem",
                        "aws.elasticloadbalancing",
                        "aws.elasticmapreduce",
                        "aws.elastictranscoder",
                        "aws.es",
                        "aws.mediapackage",
                        "aws.gamelift",
                        "aws.glacier",
                        "aws.glue",
                        "aws.guardduty",
                        "aws.health",
                        "aws.iam",
                        "aws.inspector",
                        "aws.iot",
                        "aws.iotanalytics",
                        "aws.kms",
                        "aws.kinesis",
                        "aws.firehose",
                        "aws.lambda",
                        "aws.machinelearning",
                        "aws.macie",
                        "aws.managedblockchain",
                        "aws.managedservices",
                        "aws.mediaconvert",
                        "aws.medialive",
                        "aws.mediastore",
                        "aws.metering-marketplace",
                        "aws.monitoring",
                        "aws.opsworks",
                        "aws.opsworks-cm",
                        "aws.organizations",
                        "aws.polly",
                        "aws.redshift",
                        "aws.rds",
                        "aws.ram",
                        "aws.route53",
                        "aws.sagemaker",
                        "aws.secretsmanager",
                        "aws.securityhub",
                        "aws.sts",
                        "aws.sms",
                        "aws.servicecatalog",
                        "aws.signer",
                        "aws.ses",
                        "aws.sns",
                        "aws.sqs",
                        "aws.s3",
                        "aws.swf",
                        "aws.states",
                        "aws.storagegateway",
                        "aws.support",
                        "aws.tags",
                        "aws.transcribe",
                        "aws.trustedadvisor",
                        "aws.waf-regional",
                        "aws.waf",
                        "aws.workdocs",
                        "aws.workspaces"
                    ]
                },
                "Targets": [
                    {
                        "Arn": { 
                            "Fn::Sub": [
                                "arn:aws:events:us-east-1:213916962078:event-bus/${account}-bus",
                                {
                                    "account": {
                                        "Ref": "AWS::AccountId"
                                    }
                                }
                            ]
                        },
                        "RoleArn": {
                            "Fn::GetAtt": [
                                "OpsCompassEventBridgeRole",
                                "Arn"
                            ]
                        },
                        "Id": "opscompass-event-bus"
                    }
                ],
                "Name": "OpsCompass-Forwarding-Rule",
                "State": "ENABLED"
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "e4c172cd-faef-4b7c-8a2e-b23ac5502681"
                }
            }
        },
        "OpsCompassEventBridgeRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": {
                                    "Fn::Sub": "events.amazonaws.com"
                                }
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "Path": "/",
                "Policies": [
                    {
                        "PolicyName": "PutEventsDestinationBus",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "events:PutEvents"
                                    ],
                                    "Resource": {
                                        "Fn::Sub": [
                                            "arn:aws:events:us-east-1:213916962078:event-bus/${account}-bus",
                                            {
                                                "account": {
                                                    "Ref": "AWS::AccountId"
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "60c469b2-a267-4256-93af-67d007862c5b"
                }
            }
        },
        "LoggingTrail": {
            "Type": "AWS::CloudTrail::Trail",
            "Properties": {
                "IncludeGlobalServiceEvents": true,
                "IsMultiRegionTrail": true,
                "TrailName": "OpsCompass-EventCapture-Trail",
                "IsLogging": true,
                "S3BucketName": {
                    "Fn::Join": [
                        "",
                        [
                            "opscompass-event-logging-",
                            {
                                "Ref": "AWS::AccountId"
                            }
                        ]
                    ]
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "9b4f5524-6385-4f48-9ea1-149b85259796"
                }
            },
            "DependsOn": [
                "LoggingBucket",
                "CloudTrailEventPolicy"
            ]
        },
        "LoggingBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "BucketName": {
                    "Fn::Join": [
                        "",
                        [
                            "opscompass-event-logging-",
                            {
                                "Ref": "AWS::AccountId"
                            }
                        ]
                    ]
                },
                "AccessControl": "Private"
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "9c12c2c7-112f-4cc5-9fe7-160a3c237bad"
                }
            }
        },
        "CloudTrailEventPolicy": {
            "Type": "AWS::S3::BucketPolicy",
            "Properties": {
                "Bucket": {
                    "Fn::Join": [
                        "",
                        [
                            "opscompass-event-logging-",
                            {
                                "Ref": "AWS::AccountId"
                            }
                        ]
                    ]
                },
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Sid": "AWSCloudTrailAclCheck20150319",
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "cloudtrail.amazonaws.com"
                            },
                            "Action": "s3:GetBucketAcl",
                            "Resource": {
                                "Fn::Join": [
                                    "",
                                    [
                                        "arn:aws:s3:::",
                                        "opscompass-event-logging-",
                                        {
                                            "Ref": "AWS::AccountId"
                                        }
                                    ]
                                ]
                            }
                        },
                        {
                            "Sid": "AWSCloudTrailWrite20150319",
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "cloudtrail.amazonaws.com"
                            },
                            "Action": "s3:PutObject",
                            "Resource": {
                                "Fn::Join": [
                                    "",
                                    [
                                        "arn:aws:s3:::",
                                        "opscompass-event-logging-",
                                        {
                                            "Ref": "AWS::AccountId"
                                        },
                                        "/*/*"
                                    ]
                                ]
                            },
                            "Condition": {
                                "StringEquals": {
                                    "s3:x-amz-acl": "bucket-owner-full-control"
                                }
                            }
                        }
                    ]
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "0a5ebe0c-b493-4bcf-b1ad-1c2eaa6813da"
                }
            },
            "DependsOn": [
                "LoggingBucket"
            ]
        }
    }
}