AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Optional ScaleOps node consolidation role for one EKS cluster. Node group
  writes are limited to that cluster. This is not the default policy.

Parameters:
  roleName:
    Type: String
    Description: Name of the IAM role ScaleOps assumes.
    Default: scaleops-node-integration-least-privilege-role
  policyName:
    Type: String
    Description: Name of the inline policy on that role.
    Default: scaleops-node-integration-least-privilege-policy
  externalID:
    Type: String
    Description: External ID from Cloud Integration in the ScaleOps settings page.
    MinLength: 36
    MaxLength: 36
  clusterName:
    Type: String
    Description: EKS cluster name. Node group changes are limited to this cluster.
    AllowedPattern: "^[A-Za-z0-9][A-Za-z0-9_-]{0,99}$"
  clusterRegion:
    Type: String
    Description: Region of the EKS cluster. The role can be created from any region.
    AllowedPattern: "^[a-z]{2}(-gov)?-[a-z]+-\\d+$"

Resources:
  ScaleOpsNodeIntegrationAccessRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Ref roleName
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              AWS: arn:aws:iam::427621738444:role/ScaleOps-AssumeRoleDelegator
            Action:
              - sts:AssumeRole
              - sts:TagSession
            Condition:
              StringEquals:
                sts:ExternalId: !Ref externalID
      Policies:
        - PolicyName: !Ref policyName
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: SimulateIAM
                Effect: Allow
                Action:
                  - iam:GetRole
                  - iam:ListAttachedRolePolicies
                  - iam:SimulatePrincipalPolicy
                Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/${roleName}"
              - Sid: NodeConsolidationRead
                Effect: Allow
                Action:
                  - autoscaling:DescribeAutoScalingGroups
                  - autoscaling:DescribeLifecycleHooks
                  - ec2:DescribeInstances
                  - ec2:DescribeLaunchTemplateVersions
                Resource: '*'
              - Sid: AutoScalingWrites
                Effect: Allow
                Action:
                  - autoscaling:CreateAutoScalingGroup
                  - autoscaling:DeleteAutoScalingGroup
                  - autoscaling:TerminateInstanceInAutoScalingGroup
                  - autoscaling:UpdateAutoScalingGroup
                  - autoscaling:CreateOrUpdateTags
                  - autoscaling:SuspendProcesses
                Resource: '*'
              - Sid: LaunchInstances
                Effect: Allow
                Action:
                  - ec2:CreateLaunchTemplate
                  - ec2:DeleteLaunchTemplate
                  - ec2:RunInstances
                Resource: '*'
              - Sid: EksCluster
                Effect: Allow
                Action:
                  - eks:CreateNodegroup
                  - eks:DeleteNodegroup
                  - eks:DescribeCluster
                  - eks:DescribeNodegroup
                  - eks:ListNodegroups
                  - eks:TagResource
                  - eks:UpdateNodegroupConfig
                Resource:
                  - !Sub "arn:aws:eks:${clusterRegion}:${AWS::AccountId}:cluster/${clusterName}"
                  - !Sub "arn:aws:eks:${clusterRegion}:${AWS::AccountId}:nodegroup/${clusterName}/*"
              - Sid: Network
                Effect: Allow
                Action:
                  - ec2:DescribeNetworkInterfaces
                  - ec2:DescribeVpcEndpoints
                  - ec2:DescribeVpcs
                  - ec2:DescribeSubnets
                  - ec2:DescribeNatGateways
                  - ec2:DescribeRouteTables
                  - ec2:DescribePrefixLists
                  - ec2:GetManagedPrefixListEntries
                  - ec2:DescribeInternetGateways
                  - ec2:DescribeEgressOnlyInternetGateways
                Resource: '*'
              - Sid: PassNodeRole
                Effect: Allow
                Action:
                  - iam:PassRole
                Resource: '*'
                Condition:
                  StringEquals:
                    iam:PassedToService:
                      - eks.amazonaws.com
                      - ec2.amazonaws.com

Outputs:
  RoleArn:
    Description: Role ARN. Use this as cloudNodeIntegration.aws.roleArn.
    Value: !GetAtt ScaleOpsNodeIntegrationAccessRole.Arn
