AWSTemplateFormatVersion: 2010-09-09
Description: ScaleOps IAM Permission Stack (node consolidation, network, and EBS volume permissions)

Parameters:
  roleName:
    Type: String
    Description: Name of the new IAM role to be used by ScaleOps for Node Integration access
    Default: scaleops-node-integration-minimal-role
  policyName:
    Type: String
    Description: Name of the new policy
    Default: scaleops-node-integration-minimal-policy
  externalID:
    Type: String
    Description: The external ID is not configurable and can be found in the ScaleOps settings page.
    MinLength: 36
    MaxLength: 36

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": !Sub '${externalID}'
      Policies:
        - PolicyName: !Ref policyName
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Sid: SimulateIAM
                Effect: Allow
                Action:
                  - iam:SimulatePrincipalPolicy
                  - iam:SimulateCustomPolicy
                  - iam:GetRole
                  - iam:ListAttachedRolePolicies
                Resource:
                  - '*'
              - Sid: NodeConsolidation
                Effect: Allow
                Action:
                  - autoscaling:CreateAutoScalingGroup
                  - autoscaling:DeleteAutoScalingGroup
                  - autoscaling:DescribeAutoScalingGroups
                  - autoscaling:DescribeLifecycleHooks
                  - autoscaling:TerminateInstanceInAutoScalingGroup
                  - autoscaling:UpdateAutoScalingGroup
                  - autoscaling:CreateOrUpdateTags
                  - autoscaling:SuspendProcesses
                  - ec2:CreateLaunchTemplate
                  - ec2:DeleteLaunchTemplate
                  - ec2:DescribeInstances
                  - ec2:DescribeLaunchTemplateVersions
                  - ec2:RunInstances
                  - eks:CreateNodegroup
                  - eks:DeleteNodegroup
                  - eks:DescribeCluster
                  - eks:DescribeNodegroup
                  - eks:ListNodegroups
                  - eks:TagResource
                  - eks:UpdateNodegroupConfig
                Resource: '*'
              - 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: VolumeModification
                Effect: Allow
                Action:
                  - ec2:DescribeVolumes
                  - ec2:DescribeVolumesModifications
                  - ec2:ModifyVolume
                Resource: '*'
              - Sid: PassNodeRole
                Effect: Allow
                Action:
                  - iam:PassRole
                Resource: '*'
                Condition:
                  StringEquals:
                    iam:PassedToService:
                      - eks.amazonaws.com
                      - ec2.amazonaws.com
              - Sid: MarketplaceTagging
                Effect: Allow
                Action:
                  - ec2:CreateTags
                Resource: 'arn:aws:ec2:*:*:volume/*'
