AWSTemplateFormatVersion: '2010-09-09'
Description: One private Fargate task serving the same echo binary as the EKS pod.
Parameters:
  Cluster: {Type: String}
  Image: {Type: String}
  ExecutionRole: {Type: String}
  BackendSG: {Type: 'AWS::EC2::SecurityGroup::Id'}
  PrivateA: {Type: 'AWS::EC2::Subnet::Id'}
  PrivateB: {Type: 'AWS::EC2::Subnet::Id'}
Resources:
  Task:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Cpu: '256'
      Memory: '512'
      NetworkMode: awsvpc
      RequiresCompatibilities: [FARGATE]
      ExecutionRoleArn: !Ref ExecutionRole
      ContainerDefinitions:
        - Name: echo
          Image: !Ref Image
          Essential: true
          Environment: [{Name: BACKEND, Value: ecs}]
          PortMappings: [{ContainerPort: 8080}]
  Service:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !Ref Cluster
      LaunchType: FARGATE
      TaskDefinition: !Ref Task
      DesiredCount: 1
      DeploymentConfiguration:
        DeploymentCircuitBreaker: {Enable: true, Rollback: true}
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: DISABLED
          SecurityGroups: [!Ref BackendSG]
          Subnets: [!Ref PrivateA, !Ref PrivateB]
Outputs:
  Service: {Value: !GetAtt Service.Name}
