Search code examples
amazon-web-servicesaws-application-load-balancer

AWS ALB routing - Requested url not found


I have a Microservice dedicated as the front-end of an admin area of a website. The Microservice has a route dedicated to it, /admin. Which is giving me a 404 error when I'm trying to access. Given below is an extract from the cloud template of the Microservice.

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

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      Family: activity-monitor-service
      ContainerDefinitions:
        - Name: activity-monitor-service
          Essential: true
          Image: registry.hub.docker.com/abameerdeen/activity_monitor:v4
          Memory: 128
          Environment:
            - Name: PRODUCT_SERVICE_URL
              Value: !Ref ProductServiceUrl
          PortMappings:
            - ContainerPort: 80
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref CloudWatchLogsGroup
              awslogs-region: !Ref AWS::Region

  CloudWatchLogsGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Ref AWS::StackName
      RetentionInDays: 365

  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      VpcId: !Ref VPC
      Port: 80
      Protocol: HTTP
      Matcher:
        HttpCode: 200-299
      HealthCheckIntervalSeconds: 10
      HealthCheckPath: /test.html
      HealthCheckProtocol: HTTP
      HealthCheckTimeoutSeconds: 5
      HealthyThresholdCount: 2

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      ListenerArn: !Ref Listener
      Priority: 2
      Conditions:
        - Field: path-pattern
          Values: [ "/admin/*" ]
      Actions:
        - TargetGroupArn: !Ref TargetGroup
          Type: forward

I have an Application Load Balancer (ALB) in the infrastructure too. Here is my project : https://github.com/ammarisme/aws-cloudformation.git


Solution

  • Above ListenerRule matches files inside the admin folder (eg :- index.html) not the path /. So, I added the missing ListenerRule as given below.

    ListenerRule2:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
          ListenerArn: !Ref Listener
          Priority: 3
          Conditions:
            - Field: path-pattern
              Values: [ "/admin" ]
          Actions:
            - TargetGroupArn: !Ref TargetGroup
              Type: forward