{
  "Parameters": {
    "YasuID": {
      "Description": "The Yasu customer ID to sync your account. Do not change or share this.",
      "MinLength": "1",
      "Type": "String"
    },
    "YasuHandshakeID": {
      "Description": "The Yasu external ID to authenticate your account. Do not change or share this.",
      "MinLength": "1",
      "Type": "String"
    },
    "YasuIamRole": {
      "Description": "The IAM role that Yasu will use to access your account.",
      "MinLength": "1",
      "Type": "String"
    },
    "YasuPingbackArn": {
      "Description": "The SNS topic ARN or Lambda ARN used for Yasu pingback callback.",
      "MinLength": "1",
      "Type": "String"
    },
    "TemplateVersion": {
      "Description": "Version of this CloudFormation template",
      "Type": "String",
      "Default": "v0.0.2"
    },
    "TemplateUrl": {
      "Description": "URL of the CloudFormation template",
      "Type": "String",
      "Default": ""
    }
  },

  "Resources": {
    "CrossAccountRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::Sub": "YasuCostOptimizationRole-${YasuID}"
        },
        "Description": "Role for Yasu to access cost optimization data",
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "AWS": { "Ref": "YasuIamRole" }
              },
              "Action": [ "sts:AssumeRole" ],
              "Condition": {
                "StringEquals": {
                  "sts:ExternalId": { "Ref": "YasuHandshakeID" }
                }
              }
            }
          ]
        },
        "ManagedPolicyArns": [
          "arn:aws:iam::aws:policy/job-function/ViewOnlyAccess"
        ],
        "Path": "/",
        "Policies": [
          {
            "PolicyName": {
              "Fn::Sub": "YasuCostOptimizationPolicy-${YasuID}"
            },
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Sid": "YasuBillingReadOnly",
                  "Effect": "Allow",
                  "Action": [
                    "application-autoscaling:Describe*",
                    "aws-portal:ViewBilling",
                    "aws-portal:ViewUsage",
                    "budgets:Describe*",
                    "budgets:View*",
                    "ce:Describe*",
                    "ce:Get*",
                    "ce:List*",
                    "cloudwatch:Get*",
                    "cloudwatch:List*",
                    "cloudwatch:Describe*",
                    "cloudfront:GetDistribution",
                    "cloudfront:GetDistributionConfig",
                    "cloudfront:GetInvalidation",
                    "cloudfront:ListDistributions",
                    "cloudfront:ListInvalidations",
                    "cloudfront:ListTagsForResource",
                    "compute-optimizer:Describe*",
                    "compute-optimizer:Get*",
                    "cost-optimization-hub:Get*",
                    "cost-optimization-hub:List*",
                    "cur:Describe*",
                    "directconnect:Describe*",
                    "ec2:Describe*",
                    "ec2:List*",
                    "ecr:Describe*",
                    "ecr:List*",
                    "eks:Describe*",
                    "eks:List*",
                    "elasticache:List*",
                    "elasticfilesystem:Describe*",
                    "elasticloadbalancing:Describe*",
                    "es:Describe*",
                    "es:List*",
                    "glacier:Describe*",
                    "kafka:Describe*",
                    "kafka:List*",
                    "lambda:Get*",
                    "lambda:List*",
                    "license-manager:List*",
                    "mediaconnect:Describe*",
                    "mediaconnect:List*",
                    "mediaconvert:List*",
                    "medialive:Describe*",
                    "medialive:List*",
                    "mediapackage:Describe*",
                    "mediapackage:List*",
                    "mediapackage-vod:Describe*",
                    "mediapackage-vod:List*",
                    "mediastore:Describe*",
                    "mediastore:List*",
                    "mediatailor:Describe*",
                    "mediatailor:List*",
                    "mq:Describe*",
                    "mq:List*",
                    "organizations:Describe*",
                    "organizations:List*",
                    "pi:Describe*",
                    "pi:Get*",
                    "pi:List*",
                    "pricing:Get*",
                    "pricing:Describe*",
                    "pricing:List*",
                    "rds:List*",
                    "redshift:Describe*",
                    "redshift:List*",
                    "resource-groups:Get*",
                    "resource-groups:List*",
                    "resource-groups:Search*",
                    "route53:Get*",
                    "route53:List*",
                    "route53domains:Check*",
                    "route53domains:Get*",
                    "route53domains:View*",
                    "sagemaker:Describe*",
                    "sagemaker:List*",
                    "s3:GetBucketLocation",
                    "s3:GetBucketTagging",
                    "s3:List*",
                    "savingsplans:Describe*",
                    "secretsmanager:Describe*",
                    "secretsmanager:List*",
                    "ses:Describe*",
                    "shield:Describe*",
                    "sqs:List*",
                    "ssm:Describe*",
                    "ssm:List*",
                    "storagegateway:Describe*",
                    "storagegateway:List*",
                    "support:DescribeTrustedAdvisorChecks",
                    "support:DescribeTrustedAdvisorCheckResult",
                    "support:DescribeTrustedAdvisorCheckSummaries",
                    "support:DescribeTrustedAdvisorCheckRefreshStatuses",
                    "tag:Get*",
                    "tag:GetResources",
                    "transfer:Describe*",
                    "transfer:List*",
                    "trustedadvisor:Get*",
                    "trustedadvisor:List*",
                    "rds:Describe*",
                    "s3:GetReplicationConfiguration",
                    "backup:ListBackupPlans",
                    "backup:GetBackupPlan",
                    "backup:ListBackupSelections",
                    "elasticache:Describe*"
                  ],
                  "Resource": "*"
                },
                {
                  "Sid": "YasuDenyDataPlaneReads",
                  "Effect": "Deny",
                  "Action": [
                    "secretsmanager:GetSecretValue",
                    "kms:Decrypt",
                    "ssm:GetParameter*",
                    "dynamodb:GetItem",
                    "dynamodb:Query",
                    "dynamodb:Scan",
                    "s3:GetObject"
                  ],
                  "Resource": "*"
                }
              ]
            }
          },
          {
            "PolicyName": {
              "Fn::Sub": "YasuCloudWatchMetricsReadOnly-${YasuID}"
            },
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Sid": "YasuContainerInsightsReadOnly",
                  "Effect": "Allow",
                  "Action": [
                    "logs:List*",
                    "logs:Describe*",
                    "logs:StartQuery",
                    "logs:StopQuery",
                    "logs:Filter*",
                    "logs:Get*"
                  ],
                  "Resource": "arn:aws:logs:*:*:log-group:/aws/containerinsights/*"
                },
                {
                  "Sid": "YasuContainerInsightsLogStream",
                  "Effect": "Allow",
                  "Action": [
                    "logs:GetQueryResults",
                    "logs:DescribeLogGroups"
                  ],
                  "Resource": "arn:aws:logs:*:*:log-group::log-stream:*"
                },
                {
                  "Sid": "YasuContainerMetricsAccess",
                  "Effect": "Allow",
                  "Action": [
                    "autoscaling:Describe*",
                    "cloudwatch:Describe*",
                    "cloudwatch:Get*",
                    "cloudwatch:List*"
                  ],
                  "Resource": "*"
                }
              ]
            }
          }
        ]
      }
    },

    "YasuPingResource": {
      "Type": "Custom::YasuPingResource",
      "DeletionPolicy": "Retain",
      "Version": "1.0",
      "Properties": {
        "ServiceToken": { "Ref": "YasuPingbackArn" },
        "RoleArn": { "Fn::GetAtt": ["CrossAccountRole", "Arn"] },
        "YasuID": { "Ref": "YasuID" },
        "TemplateVersion": { "Ref": "TemplateVersion" },
        "TemplateUrl": { "Ref": "TemplateUrl" }
      }
    }
  },

  "Outputs": {
    "RoleArn": {
      "Value": { "Fn::GetAtt": ["CrossAccountRole", "Arn"] },
      "Description": "The ARN value of the Cross-Account Role with IAM read-only permissions. Add this ARN value to Yasu."
    },
    "TemplateVersion": {
      "Value": { "Ref": "TemplateVersion" },
      "Description": "Version of the deployed template"
    },
    "TemplateUrl": {
      "Value": { "Ref": "TemplateUrl" },
      "Description": "URL of the deployed template"
    }
  }
}
