AWSTemplateFormatVersion: "2010-09-09"
Description: >-
  Fargate Spot to On-Demand Fallback for Amazon ECS. Creates a role our service can assume with your External ID.
  It can list ECS clusters, services and tasks and read their status and tags. If AllowFallback is true it can also call
  ecs:UpdateService, but only on services you tagged fallback-managed=true. It cannot read task definitions,
  environment variables, secrets or logs, cannot run or stop tasks, and cannot change tags.

Parameters:
  ExternalId:
    Type: String
    MinLength: 8
    Description: External ID shown on your Connect page.
  TrustedAccountId:
    Type: String
    AllowedPattern: "[0-9]{12}"
    Default: "600627351450"
    Description: AWS account of the fallback service (pre-filled).
  AllowFallback:
    Type: String
    AllowedValues: ["true", "false"]
    Default: "true"
    Description: >-
      true = may update ECS services tagged fallback-managed=true. false = alert-only: the role is read-only and
      nothing in your account can be changed.

Conditions:
  CanFallback: !Equals [!Ref AllowFallback, "true"]

Resources:
  SpotFallbackRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: PuzariCloud-SpotFallback
      Description: Fargate Spot to On-Demand Fallback - read ECS service status; update only services tagged fallback-managed=true
      MaxSessionDuration: 3600
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal: {AWS: !Sub "arn:${AWS::Partition}:iam::${TrustedAccountId}:root"}
            Action: sts:AssumeRole
            Condition: {StringEquals: {"sts:ExternalId": !Ref ExternalId}}
      Policies:
        - PolicyName: spot-fallback-read
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Sid: ReadServiceStatus
                Effect: Allow
                Action:
                  - ecs:ListClusters
                  - ecs:ListServices
                  - ecs:DescribeServices
                  - ecs:ListTasks
                  - ecs:DescribeTasks
                  - ecs:ListTagsForResource
                Resource: "*"
        - !If
          - CanFallback
          - PolicyName: spot-fallback-update-tagged-services-only
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Sid: UpdateTaggedServicesOnly
                  Effect: Allow
                  Action: ecs:UpdateService
                  Resource: !Sub "arn:${AWS::Partition}:ecs:*:${AWS::AccountId}:service/*"
                  Condition:
                    StringEquals:
                      "aws:ResourceTag/fallback-managed": "true"
          - !Ref AWS::NoValue

Outputs:
  RoleArn:
    Description: Paste this into the Connect page.
    Value: !GetAtt SpotFallbackRole.Arn
  Mode:
    Description: What this role may do.
    Value: !If [CanFallback, "fallback (may update services tagged fallback-managed=true)", "alert-only (read-only)"]
