Set Up CloudFront Distribution with Geo Restriction (Country Blocking)

Content delivery networks (CDNs) like AWS CloudFront help serve your web content quickly and reliably across the globe. Sometimes, you might want to restrict access to your content by country—for compliance, licensing, or security reasons. This blog will guide you step-by-step on setting up a CloudFront distribution with geo restriction to block specific countries from accessing your content. Step 1: Prepare Your Origin Typically, your origin is an S3 bucket (for static websites) or an EC2 instance, Load Balancer, or custom origin server. Enable static website hosting in the S3 bucket. Ensure the bucket policy allows public reads. For detailed instructions on setting up a static website on S3, check out this article. Step 2: Go to CloudFront Console Open the CloudFront console in AWS. Click on “Create Distribution.” Step 3: Create Distribution Configure your distribution settings as needed. Click “Create Distribution.” Wait until the status changes to “Deployed” and “Enabled.” Step 4: Test Your CloudFront Distribution Use your CloudFront domain (e.g., d123abc456.cloudfront.net) to access your content. Example URL: https://d123abc456.cloudfront.net/index.html Step 5: Implement Geo Restriction (Block by Country) Once your distribution is deployed, click on it in the CloudFront console. Navigate to the Security section. Under Geo restriction, click Edit. Choose Blacklist. Select the countries you want to block (e.g., India). Click Save changes. Conclusion And there you have it! Your CloudFront distribution now restricts access by country, helping you control who can view your content. If needed, you can easily remove or update these geo restrictions by returning to the settings. This method is perfect for compliance, content licensing, or blocking unwanted traffic from certain regions. Pair this with custom error pages to provide a better user experience for blocked users.

Apr 24, 2025 - 02:16
 0
Set Up CloudFront Distribution with Geo Restriction (Country Blocking)

Content delivery networks (CDNs) like AWS CloudFront help serve your web content quickly and reliably across the globe. Sometimes, you might want to restrict access to your content by country—for compliance, licensing, or security reasons. This blog will guide you step-by-step on setting up a CloudFront distribution with geo restriction to block specific countries from accessing your content.

Step 1: Prepare Your Origin

Typically, your origin is an S3 bucket (for static websites) or an EC2 instance, Load Balancer, or custom origin server.

  • Enable static website hosting in the S3 bucket.
  • Ensure the bucket policy allows public reads.

S3 Static Website Hosting

For detailed instructions on setting up a static website on S3, check out this article.

Step 2: Go to CloudFront Console

  1. Open the CloudFront console in AWS.
  2. Click on “Create Distribution.”

Create Distribution

Step 3: Create Distribution

  • Configure your distribution settings as needed.
  • Click “Create Distribution.”
  • Wait until the status changes to “Deployed” and “Enabled.”

CloudFront Settings

More CloudFront Settings

Final Settings

Distribution Created

Step 4: Test Your CloudFront Distribution

Use your CloudFront domain (e.g., d123abc456.cloudfront.net) to access your content.

Example URL: https://d123abc456.cloudfront.net/index.html

Security Tab

Step 5: Implement Geo Restriction (Block by Country)

  1. Once your distribution is deployed, click on it in the CloudFront console.
  2. Navigate to the Security section.
  3. Under Geo restriction, click Edit.
  4. Choose Blacklist.
  5. Select the countries you want to block (e.g., India).
  6. Click Save changes.

Edit Geo Restriction

Select Countries

Blocked View

Conclusion

And there you have it! Your CloudFront distribution now restricts access by country, helping you control who can view your content. If needed, you can easily remove or update these geo restrictions by returning to the settings.

This method is perfect for compliance, content licensing, or blocking unwanted traffic from certain regions. Pair this with custom error pages to provide a better user experience for blocked users.