aws-cloudformationamazon-ecsaws-application-load-balanceraws-alb

504 Gateway Timeout using Application Load Balancer in ECS


Deploying a Laravel web application on ECS, in order to enable autoscaling I am using an Application Load Balancer. The application worked (and scaled) perfectly until I introduced a heavy weight page, where I started to get 504 Gateway Timeout errors after a minute or so.

I am pretty sure the single web server has a higher timeout (this never happens when the application is tested in local) so the problem must be related to something related to AWS environment (ECS / ALB).

Below you can find a snipped of the ALB setting

AdminLoadBalancer:
  Type: AWS::ElasticLoadBalancingV2::LoadBalancer
  Properties:
    SecurityGroups:
      - !Ref 'AlbSecurityGroup'
    Subnets:
      - !Ref 'PublicSubnetAz1'
      - !Ref 'PublicSubnetAz2'
    Scheme: internet-facing
  Name: !Join ['-', [!Ref 'AWS::StackName', 'lb']]

Solution

  • After some attempts, I solved the issue setting the idle timeout attribute of the load balancer, as explained here in theory, because nothing was wrong with the single ECS Tasks. In Cloudformation, it was enough to add the attribute setting of the parameter, and double the default value.

    AdminLoadBalancer:
      Type: AWS::ElasticLoadBalancingV2::LoadBalancer
      Properties:
        LoadBalancerAttributes:
          - Key: 'idle_timeout.timeout_seconds'
            Value: 120
        SecurityGroups:
          - !Ref 'AlbSecurityGroup'
        Subnets:
          - !Ref 'PublicSubnetAz1'
          - !Ref 'PublicSubnetAz2'
        Scheme: internet-facing
      Name: !Join ['-', [!Ref 'AWS::StackName', 'lb']]