{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
        "TrustedAccount": {
            "Type": "String",
            "Description": "Microtica AWS account ID"
        },
        "ExternalId": {
            "Type": "String",
            "Description": "Your secret customer unique id"
        }
    },
    "Resources": {
        "MicroticaCrossAccountPolicy": {
            "Type": "AWS::IAM::ManagedPolicy",
            "Properties": {
                "Description": "Microtica Cross-Account Access Policy",
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "*",
                            "Resource": "*"
                        },
                        {
                            "Action": "ce:GetCostAndUsage",
                            "Resource": "*",
                            "Effect": "Allow"
                        },
                        {
                            "Action": "ce:GetCostForecast",
                            "Resource": "*",
                            "Effect": "Allow"
                        },
                        {
                            "Action": "pricing:*",
                            "Resource": "*",
                            "Effect": "Allow"
                        }
                    ]
                }
            }
        },
        "MicroticaCrossAccountRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "AWS": {
                                    "Fn::Sub": "arn:aws:iam::${TrustedAccount}:root"
                                }
                            },
                            "Action": "sts:AssumeRole",
                            "Condition": {
                                "StringEquals": {
                                    "sts:ExternalId": {
                                        "Ref": "ExternalId"
                                    }
                                }
                            }
                        },
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "codebuild.amazonaws.com"
                            },
                            "Action": "sts:AssumeRole"
                        },
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "lambda.amazonaws.com"
                            },
                            "Action": "sts:AssumeRole"
                        },
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "cloudformation.amazonaws.com"
                            },
                            "Action": "sts:AssumeRole"
                        }
                    ]
                },
                "ManagedPolicyArns": [
                    {
                        "Ref": "MicroticaCrossAccountPolicy"
                    }
                ]
            }
        },
        "CrossAccountHandler": {
            "Type": "Custom::CrossAccountHandler",
            "Version": "1.0",
            "Properties": {
                "ServiceToken": {
                    "Fn::Sub": "arn:aws:sns:${AWS::Region}:${TrustedAccount}:MicroticaCFNTopic"
                },
                "roleArn": {
                    "Fn::GetAtt": "MicroticaCrossAccountRole.Arn"
                },
                "accountId": {
                    "Ref": "AWS::AccountId"
                },
                "externalId": {
                    "Ref": "ExternalId"
                }
            }
        }
    },
    "Outputs": {
        "MicroticaRoleArn": {
            "Value": {
                "Fn::GetAtt": [
                    "MicroticaCrossAccountRole",
                    "Arn"
                ]
            }
        }
    }
}