AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS Cost Analyzer & DevOps Decision Platform - Read-Only Cross-Account IAM Role'

Parameters:
  PlatformAccountId:
    Type: String
    Description: 'The 12-digit AWS Account ID of the AWS Cost Analyzer platform.'
    AllowedPattern: '^\d{12}$'
    ConstraintDescription: 'Must be a valid 12-digit AWS Account ID.'

  ExternalId:
    Type: String
    Description: 'Unique External ID provided in your AWS Cost Analyzer onboarding wizard.'
    MinLength: 16
    MaxLength: 128
    ConstraintDescription: 'Must match the External ID assigned to your tenant.'

  RoleName:
    Type: String
    Default: 'AwsCostAnalyzerCrossAccountRole'
    Description: 'The name of the IAM Role created in your account.'

Resources:
  CostAnalyzerRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Ref RoleName
      Description: 'Read-only access for AWS Cost Analyzer & DevOps Decision Platform (No write/modify/delete access)'
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              AWS: !Sub 'arn:aws:iam::${PlatformAccountId}:root'
            Action: 'sts:AssumeRole'
            Condition:
              StringEquals:
                'sts:ExternalId': !Ref ExternalId
      Policies:
        - PolicyName: CostAnalyzerPhase1ReadOnlyPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: CostExplorerAccess
                Effect: Allow
                Action:
                  - 'ce:GetCostAndUsage'
                  - 'ce:GetCostForecast'
                  - 'ce:GetDimensionValues'
                  - 'ce:GetAnomalies'
                Resource: '*'
              - Sid: BudgetsAccess
                Effect: Allow
                Action:
                  - 'budgets:ViewBudget'
                  - 'budgets:DescribeBudgets'
                Resource: '*'
              - Sid: ComputeOptimizerAccess
                Effect: Allow
                Action:
                  - 'compute-optimizer:GetEC2InstanceRecommendations'
                  - 'compute-optimizer:GetEBSVolumeRecommendations'
                  - 'compute-optimizer:GetLambdaFunctionRecommendations'
                  - 'compute-optimizer:GetEnrollmentStatus'
                Resource: '*'
              - Sid: SupportAndHealthProbes
                Effect: Allow
                Action:
                  - 'support:DescribeServices'
                  - 'trustedadvisor:DescribeTrustedAdvisorChecks'
                Resource: '*'
              - Sid: QuickWinDevOpsInventory
                Effect: Allow
                Action:
                  - 'ec2:DescribeVolumes'
                  - 'ec2:DescribeInstances'
                  - 'ec2:DescribeAddresses'
                  - 'ec2:DescribeSnapshots'
                  - 'ec2:DescribeRegions'
                  - 'ec2:DescribeNatGateways'
                  - 'ec2:DescribeVpcEndpoints'
                  - 'rds:DescribeDBInstances'
                  - 'rds:DescribeDBClusters'
                  - 's3:ListAllMyBuckets'
                  - 's3:GetBucketLocation'
                  - 's3:GetLifecycleConfiguration'
                  - 's3:GetBucketVersioning'
                  - 's3:ListBucketMultipartUploads'
                  - 's3:GetBucketPublicAccessBlock'
                  - 'lambda:ListFunctions'
                  - 'dynamodb:ListTables'
                  - 'dynamodb:DescribeTable'
                  - 'elasticache:DescribeCacheClusters'
                  - 'ecs:ListClusters'
                  - 'ecs:DescribeClusters'
                  - 'ecs:ListServices'
                  - 'ecs:DescribeServices'
                  - 'ecs:DescribeTaskDefinition'
                  - 'eks:ListClusters'
                  - 'eks:DescribeCluster'
                  - 'cloudwatch:DescribeAlarms'
                  - 'cloudwatch:GetMetricData'
                  - 'cloudwatch:GetMetricStatistics'
                  - 'cloudwatch:ListMetrics'
                Resource: '*'

Outputs:
  RoleArn:
    Description: 'The Role ARN to copy and paste back into the AWS Cost Analyzer dashboard.'
    Value: !GetAtt CostAnalyzerRole.Arn
  ExternalIdUsed:
    Description: 'The External ID configured in the role trust policy.'
    Value: !Ref ExternalId
