Block access from specific regions using AWS Cloudfront's geo-restriction feature

My name is Teraoka and I am an infrastructure engineer.


I tried to set up
AWS Cloudfront The setting itself is simple, but I thought it was practical, so I'll summarize it here.

Amazon CloudFront CDN (Content Delivery and Streaming) | AWS

■What is Cloudfront?

Cloudfront is AWS's CDN (Content Delivery Network) service, with

servers located
around the world in "edge locations" By using these "edge locations,"
when a user requests a URL, the system determines the user's geographic location from their IP address and
returns a response from the nearest server.
Thanks to this mechanism, simply installing Cloudfront in front of services like EC2 and S3
eliminates the need to access servers in distant locations,
making it easy to speed up your website.

■What is the geographic restriction function?

I'll try using the "geo-restriction feature" mentioned in the title.
Cloudfront's default setting
delivers requested content regardless of the user's location.
This is convenient and appreciated, but

  • I want to restrict access because I frequently receive attacks from a specific area
  • Access is not permitted in some countries due to copyright reasons

It can be a problem if there are reasons such as:

In such cases, Cloudfront's feature allows you to allow or deny access at the country level.
This is the geo-restriction feature.
Let's actually set it up and try it out.

■Let's try it out


I created a Cloudfront distribution in advance to distribute the S3 content

I tried delivering a static site with AWS S3 | Beyond Co., Ltd.

If you are setting up to distribute content stored in S3,
the Origin item will be the S3 endpoint.
Make a note of the Domain Name item as you will access it later in your browser.
There is a checkbox on the left, so check it and
click Distribution Settings.

2016-09-10_03h06_58

This will take you to the distribution settings screen, so
select the Restrictions tab.
The Geo Restriction item should be disabled.
Enabling this will allow you to use the geo-restriction feature.
Check the checkbox on the left and click Edit.

2016-09-10_01h42_05

This will take you to the Geo-Restriction settings screen

2016-09-10_03h15_41

The Enable Geo-Restriction item should be set to No, so
if you select Yes, two new items should appear.
In Restriction Type, select the type of restriction you want to apply.
To briefly summarize each item:

Item name overview
Whitelist "Whitelist" allows you to set the countries you want to allow access to, and only allows users to access content if they are in one of the countries on the list
Blacklist "Blacklist" allows you to set countries you want to block access from, and if a user is from one of the countries on the list, they will be denied access to the content

This time, we will use the "Blacklist" to prohibit access from specific regions.
Select Blacklist and select a country in the Countries section.
Let's select Japan to check whether access will be prohibited.
Select JP (Japan) from the list of countries on the left and click the ADD>> button. It
should then be added to the right.
In this state, click Yes, Edit to complete the edit.

2016-09-10_03h38_59

Cloudfront will take a while to apply the settings, so
go back to the distribution list and
wait until the status changes from in progress to deployed.

2016-09-10_03h36_56

Try accessing the CloudFront endpoint from your browser

2016-09-10_03h40_42

Yes, I was blocked by Cloudfront, which I created myself

What do you think?
You can restrict access with just these steps, and
I think it's a feature worth using, so please give it a try.
Until next time, thank you.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
3,246
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yuki Teraoka

Joined Beyond in 2016 and is currently in his 6th year as an Infrastructure Engineer
MSP, where he troubleshoots failures while
also designing and building infrastructure using public clouds such as AWS.
Recently, I
have been working with Hashicorp tools such as Terraform and Packer as part of building container infrastructure such as Docker and Kubernetes and automating operations, and I
also play the role of an evangelist who speaks at external study groups and seminars.

・GitHub
https://github.com/nezumisannn

・Presentation history
https://github.com/nezumisannn/my-profile

・Presentation materials (SpeakerDeck)
https://speakerdeck.com/nezumisannn

・Certification:
AWS Certified Solutions Architect - Associate
Google Cloud Professional Cloud Architect