How to setup geofencing and IP allow-list for Cognito user pool
Tags: aws, cognito, security, waf
Published: 2022-08-18 08:37:25

---
title: How to setup geofencing and IP allow-list for Cognito user pool
published: true
date: 2022-08-18 01:06:23 UTC
tags: AWS,Cognito,Security,WAF
canonical_url: https://theburningmonk.com/2022/08/how-to-setup-geofencing-and-ip-allow-list-for-cognito-user-pool/
---

AWS announced a new feature this week that lets you [enable WAF protection for Cognito user pools](https://aws.amazon.com/about-aws/whats-new/2022/08/amazon-cognito-enables-native-support-aws-waf/). And one of the things you can do with this is to implement geo-fencing and IP allow/deny lists.

Geo-fencing and IP allow/deny lists are usually implemented in the frontend as well as the backend APIs. But users (and malicious actors) can still bypass the geo-fence and IP blockade by interacting with the Cognito user pool directly. This is where the WAF protection comes in.

You can associate a Cognito user pool with a regional WAF web ACL. This can be done via CloudFormation, but you can also do it via the Cognito console (note: this only works in the “new” console experience for Cognito).

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f490627e8.png)

### Geo-fencing

You can use geo-fencing to block users from certain countries from signing up or logging into your application.

To do that, add a custom rule to the WAF web ACL like this:

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f4b7e0c2f.png)

Which identifies users from the Netherlands based on their IP address.

And if you set the matched action to `Block` (this is the default) then the rule would forbid these users from interacting with the Cognito user pool’s public API (and hosted UI if you have configured one).

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f4e2c8893.png)

Users from the Netherlands would get a `ForbiddenException` like this:

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f4f9949ab.png)

This is how you can implement geo-fencing using a deny list.

Similarly, if you want to only allow users from an allowed list of countries, then change the rule to use the `NOT` condition.

Here, we’re matching users who are not from the Netherlands and forbidding them from accessing the Cognito user pool.

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f50ec6fb1.png)

### IP allow/deny list

You can also apply the same allow/deny list to individual IP addresses. This might be the case if users are only supposed to use your system inside a VPN. You can now use WAF to ensure that all traffic originates from a set of allowed IP addresses.

To do this, you need to first create an IP set in WAF to capture the list of allowed IP addresses.

The IP addresses must be expressed in the [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) format, like this:

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f53b6ee94.png)

Once you have created the IP set, you can create a rule to block all traffic that did not originate from the list of allowed IP addresses.

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f54f36153.png)

Once again, you can flip the match condition (from NOT matched to matched) to implement an IP deny list.

### Dealing with VPN proxies

Geo-fencing systems can often be side-stepped by VPN proxies such as NordVPN. Luckily, WAF have you covered here as well.

You can add the managed rule `Anonymous IP list` to the web ACL and it’ll block traffic originating from VPN proxies, Tor nodes and even hosting providers such as AWS.

![](https://theburningmonk.com/wp-content/uploads/2022/08/img_62f7f5674b7b9.png)

This rule works against commercial VPN proxies such as NordVPN as well as users who host their own private VPNs using EC2 instances.

I hope you find this interesting. It’s one of the new topics that we’ll be discussing in my upcoming [**Production-Ready Serverless**](https://productionreadyserverless.com/?utm_campaign=cognito-waf&utm_source=blog&utm_medium=footnote) workshop.

If you want to learn more about running serverless in production and what it takes to build production-ready serverless applications then check out you should definitely check it out!

[![](https://theburningmonk.com/wp-content/uploads/2022/07/img_62d34443958b3.png)](https://productionreadyserverless.com/?utm_campaign=cognito-waf&utm_source=blog&utm_medium=footnote)

In the workshop, I will give you a quick introduction to AWS Lambda and the Serverless framework, and take you through topics such as:

- testing strategies
- how to secure your APIs
- API Gateway best practices
- CI/CD
- configuration management
- security best practices
- event-driven architectures
- how to build observability into serverless applications

and much more!

The post [How to setup geofencing and IP allow-list for Cognito user pool](https://theburningmonk.com/2022/08/how-to-setup-geofencing-and-ip-allow-list-for-cognito-user-pool/) appeared first on [theburningmonk.com](https://theburningmonk.com).