# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template that represents a backend service on Amazon ECS.
Metadata:
  Version: {{ .Version }}
{{- if .SerializedManifest }}
  Manifest: |
{{indent 4 .SerializedManifest}}
{{- end }}
Parameters:
  AppName:
    Type: String
  EnvName:
    Type: String
  WorkloadName:
    Type: String
  ContainerImage:
    Type: String
  ContainerPort:
    Type: Number
  TaskCPU:
    Type: String
  TaskMemory:
    Type: String
  TaskCount:
    Type: Number
  AddonsTemplateURL:
    Description: 'URL of the addons nested stack template within the S3 bucket.'
    Type: String
    Default: ""
  EnvFileARN:
    Description: 'URL of the environment file.'
    Type: String
    Default: ""
{{- if .LogConfig}}
  LoggingEnvFileARN:
    Type: String
    Description: 'URL of the environment file for the logging sidecar.'
    Default: ""
{{- end}}
{{- range $sidecar := .Sidecars }}
  EnvFileARNFor{{ logicalIDSafe $sidecar.Name}}:
    Type: String
    Description: 'URL of the environment file for the {{$sidecar.Name}} sidecar.'
    Default: ""
{{- end }}
  LogRetention:
    Type: Number
    Default: 30
  TargetContainer:
    Type: String
  TargetPort:
    Type: Number
  {{- if .ALBListener}}
  HTTPSEnabled:
    Type: String
    AllowedValues: [true, false]
  RulePath:
    Type: String
  {{- end}}
Conditions:
  IsGovCloud:
    !Equals [!Ref "AWS::Partition", "aws-us-gov"]
  HasAddons:
    !Not [!Equals [!Ref AddonsTemplateURL, ""]]
  HasEnvFile:
    !Not [!Equals [!Ref EnvFileARN, ""]]
{{- if .LogConfig}}
  HasLoggingEnvFile:
    !Not [!Equals [!Ref LoggingEnvFileARN, ""]]
{{- end}}
{{- range $sidecar := .Sidecars }}
  HasEnvFileFor{{ logicalIDSafe $sidecar.Name}}:
    !Not [!Equals [!Ref EnvFileARNFor{{ logicalIDSafe $sidecar.Name}}, ""]]
{{- end }}
  ExposePort:
    !Not [!Equals [!Ref TargetPort, -1]]
Resources:
{{include "loggroup" . | indent 2}}

  TaskDefinition:
    Metadata:
      'aws:copilot:description': 'An ECS task definition to group your containers and run them on ECS'
    Type: AWS::ECS::TaskDefinition
    DependsOn: LogGroup
    Properties:
{{include "fargate-taskdef-base-properties" . | indent 6}}
      ContainerDefinitions:
{{include "workload-container" . | indent 8}}
{{include "sidecars" . | indent 8}}
{{- if .Storage -}}
{{include "volumes" . | indent 6}}
{{- end}}
{{include "executionrole" . | indent 2}}
{{include "taskrole" . | indent 2}}
{{include "servicediscovery" . | indent 2}}

{{- if .Autoscaling}}
{{include "autoscaling" . | indent 2}}
{{- end}}

{{- if .ALBListener}}
{{include "alb" . | indent 2}}
{{end}}
{{include "rollback-alarms" . | indent 2}}

  Service:
    Metadata:
      'aws:copilot:description': 'An ECS service to run and maintain your tasks in the environment cluster'
    Type: AWS::ECS::Service
    DependsOn:
      - EnvControllerAction
      {{- if .ALBListener }}
      {{- range $i, $rule := .ALBListener.Rules }}
      {{- if $.ALBListener.IsHTTPS}}
      - HTTPListenerRuleWithDomain{{ if ne $i 0 }}{{ $i }}{{ end }}
      - HTTPSListenerRule{{ if ne $i 0 }}{{ $i }}{{ end }}
      {{- else }}
      - HTTPListenerRule{{ if ne $i 0 }}{{ $i }}{{ end }}
      {{- end}}
      {{- end }}
      {{- end }}
    Properties:
      {{- "\n"}}{{ include "service-base-properties" . | indent 6 }}
      ServiceRegistries: !If [ExposePort, [{RegistryArn: !GetAtt DiscoveryService.Arn, Port: !Ref TargetPort}], !Ref "AWS::NoValue"]
      {{- if .ALBListener}}
      {{- if .GracePeriod }}
      HealthCheckGracePeriodSeconds: {{.GracePeriod}}
      {{- end }}
      LoadBalancers:
        {{- range $i, $rule := .ALBListener.Rules}}
        - ContainerName: {{$rule.TargetContainer}}
          ContainerPort: {{$rule.TargetPort}}
          TargetGroupArn: !Ref TargetGroup{{ if ne $i 0 }}{{ $i }}{{ end }}
        {{- end}}
      {{- end }}
{{include "efs-access-point" . | indent 2}}

{{include "addons" . | indent 2}}

{{include "publish" . | indent 2}}

{{include "env-controller" . | indent 2}}

Outputs:
  DiscoveryServiceARN:
    Description: ARN of the Discovery Service.
    Value: !GetAtt DiscoveryService.Arn
    Export:
      Name: !Sub ${AWS::StackName}-DiscoveryServiceARN