How can I send custom HTTP responses for specific URLs from an Application Load Balancer?

2 minute read
0

I want to forward custom HTTP responses and drop client requests for specific URLs from an Application Load Balancer.

Resolution

Use fixed-response actions to drop client requests and return a custom HTTP response. You can use this action to return a 2XX, 4XX, or 5XX response code and an optional message.

To add a rule with a fixed-response action on your Application Load Balancer's listener, complete the following steps:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
  2. On the navigation pane, under Load Balancing, choose Load Balancers.
  3. Select your load balancer, and then choose the Listeners tab.
  4. Select your listener and choose Actions, and then choose Manage rules.
  5. Choose Add rules (the plus sign) in the menu bar. This adds the Insert Rule icons at every location where you can insert a rule in the priority order.
  6. Choose Insert Rule and define the rule:
    To add a fixed-response action, choose Add action and then choose Return fixed response. Enter a response code and optional response body, and then choose the check mark.
    (Optional) To configure host-based routing, choose Add condition, and then choose Host is. Enter the hostname (for example, "*.example.com"), and then choose the check mark.
    (Optional) To configure path-based routing, choose Add condition, and then choose Path is. Enter the path pattern (for example, /img/*), and then choose the check mark.
    (Optional) To change the order of the rule, use the arrows. The default rule must have the last priority.
  7. Choose Save.

Related Information

Listener rules

AWS OFFICIAL
AWS OFFICIALUpdated 10 months ago
6 Comments

Is it possible to set response headers on a fixed response? All I can see if the option to set body

replied 6 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 6 months ago

Any update on the initial question - is it possible to set custom header in the fixed-response type?

replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago

As documented here https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-rule.html
The following parameters only are allowed in the Fixed Response. Additional headers are not supported.
MessageBody -> (string)
The message.
StatusCode -> (string)
The HTTP response code (2XX, 4XX, or 5XX).
ContentType -> (string)
The content type.
Valid Values: text/plain | text/css | text/html | application/javascript | application/json

AWS
V_K_
replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago