Search code examples
amazon-web-servicesamazon-ecsaws-application-load-balancer

502 Bad gateway : node app on container port 5000


I have hosted an angular app as a Microservice in aws here : http://test-2079808347.us-east-2.elb.amazonaws.com:5000. I'm getting a 502 Bad Gateway error. Given below is the yaml template of the service and the Task definition. The image I'm using is working in the local environment when I run docker run -p 5000:5000 <image-tag>

    Resources:
      Service:
        Type: AWS::ECS::Service
        DependsOn: ListenerRule
        Properties:
          Cluster: !Ref Cluster
          Role: !Ref ServiceRole
          DesiredCount: !Ref DesiredCount
          TaskDefinition: !Ref TaskDefinition
          LoadBalancers:
            - ContainerName: "website-service"
              ContainerPort: 5000
              TargetGroupArn: !Ref TargetGroup

      TaskDefinition:
        Type: AWS::ECS::TaskDefinition
        Properties:
          Family: website-service
          ContainerDefinitions:
            - Name: website-service
              Essential: true
              Image: registry.hub.docker.com/abameerdeen/activity_service:latest
              Memory: 128
              Environment:
                - Name: PRODUCT_SERVICE_URL
                  Value: !Ref ProductServiceUrl
              PortMappings:
                - ContainerPort: 5000
              LogConfiguration:
                LogDriver: awslogs
                Options:
                  awslogs-group: !Ref CloudWatchLogsGroup
                  awslogs-region: !Ref AWS::Re
  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      VpcId: !Ref VPC
      Port: 5000
      Protocol: HTTP
      Matcher:
        HttpCode: 200-299
      HealthCheckIntervalSeconds: 10
      HealthCheckPath: /profile
      HealthCheckProtocol: HTTP
      HealthCheckTimeoutSeconds: 5
      HealthyThresholdCount: 2

Given below is the yaml template of the load balancer.

Resources:
  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: !Ref EnvironmentName
      Subnets: !Ref Subnets
      SecurityGroups:
        - !Ref SecurityGroup
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: 5000
      Protocol: HTTP
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref DefaultTargetGroup

  # We define a default target group here, as this is a mandatory Parameters
  # when creating an Application Load Balancer Listener. This is not used, instead
  # a target group is created per-service in each service template (../services/*)
  DefaultTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Sub ${EnvironmentName}-default
      VpcId: !Ref VPC
      Port: 80
      Protocol: HTTP

Given below is the Cloudformation stack. enter image description here


Solution

  • The error was I didn't had a service listening on port 5000 from the Microservice side. My bad. So, if anyone come across the same. Make sure you have the right image. Also make sure, ListenerRule and TargetGroup has been set properly.

    eg:-

    TargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        Properties:
          VpcId: !Ref VPC
          Port: 80
          Protocol: HTTP
          Matcher:
            HttpCode: 200-299
          HealthCheckIntervalSeconds: 10
          HealthCheckPath: /
          HealthCheckProtocol: HTTP
          HealthCheckTimeoutSeconds: 5
          HealthyThresholdCount: 2
    
      ListenerRule:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
          ListenerArn: !Ref Listener
          Priority: 100
          Conditions:
            - Field: path-pattern
              Values: [ "/*" ]
          Actions:
            - TargetGroupAr