{
  "Description": "Creates IAM role & read-only policy for CloudAvocado app.",
  "Parameters": {
    "AccountId": {
      "Type": "String",
      "Description": "Do not modify this value, as it serves the purpose of establishing trust between your account and CloudAvocado."
    },
    "ExternalId": {
      "Type": "String",
      "Description": "A string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@:/-",
      "MinLength": 2,
      "MaxLength": 500,
      "AllowedPattern": "^[\\w+=,.@:/-]*$",
      "ConstraintDescription": "A string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@:/-"
    },
    "NamePostfix": {
      "Description": "Postfix appended to resource names created using this stack.",
      "Type": "String"
    }
  },
  "Resources": {
    "CloudAvocadoPolicy": {
      "Type": "AWS::IAM::ManagedPolicy",
      "Properties": {
        "ManagedPolicyName": {
          "Fn::Join": [
            "-",
            [
              "CloudAvocadoPolicy",
              {
                "Ref": "NamePostfix"
              }
            ]
          ]
        },
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "organizations:DescribeOrganization",
                "iam:SimulatePrincipalPolicy",
                "sts:GetCallerIdentity",
                "ce:GetDimensionValues",
                "ce:GetCostAndUsage",
                "ce:GetRightsizingRecommendation",
                "compute-optimizer:GetEC2InstanceRecommendations",
                "compute-optimizer:GetEBSVolumeRecommendations",
                "compute-optimizer:GetAutoScalingGroupRecommendations",
                "compute-optimizer:GetECSServiceRecommendations",
                "cloudWatch:ListMetrics",
                "cloudWatch:GetMetricStatistics",
                "cloudTrail:LookupEvents",
                "ec2:DescribeRegions",
                "ec2:DescribeInstances",
                "ec2:DescribeImages",
                "ec2:DescribeVolumes",
                "ec2:DescribeAddresses",
                "ec2:DescribeLaunchTemplateVersions",
                "ec2:DescribeReservedInstances",
                "ec2:DescribeReservedInstancesOfferings",
                "rds:DescribeDBInstances",
                "rds:DescribeDBClusters",
                "rds:ListTagsForResource",
                "rds:DescribeReservedDBInstances",
                "rds:DescribeReservedDBInstancesOfferings",
                "ecs:ListClusters",
                "ecs:DescribeClusters",
                "ecs:ListServices",
                "ecs:DescribeServices",
                "ecs:DescribeTaskDefinition",
                "ecs:DescribeCapacityProviders",
                "autoscaling:DescribeAutoScalingGroups",
                "autoscaling:DescribeAutoScalingInstances",
                "autoscaling:DescribeLaunchConfigurations",
                "elasticloadbalancing:DescribeLoadBalancers",
                "elasticloadbalancing:DescribeTags",
                "eks:listClusters",
                "eks:describeCluster",
                "eks:listNodegroups",
                "eks:describeNodegroup",
                "sageMaker:ListTags",
                "sageMaker:ListNotebookInstances",
                "sageMaker:DescribeNotebookInstance",
                "sageMaker:ListEndpoints",
                "sageMaker:DescribeEndpoint",
                "sageMaker:DescribeEndpointConfig"
              ],
              "Resource": "*"
            }
          ]
        }
      }
    },
    "CloudAvocadoRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": {
          "Fn::Join": [
            "-",
            [
              "CloudAvocadoRole",
              {
                "Ref": "NamePostfix"
              }
            ]
          ]
        },
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "AWS": {
                  "Fn::Join": [
                    "",
                    [
                      "arn:aws:iam::",
                      {
                        "Ref": "AccountId"
                      },
                      ":root"
                    ]
                  ]
                }
              },
              "Action": "sts:AssumeRole",
              "Condition": {
                "StringEquals": {
                  "sts:ExternalId": {
                    "Ref": "ExternalId"
                  }
                }
              }
            }
          ]
        },
        "ManagedPolicyArns": [
          {
            "Ref": "CloudAvocadoPolicy"
          }
        ]
      }
    }
  },
  "Outputs": {
    "RoleArn": {
      "Description": "Role ARN",
      "Value": {
        "Fn::GetAtt": [
          "CloudAvocadoRole",
          "Arn"
        ]
      }
    },
    "ExternalId": {
      "Description": "External ID",
      "Value": {
        "Ref": "ExternalId"
      }
    }
  }
}