walkthroughs/howto-metrics-extension-ecs/deploy/djapp-v3.yaml [597:1178]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                              ]
                            }
                          ]
                        }
                      }
                    }
                  }
                }

  DJAppGatewayTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: !Ref GatewayPort
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: gateway
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref DJAppGateway

  DJAppTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: djapp
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref DJAppVirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: djapp
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: BACKENDS
              Value: !Sub >
                [
                  "${JazzVirtualService.VirtualServiceName}:${ContainerPort}",
                  "${MetalVirtualService.VirtualServiceName}:${ContainerPort}"
                ]

  JazzTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: jazz
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref JazzVirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: jazz
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Astrud Gilberto",
                  "Miles Davis"
                ]

  MetalV1TaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      Family: !Sub '${ProjectName}-metal-v1'
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v1
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref MetalV1VirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v1
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Megadeth",
                  "Judas Priest"
                ]

  MetalV2TaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      Family: !Sub '${ProjectName}-metal-v2'
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v2
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref MetalV2VirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v2
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Megadeth (Los Angeles, California)",
                  "Judas Priest (West Bromwich, England)"
                ]

  ###################################################
  # App Mesh
  ###################################################
  Mesh:
    Type: AWS::AppMesh::Mesh
    Properties:
      MeshName: !Ref ProjectName

  DJAppGateway:
    Type: AWS::AppMesh::VirtualGateway
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualGatewayName: gateway
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref GatewayPort
              Protocol: http

  DJAppVirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: djapp
      Spec:
        Backends:
          - VirtualService:
              VirtualServiceName: !GetAtt JazzVirtualService.VirtualServiceName
          - VirtualService:
              VirtualServiceName: !GetAtt MetalVirtualService.VirtualServiceName 
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt DJAppServiceRegistry.Name

  JazzVirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: jazz
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt JazzServiceRegistry.Name

  MetalV1VirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: metal-v1
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt MetalServiceRegistry.Name
            Attributes:
              - Key: ECS_TASK_DEFINITION_FAMILY
                Value: !Sub '${ProjectName}-metal-v1'
  
  MetalV2VirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: metal-v2
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt MetalServiceRegistry.Name
            Attributes:
              - Key: ECS_TASK_DEFINITION_FAMILY
                Value: !Sub '${ProjectName}-metal-v2'

  DJAppVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${DJAppServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualNode:
            VirtualNodeName: !GetAtt DJAppVirtualNode.VirtualNodeName
  
  JazzVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${JazzServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualNode:
            VirtualNodeName: !GetAtt JazzVirtualNode.VirtualNodeName
  
  MetalVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${MetalServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualRouter:
            VirtualRouterName: !GetAtt MetalVirtualRouter.VirtualRouterName

  MetalVirtualRouter:
    Type: AWS::AppMesh::VirtualRouter
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualRouterName: metal
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http

  MetalRoute:
    Type: AWS::AppMesh::Route
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualRouterName: !GetAtt MetalVirtualRouter.VirtualRouterName
      RouteName: metal
      Spec:
        HttpRoute:
          Match:
            Prefix: /
          Action:
            WeightedTargets:
              - VirtualNode: !GetAtt MetalV1VirtualNode.VirtualNodeName
                Weight: 60
              - VirtualNode: !GetAtt MetalV2VirtualNode.VirtualNodeName
                Weight: 40

  DJAppGatewayRoute:
    Type: AWS::AppMesh::GatewayRoute
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualGatewayName: !GetAtt DJAppGateway.VirtualGatewayName
      GatewayRouteName: djapp
      Spec:
        HttpRoute:
          Match:
            Prefix: /
          Action:
            Target:
              VirtualService:
                VirtualServiceName: !GetAtt DJAppVirtualService.VirtualServiceName

Outputs:
  PublicEndpoint:
    Value: !Sub 'http://${PublicLoadBalancer.DNSName}'
    Export:
      Name: !Sub '${ProjectName}:PublicEndpoint'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



walkthroughs/howto-metrics-extension-ecs/deploy/djapp-v4.yaml [602:1183]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                              ]
                            }
                          ]
                        }
                      }
                    }
                  }
                }

  DJAppGatewayTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: !Ref GatewayPort
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: gateway
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref DJAppGateway

  DJAppTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: djapp
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref DJAppVirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: djapp
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: BACKENDS
              Value: !Sub >
                [
                  "${JazzVirtualService.VirtualServiceName}:${ContainerPort}",
                  "${MetalVirtualService.VirtualServiceName}:${ContainerPort}"
                ]

  JazzTaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: jazz
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref JazzVirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: jazz
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Astrud Gilberto",
                  "Miles Davis"
                ]

  MetalV1TaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      Family: !Sub '${ProjectName}-metal-v1'
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v1
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION 
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref MetalV1VirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v1
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Megadeth",
                  "Judas Priest"
                ]

  MetalV2TaskDef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      RequiresCompatibilities:
        - FARGATE
      Family: !Sub '${ProjectName}-metal-v2'
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
      TaskRoleArn: !Ref TaskIAMRole
      ExecutionRoleArn: !Ref TaskExecutionIAMRole
      ProxyConfiguration:
        Type: APPMESH
        ContainerName: envoy
        ProxyConfigurationProperties:
          - Name: IgnoredUID
            Value: '1337'
          - Name: ProxyIngressPort
            Value: '15000'
          - Name: ProxyEgressPort
            Value: '15001'
          - Name: AppPorts
            Value: !Ref ContainerPort
          - Name: EgressIgnoredIPs
            Value: '169.254.170.2,169.254.169.254'
      ContainerDefinitions:
        - Name: envoy
          Image: !Ref EnvoyImage
          Essential: true
          User: '1337'
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: 9901
              Protocol: tcp
            - ContainerPort: 15000
              Protocol: tcp
            - ContainerPort: 15001
              Protocol: tcp
          HealthCheck:
            Command:
              - CMD-SHELL
              - curl -s http://localhost:9901/ready
            Interval: 5
            Timeout: 10
            Retries: 10
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v2
          DockerLabels:
            ECS_PROMETHEUS_METRICS_PATH: /stats/prometheus
            ECS_PROMETHEUS_EXPORTER_PORT: 9901
          Environment:
            - Name: ENVOY_LOG_LEVEL
              Value: debug
            - Name: APPMESH_METRIC_EXTENSION_VERSION
              Value: 1
            - Name: APPMESH_RESOURCE_ARN
              Value: !Ref MetalV2VirtualNode
        - Name: app
          Image: !Ref DJAppImage
          Essential: true
          Ulimits:
            - Name: nofile
              HardLimit: 15000
              SoftLimit: 15000
          PortMappings:
            - ContainerPort: !Ref ContainerPort
              Protocol: tcp
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: metal-v2
          Environment:
            - Name: PORT
              Value: !Ref ContainerPort
            - Name: RESPONSES
              Value: >
                [
                  "Megadeth (Los Angeles, California)",
                  "Judas Priest (West Bromwich, England)"
                ]

  ###################################################
  # App Mesh
  ###################################################
  Mesh:
    Type: AWS::AppMesh::Mesh
    Properties:
      MeshName: !Ref ProjectName

  DJAppGateway:
    Type: AWS::AppMesh::VirtualGateway
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualGatewayName: gateway
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref GatewayPort
              Protocol: http

  DJAppVirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: djapp
      Spec:
        Backends:
          - VirtualService:
              VirtualServiceName: !GetAtt JazzVirtualService.VirtualServiceName
          - VirtualService:
              VirtualServiceName: !GetAtt MetalVirtualService.VirtualServiceName 
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt DJAppServiceRegistry.Name

  JazzVirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: jazz
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt JazzServiceRegistry.Name

  MetalV1VirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: metal-v1
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt MetalServiceRegistry.Name
            Attributes:
              - Key: ECS_TASK_DEFINITION_FAMILY
                Value: !Sub '${ProjectName}-metal-v1'
  
  MetalV2VirtualNode:
    Type: AWS::AppMesh::VirtualNode
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualNodeName: metal-v2
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http
        ServiceDiscovery:
          AWSCloudMap:
            NamespaceName: !Sub '${ProjectName}.local'
            ServiceName: !GetAtt MetalServiceRegistry.Name
            Attributes:
              - Key: ECS_TASK_DEFINITION_FAMILY
                Value: !Sub '${ProjectName}-metal-v2'

  DJAppVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${DJAppServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualNode:
            VirtualNodeName: !GetAtt DJAppVirtualNode.VirtualNodeName
  
  JazzVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${JazzServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualNode:
            VirtualNodeName: !GetAtt JazzVirtualNode.VirtualNodeName
  
  MetalVirtualService:
    Type: AWS::AppMesh::VirtualService
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualServiceName: !Sub '${MetalServiceRegistry.Name}.${ProjectName}.local'
      Spec:
        Provider:
          VirtualRouter:
            VirtualRouterName: !GetAtt MetalVirtualRouter.VirtualRouterName

  MetalVirtualRouter:
    Type: AWS::AppMesh::VirtualRouter
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualRouterName: metal
      Spec:
        Listeners:
          - PortMapping:
              Port: !Ref ContainerPort
              Protocol: http

  MetalRoute:
    Type: AWS::AppMesh::Route
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualRouterName: !GetAtt MetalVirtualRouter.VirtualRouterName
      RouteName: metal
      Spec:
        HttpRoute:
          Match:
            Prefix: /
          Action:
            WeightedTargets:
              - VirtualNode: !GetAtt MetalV1VirtualNode.VirtualNodeName
                Weight: 60
              - VirtualNode: !GetAtt MetalV2VirtualNode.VirtualNodeName
                Weight: 40

  DJAppGatewayRoute:
    Type: AWS::AppMesh::GatewayRoute
    Properties:
      MeshName: !GetAtt Mesh.MeshName
      VirtualGatewayName: !GetAtt DJAppGateway.VirtualGatewayName
      GatewayRouteName: djapp
      Spec:
        HttpRoute:
          Match:
            Prefix: /
          Action:
            Target:
              VirtualService:
                VirtualServiceName: !GetAtt DJAppVirtualService.VirtualServiceName

Outputs:
  PublicEndpoint:
    Value: !Sub 'http://${PublicLoadBalancer.DNSName}'
    Export:
      Name: !Sub '${ProjectName}:PublicEndpoint'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



