By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do 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

To drop client requests and return a 2XX, 4XX, or 5XX response code and an optional message, use fixed-response actions.

Note: Fixed responses don't support custom headers.

For more information, see the AWS Command Line Interface (AWS CLI) create-rule command.

Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

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. In the navigation pane, under Load Balancing, choose Load Balancers.
  3. Select your load balancer, and then choose Listeners.
  4. Select your listener, and then choose Actions.
  5. Choose Manage rules.
  6. In the navigation pane, choose Add rules to add the Insert Rule icon at locations where you can insert a rule in priority order.
  7. Choose Insert Rule, and then 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 select the check mark.
    (Optional) To configure host-based routing, choose Add condition, and then choose Host is. Enter the host name, for example example.com, and then select 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 select the check mark.
    (Optional) To change the order of the rule, use the arrows. The default rule must have the last priority.
  8. Choose Save.

Related information

Listener rules

AWS OFFICIAL
AWS OFFICIALUpdated 7 months ago
7 Comments

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

replied a year ago

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

profile pictureAWS
EXPERT
replied a year ago

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

replied 9 months ago

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

profile pictureAWS
EXPERT
replied 9 months 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
replied 9 months ago

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

profile pictureAWS
MODERATOR
replied 9 months ago

Please add the possibility to set custom headers.

replied 3 months ago