How can I route requests based on the source IP using an Application Load Balancer?

Last updated: 2020-04-28

I want to perform specific actions on requests based on the source IP of the request. This might include sending custom responses to specific users, or serving different versions of an application to a certain set of users. How can I do this using an Application Load Balancer?

Resolution

There are several use cases in which you might want to perform specific actions based on the source IP of a request. For example, let's say that you have two versions of an application. One version is a public version that's meant for global users. The other is an internal version that includes some extended (beta) features. You might only want the internal version to be available to employees accessing the application from corporate network CIDRs. To accomplish this, and many other similar tasks, you can configure listener rules based on source IP addresses.

Before you begin, keep in mind that a rule based on source IP checks the source IP address in the IP header (layer-3). If there's a proxy or firewall between the client and the Application Load Balancer that changes the source IP, then specify the firewall/proxy's IP address in the listener rule.

Also note that listener rules aren't recommended for blocking requests from clients. To block client requests, you can use security groups or network access control lists. To block a large number of clients, you can use AWS WAF.

  1. Create an Application Load Balancer, if you don't already have one.
  2. Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
  3. On the navigation pane, under Load Balancing, choose Load Balancers.
  4. Select your load balancer.
  5. Choose the Listeners tab.
  6. Select your listener, and then choose View/edit rules.
  7. Choose the Add rules icon (the plus sign), and then choose Insert rule.
  8. Choose Add condition, and then choose Source IP.
  9. Specify the IP addresses for which you plan to configure a different action.
    Note: You can specify either a single IP address or network CIDRs with prefixes. For example, "1.1.1.1/32" or "10.8.0.0/21".
  10. Choose Add action, and then select the required action. For example,
    Forward – To forward to a different target group. (Such as forwarding to a target group running an internal version of an application.)
    Return fixed response – To block specific users or provide custom responses to specific users.
  11. To save the condition, choose the checkmark icon.
  12. To save the rule, choose Save.

Did this article help you?

Anything we could improve?


Need more help?