dual-primary/cf-service-replica.yml [218:541]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Type: String
    Default: false
    AllowedValues: [true, false]

Mappings:
  XRay:
    Info:
      ContainerName: xray-daemon
      Port: 2000

Conditions:
  ShouldEnableXRay: !Equals [!Ref EnableXray, true]

Resources:
    GerritService:
        Type: AWS::ECS::Service
        DependsOn:
          - HTTPListener
          - SSHListener
          - GitListener
          - GitSSHListener
        Properties:
            Cluster:
              Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'ClusterName']]
            CapacityProviderStrategy:
              - CapacityProvider: !Sub '${ClusterStackName}-R'
                Weight: 100
            DesiredCount: !Ref ReplicaAutoScalingDesiredCapacity
            TaskDefinition: !Ref GerritTaskDefinition
            LoadBalancers:
                - ContainerName: !Ref GerritServiceName
                  ContainerPort: !Ref HTTPContainePort
                  TargetGroupArn: !Ref HTTPTargetGroup
                - ContainerName: !Ref GerritServiceName
                  ContainerPort: !Ref SSHContainerPort
                  TargetGroupArn: !Ref SSHTargetGroup
                - ContainerName: !Ref GitDaemonServiceName
                  ContainerPort: !Ref GitPort
                  TargetGroupArn: !Ref GitTargetGroup
                - ContainerName: !Ref GitSSHServiceName
                  ContainerPort: !Ref GitSSHContainerPort
                  TargetGroupArn: !Ref GitSSHTargetGroup

    GerritTaskDefinition:
        Type: AWS::ECS::TaskDefinition
        Properties:
            Family: !Join ['', [!Ref GerritServiceName, TaskDefinition]]
            TaskRoleArn: !GetAtt ECSTaskExecutionRoleStack.Outputs.TaskExecutionRoleRef
            ExecutionRoleArn: !GetAtt ECSTaskExecutionRoleStack.Outputs.TaskExecutionRoleRef
            NetworkMode: bridge
            PlacementConstraints:
                - Expression: !Sub 'attribute:target_group == replica'
                  Type: "memberOf"
            ContainerDefinitions:
                - Name: !Ref GerritServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GerritDockerImage}'
                  Environment:
                    - Name: XRAY_ENABLED
                      Value: !Ref EnableXray
                    - Name: AWS_XRAY_TRACING_NAME
                      Value: !Ref InstanceId
                    - Name: AWS_XRAY_DAEMON_ADDRESS
                      Value: !Join [':', [!FindInMap ['XRay', 'Info', 'ContainerName'], !FindInMap ['XRay', 'Info', 'Port']]]
                    - Name: CANONICAL_WEB_URL
                      Value: !Sub 'https://${HttpSubdomain}.${HostedZoneName}'
                    - Name: SSHD_ADVERTISED_ADDRESS
                      Value: !Sub '${SshSubdomain}.${HostedZoneName}:${SSHHostPort}'
                    - Name: HTTPD_LISTEN_URL
                      Value: !Sub 'proxy-https://*:${HTTPContainePort}/'
                    - Name: CONTAINER_REPLICA
                      Value: true
                    - Name: GERRIT_KEY_PREFIX
                      Value: !Ref GerritKeyPrefix
                    - Name: AWS_REGION
                      Value: !Ref AWS::Region
                    - Name: GERRIT_HEAP_LIMIT
                      Value: !Ref GerritHeapLimit
                    - Name: JGIT_CACHE_SIZE
                      Value: !Ref JgitCacheSize
                    - Name: JGIT_OPEN_FILES
                      Value: !Ref JgitOpenFiles
                    - Name: LDAP_SERVER
                      Value: !Ref LDAPServer
                    - Name: LDAP_USERNAME
                      Value: !Ref LDAPUsername
                    - Name: LDAP_ACCOUNT_BASE
                      Value: !Ref LDAPAccountBase
                    - Name: LDAP_GROUP_BASE
                      Value: !Ref LDAPGroupBase
                    - Name: LDAP_ACCOUNT_PATTERN
                      Value: !Ref LDAPAccountPattern
                    - Name: GERRIT_INSTANCE_ID
                      Value: !Ref InstanceId
                    - Name: METRICS_CLOUDWATCH_ENABLED
                      Value: !Ref MetricsCloudwatchEnabled
                    - Name: METRICS_CLOUDWATCH_NAMESPACE
                      Value: !Ref MetricsCloudwatchNamespace
                    - Name: METRICS_CLOUDWATCH_RATE
                      Value: !Ref MetricsCloudwatchRate
                    - Name: METRICS_CLOUDWATCH_INITIAL_DELAY
                      Value: !Ref MetricsCloudwatchInitialDelay
                    - Name: METRICS_CLOUDWATCH_JVM_ENABLED
                      Value: !Ref MetricsCloudwatchJVMEnabled
                    - Name: METRICS_CLOUDWATCH_DRY_RUN
                      Value: !Ref MetricsCloudwatchDryRun
                    - Name: METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
                      Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
                  Ulimits:
                    - Name: nofile
                      HardLimit: !Ref FileDescriptorsHardLimit
                      SoftLimit: !Ref FileDescriptorsSoftLimit
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                    - SourceVolume: !Ref GerritDataVolume
                      ContainerPath: /var/gerrit/data
                    - SourceVolume: !Ref GerritCacheVolume
                      ContainerPath: /var/gerrit/cache
                    - SourceVolume: !Ref GerritDbVolume
                      ContainerPath: /var/gerrit/db
                    - SourceVolume: !Ref GerritLogsVolume
                      ContainerPath: /var/gerrit/logs
                  Cpu: !Ref GerritCPU
                  Memory: !Ref GerritRAM
                  PortMappings:
                    - ContainerPort: !Ref HTTPContainePort
                      HostPort: !Ref HTTPHostPort
                      Protocol: tcp
                    - ContainerPort: !Ref SSHContainerPort
                      HostPort: !Ref SSHHostPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                  Links:
                    Fn::If:
                      - ShouldEnableXRay
                      -
                        - !FindInMap ['XRay', 'Info', 'ContainerName']
                      - !Ref "AWS::NoValue"
                - Name: !Ref GitDaemonServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GitDaemonDockerImage}'
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                  Cpu: 256
                  Memory: 512
                  PortMappings:
                    - ContainerPort: !Ref GitPort
                      HostPort: !Ref GitPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                - Name: !Ref GitSSHServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GitSSHDockerImage}'
                  Environment:
                    - Name: TZ
                      Value: US/Pacific
                    - Name: SSH_USERS
                      Value: gerrit:1000:1000
                    - Name: AWS_REGION
                      Value: !Ref AWS::Region
                    - Name: GERRIT_KEY_PREFIX
                      Value: !Ref GerritKeyPrefix
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                  Cpu: 256
                  Memory: 512
                  PortMappings:
                    - ContainerPort: !Ref GitSSHContainerPort
                      HostPort: !Ref GitSSHPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                - Fn::If:
                    - ShouldEnableXRay
                    - Name: !FindInMap ['XRay', 'Info', 'ContainerName']
                      Essential: false
                      Image: "amazon/aws-xray-daemon"
                      Cpu: 32
                      MemoryReservation: 256
                      PortMappings:
                        - HostPort: 2000
                          ContainerPort: 2000
                          Protocol: "udp"
                    - !Ref "AWS::NoValue"
            Volumes:
              - Name: !Ref 'GerritDbVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-db: !Join ['-', [!Ref EnvironmentName, !Ref GerritDbVolume]]
              - Name: !Ref 'GerritGitVolume'
                EFSVolumeConfiguration:
                  FilesystemId:
                    Fn::ImportValue:
                      !Join [':', [!Ref 'ClusterStackName', 'ReplicaFileSystemID']]
              - Name: !Ref 'GerritDataVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-data: !Join ['-', [!Ref EnvironmentName, !Ref GerritDataVolume]]
              - Name: !Ref 'GerritCacheVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-cache: !Join ['-', [!Ref EnvironmentName, !Ref GerritCacheVolume]]
              - Name: !Ref 'GerritLogsVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-logs: !Join ['-', [!Ref EnvironmentName, !Ref GerritLogsVolume]]

    ReplicaCPUAutoScaling:
      Type: AWS::CloudFormation::Stack
      Properties:
        TemplateURL: !Join [ '', ['https://', !Ref TemplateBucketName, '.s3.amazonaws.com/cf-ecs-service-cpu-autoscaling.yml'] ]
        TimeoutInMinutes: '5'
        Parameters:
          AutoScalingMinCapacity: !Ref ReplicaAutoScalingMinCapacity
          AutoScalingDesiredCapacity: !Ref ReplicaAutoScalingDesiredCapacity
          AutoScalingMaxCapacity: !Ref ReplicaAutoScalingMaxCapacity
          AutoScalingScaleInCooldown: !Ref ReplicaAutoScalingScaleInCooldown
          AutoScalingScaleOutCooldown: !Ref ReplicaAutoScalingScaleOutCooldown
          AutoScalingTargetCPUPercentage: !Ref ReplicaAutoScalingTargetCPUPercentage
          ResourceId:
            !Join
            - ''
            - - 'service/'
              - Fn::ImportValue: !Join [':', [!Ref 'ClusterStackName', 'ClusterName']]
              - '/'
              - !GetAtt GerritService.Name

    ReplicaHTTPLoadBalancerSG:
      Type: AWS::EC2::SecurityGroup
      Properties:
        VpcId:
          Fn::ImportValue:
            !Join [':', [!Ref 'ClusterStackName', 'VPCId']]
        GroupDescription: "Allow public HTTPS traffic to replicas"
        GroupName: !Sub '${ClusterStackName}-R-H'
        SecurityGroupIngress:
          - IpProtocol: 'tcp'
            FromPort: !Ref HTTPSPort
            ToPort: !Ref HTTPSPort
            CidrIp: '0.0.0.0/0'
            Description: "HTTPS connections from everywhere (IPv4)"
          - IpProtocol: 'tcp'
            FromPort: !Ref HTTPSPort
            ToPort: !Ref HTTPSPort
            CidrIpv6: '::/0'
            Description: "HTTPS connections from everywhere (IPv6)"

    ReplicaHTTPLoadBalancer:
      Type: AWS::ElasticLoadBalancingV2::LoadBalancer
      Properties:
        Name: !Sub '${ClusterStackName}-R-H'
        Type: application
        Scheme: !Ref 'LoadBalancerScheme'
        SecurityGroups:
          - !Ref ReplicaHTTPLoadBalancerSG
        Subnets:
          - Fn::ImportValue:
              !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetOne']]
          - Fn::ImportValue:
              !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetTwo']]
        Tags:
          - Key: Name
            Value: !Join ['-', [!Ref 'EnvironmentName', !Ref 'GerritServiceName', 'alb']]

    ReplicaSSHLoadBalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
            Name: !Sub '${ClusterStackName}-R-S'
            Type: network
            Scheme: !Ref 'LoadBalancerScheme'
            LoadBalancerAttributes:
              - Key: 'load_balancing.cross_zone.enabled'
                Value: true
            Subnets:
              - Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetOne']]
              - Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetTwo']]
            Tags:
                - Key: Name
                  Value: !Join ['-', [!Ref 'EnvironmentName', !Ref 'GerritServiceName', 'nlb']]

    HTTPTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        DependsOn: ReplicaHTTPLoadBalancer
        Properties:
            VpcId:
              Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'VPCId']]
            Port: !Ref HTTPHostPort
            Protocol: HTTP
            HealthCheckPort: !Ref HTTPHostPort
            HealthCheckProtocol: HTTP
            HealthCheckPath: '/config/server/healthcheck~status'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



primary-replica/cf-service-replica.yml [218:541]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Type: String
    Default: false
    AllowedValues: [true, false]

Mappings:
  XRay:
    Info:
      ContainerName: xray-daemon
      Port: 2000

Conditions:
  ShouldEnableXRay: !Equals [!Ref EnableXray, true]

Resources:
    GerritService:
        Type: AWS::ECS::Service
        DependsOn:
          - HTTPListener
          - SSHListener
          - GitListener
          - GitSSHListener
        Properties:
            Cluster:
              Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'ClusterName']]
            CapacityProviderStrategy:
              - CapacityProvider: !Sub '${ClusterStackName}-R'
                Weight: 100
            DesiredCount: !Ref ReplicaAutoScalingDesiredCapacity
            TaskDefinition: !Ref GerritTaskDefinition
            LoadBalancers:
                - ContainerName: !Ref GerritServiceName
                  ContainerPort: !Ref HTTPContainePort
                  TargetGroupArn: !Ref HTTPTargetGroup
                - ContainerName: !Ref GerritServiceName
                  ContainerPort: !Ref SSHContainerPort
                  TargetGroupArn: !Ref SSHTargetGroup
                - ContainerName: !Ref GitDaemonServiceName
                  ContainerPort: !Ref GitPort
                  TargetGroupArn: !Ref GitTargetGroup
                - ContainerName: !Ref GitSSHServiceName
                  ContainerPort: !Ref GitSSHContainerPort
                  TargetGroupArn: !Ref GitSSHTargetGroup

    GerritTaskDefinition:
        Type: AWS::ECS::TaskDefinition
        Properties:
            Family: !Join ['', [!Ref GerritServiceName, TaskDefinition]]
            TaskRoleArn: !GetAtt ECSTaskExecutionRoleStack.Outputs.TaskExecutionRoleRef
            ExecutionRoleArn: !GetAtt ECSTaskExecutionRoleStack.Outputs.TaskExecutionRoleRef
            NetworkMode: bridge
            PlacementConstraints:
              - Expression: !Sub 'attribute:target_group == replica'
                Type: "memberOf"
            ContainerDefinitions:
                - Name: !Ref GerritServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GerritDockerImage}'
                  Environment:
                    - Name: XRAY_ENABLED
                      Value: !Ref EnableXray
                    - Name: AWS_XRAY_TRACING_NAME
                      Value: !Ref InstanceId
                    - Name: AWS_XRAY_DAEMON_ADDRESS
                      Value: !Join [':', [!FindInMap ['XRay', 'Info', 'ContainerName'], !FindInMap ['XRay', 'Info', 'Port']]]
                    - Name: CANONICAL_WEB_URL
                      Value: !Sub 'https://${HttpSubdomain}.${HostedZoneName}'
                    - Name: SSHD_ADVERTISED_ADDRESS
                      Value: !Sub '${SshSubdomain}.${HostedZoneName}:${SSHHostPort}'
                    - Name: HTTPD_LISTEN_URL
                      Value: !Sub 'proxy-https://*:${HTTPContainePort}/'
                    - Name: CONTAINER_REPLICA
                      Value: true
                    - Name: GERRIT_KEY_PREFIX
                      Value: !Ref GerritKeyPrefix
                    - Name: AWS_REGION
                      Value: !Ref AWS::Region
                    - Name: GERRIT_HEAP_LIMIT
                      Value: !Ref GerritHeapLimit
                    - Name: JGIT_CACHE_SIZE
                      Value: !Ref JgitCacheSize
                    - Name: JGIT_OPEN_FILES
                      Value: !Ref JgitOpenFiles
                    - Name: LDAP_SERVER
                      Value: !Ref LDAPServer
                    - Name: LDAP_USERNAME
                      Value: !Ref LDAPUsername
                    - Name: LDAP_ACCOUNT_BASE
                      Value: !Ref LDAPAccountBase
                    - Name: LDAP_GROUP_BASE
                      Value: !Ref LDAPGroupBase
                    - Name: LDAP_ACCOUNT_PATTERN
                      Value: !Ref LDAPAccountPattern
                    - Name: GERRIT_INSTANCE_ID
                      Value: !Ref InstanceId
                    - Name: METRICS_CLOUDWATCH_ENABLED
                      Value: !Ref MetricsCloudwatchEnabled
                    - Name: METRICS_CLOUDWATCH_NAMESPACE
                      Value: !Ref MetricsCloudwatchNamespace
                    - Name: METRICS_CLOUDWATCH_RATE
                      Value: !Ref MetricsCloudwatchRate
                    - Name: METRICS_CLOUDWATCH_INITIAL_DELAY
                      Value: !Ref MetricsCloudwatchInitialDelay
                    - Name: METRICS_CLOUDWATCH_JVM_ENABLED
                      Value: !Ref MetricsCloudwatchJVMEnabled
                    - Name: METRICS_CLOUDWATCH_DRY_RUN
                      Value: !Ref MetricsCloudwatchDryRun
                    - Name: METRICS_CLOUDWATCH_EXCLUDE_METRICS_LIST
                      Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
                  Ulimits:
                    - Name: nofile
                      HardLimit: !Ref FileDescriptorsHardLimit
                      SoftLimit: !Ref FileDescriptorsSoftLimit
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                    - SourceVolume: !Ref GerritDataVolume
                      ContainerPath: /var/gerrit/data
                    - SourceVolume: !Ref GerritCacheVolume
                      ContainerPath: /var/gerrit/cache
                    - SourceVolume: !Ref GerritDbVolume
                      ContainerPath: /var/gerrit/db
                    - SourceVolume: !Ref GerritLogsVolume
                      ContainerPath: /var/gerrit/logs
                  Cpu: !Ref GerritCPU
                  Memory: !Ref GerritRAM
                  PortMappings:
                    - ContainerPort: !Ref HTTPContainePort
                      HostPort: !Ref HTTPHostPort
                      Protocol: tcp
                    - ContainerPort: !Ref SSHContainerPort
                      HostPort: !Ref SSHHostPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                  Links:
                    Fn::If:
                      - ShouldEnableXRay
                      -
                        - !FindInMap ['XRay', 'Info', 'ContainerName']
                      - !Ref "AWS::NoValue"
                - Name: !Ref GitDaemonServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GitDaemonDockerImage}'
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                  Cpu: 256
                  Memory: 512
                  PortMappings:
                    - ContainerPort: !Ref GitPort
                      HostPort: !Ref GitPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                - Name: !Ref GitSSHServiceName
                  Essential: true
                  Image: !Sub '${DockerRegistryUrl}/${GitSSHDockerImage}'
                  Environment:
                    - Name: TZ
                      Value: US/Pacific
                    - Name: SSH_USERS
                      Value: gerrit:1000:1000
                    - Name: AWS_REGION
                      Value: !Ref AWS::Region
                    - Name: GERRIT_KEY_PREFIX
                      Value: !Ref GerritKeyPrefix
                  MountPoints:
                    - SourceVolume: !Ref GerritGitVolume
                      ContainerPath: /var/gerrit/git
                  Cpu: 256
                  Memory: 512
                  PortMappings:
                    - ContainerPort: !Ref GitSSHContainerPort
                      HostPort: !Ref GitSSHPort
                      Protocol: tcp
                  LogConfiguration:
                    LogDriver: awslogs
                    Options:
                        awslogs-group: !Ref ClusterStackName
                        awslogs-region: !Ref AWS::Region
                        awslogs-stream-prefix: !Ref EnvironmentName
                - Fn::If:
                    - ShouldEnableXRay
                    - Name: !FindInMap ['XRay', 'Info', 'ContainerName']
                      Essential: false
                      Image: "amazon/aws-xray-daemon"
                      Cpu: 32
                      MemoryReservation: 256
                      PortMappings:
                        - HostPort: 2000
                          ContainerPort: 2000
                          Protocol: "udp"
                    - !Ref "AWS::NoValue"
            Volumes:
              - Name: !Ref 'GerritDbVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-db: !Join ['-', [!Ref EnvironmentName, !Ref GerritDbVolume]]
              - Name: !Ref 'GerritGitVolume'
                EFSVolumeConfiguration:
                  FilesystemId:
                    Fn::ImportValue:
                      !Join [':', [!Ref 'ClusterStackName', 'ReplicaFileSystemID']]
              - Name: !Ref 'GerritDataVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-data: !Join ['-', [!Ref EnvironmentName, !Ref GerritDataVolume]]
              - Name: !Ref 'GerritCacheVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-cache: !Join ['-', [!Ref EnvironmentName, !Ref GerritCacheVolume]]
              - Name: !Ref 'GerritLogsVolume'
                DockerVolumeConfiguration:
                  Scope: shared
                  Autoprovision: true
                  Driver: local
                  Labels:
                    gerrit-logs: !Join ['-', [!Ref EnvironmentName, !Ref GerritLogsVolume]]

    ReplicaCPUAutoScaling:
      Type: AWS::CloudFormation::Stack
      Properties:
        TemplateURL: !Join [ '', ['https://', !Ref TemplateBucketName, '.s3.amazonaws.com/cf-ecs-service-cpu-autoscaling.yml'] ]
        TimeoutInMinutes: '5'
        Parameters:
          AutoScalingMinCapacity: !Ref ReplicaAutoScalingMinCapacity
          AutoScalingDesiredCapacity: !Ref ReplicaAutoScalingDesiredCapacity
          AutoScalingMaxCapacity: !Ref ReplicaAutoScalingMaxCapacity
          AutoScalingScaleInCooldown: !Ref ReplicaAutoScalingScaleInCooldown
          AutoScalingScaleOutCooldown: !Ref ReplicaAutoScalingScaleOutCooldown
          AutoScalingTargetCPUPercentage: !Ref ReplicaAutoScalingTargetCPUPercentage
          ResourceId:
            !Join
            - ''
            - - 'service/'
              - Fn::ImportValue: !Join [':', [!Ref 'ClusterStackName', 'ClusterName']]
              - '/'
              - !GetAtt GerritService.Name

    ReplicaHTTPLoadBalancerSG:
      Type: AWS::EC2::SecurityGroup
      Properties:
        VpcId:
          Fn::ImportValue:
            !Join [':', [!Ref 'ClusterStackName', 'VPCId']]
        GroupDescription: "Allow public HTTPS traffic to replicas"
        GroupName: !Sub '${ClusterStackName}-R-H'
        SecurityGroupIngress:
          - IpProtocol: 'tcp'
            FromPort: !Ref HTTPSPort
            ToPort: !Ref HTTPSPort
            CidrIp: '0.0.0.0/0'
            Description: "HTTPS connections from everywhere (IPv4)"
          - IpProtocol: 'tcp'
            FromPort: !Ref HTTPSPort
            ToPort: !Ref HTTPSPort
            CidrIpv6: '::/0'
            Description: "HTTPS connections from everywhere (IPv6)"

    ReplicaHTTPLoadBalancer:
      Type: AWS::ElasticLoadBalancingV2::LoadBalancer
      Properties:
        Name: !Sub '${ClusterStackName}-R-H'
        Type: application
        Scheme: !Ref 'LoadBalancerScheme'
        SecurityGroups:
          - !Ref ReplicaHTTPLoadBalancerSG
        Subnets:
          - Fn::ImportValue:
              !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetOne']]
          - Fn::ImportValue:
              !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetTwo']]
        Tags:
          - Key: Name
            Value: !Join ['-', [!Ref 'EnvironmentName', !Ref 'GerritServiceName', 'alb']]

    ReplicaSSHLoadBalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
            Name: !Sub '${ClusterStackName}-R-S'
            Type: network
            Scheme: !Ref 'LoadBalancerScheme'
            LoadBalancerAttributes:
              - Key: 'load_balancing.cross_zone.enabled'
                Value: true
            Subnets:
              - Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetOne']]
              - Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'PublicSubnetTwo']]
            Tags:
                - Key: Name
                  Value: !Join ['-', [!Ref 'EnvironmentName', !Ref 'GerritServiceName', 'nlb']]

    HTTPTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        DependsOn: ReplicaHTTPLoadBalancer
        Properties:
            VpcId:
              Fn::ImportValue:
                  !Join [':', [!Ref 'ClusterStackName', 'VPCId']]
            Port: !Ref HTTPHostPort
            Protocol: HTTP
            HealthCheckPort: !Ref HTTPHostPort
            HealthCheckProtocol: HTTP
            HealthCheckPath: '/config/server/healthcheck~status'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



