AWS: Limit API Access To Specific Regions

You can now use aws:RequestedRegion as a global condition key to control which regions your users can make calls to, regardless of whether the AWS underlying service supports region-level controls or not.

For IAM-policies to work, they need to be attached to a role, group or user. (order of preference)

IAM-policy Examples

Limit API Access for users and services to EU-Region-only and global

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DenyAllOutsideEU",
            "Effect": "Deny",
            "Action": "*",
            "Resource": "*",
            "Condition": {
                "StringNotEquals": {
                    "aws:RequestedRegion": [
                        "eu-central-1",
                        "eu-west-1",
                        "eu-west-2",
                        "eu-west-3"
                    ]
                }
            }
        }
    ]
}

This snippet limits API region based requests to eu-central-1 plus eu-west-1, eu-west-2 and eu-west-3

The statement is build out of a "deny" to "all" with the condition: "aws:RequestedRegion": and a comma-delimited array of [ "AWS::Region" ]. The AWS::Region can be found/looked up in the table below.

There are some issues with cloudfront/acm potentially when working with certificates as one needs to access resources from us-east-1. You might need to add an exception of ACM into the condition

Region Table

RegionNameAWS::Region
US East 1N. Virginiaus-east-1
US East 2Ohious-east-2
US West 1N. Californiaus-west-1
US West 2Oregonus-west-2
Canada Central 1Centralca-central-1
Asia Pacific South 1Mumbaiap-south-1
Asia Pacific North-East 1Tokyoap-northeast-1
Asia Pacific North-East 2Seoulap-northeast-2
Asia Pacific North-East 3Osaka-Localap-northeast-3
Asia Pacific South-East 1Singaporeap-southeast-1
Asia Pacific South-East 2Sydneyap-southeast-2
China North 1Beijingcn-north-1
China North-West 1Ningxiacn-northwest-1
EU Central 1Frankfurteu-central-1
EU West 1Irelandeu-west-1
EU West 2Londoneu-west-2
EU West 3Pariseu-west-3
South America East 1Sao Paulosa-east-1

For more information: See Reference Policy Conditions

Cloudformation - YAML

Description: Managed Policies Examples
AWSTemplateFormatVersion: '2010-09-09'
Metadata:
  Version: 0.1
  Author: Angelique Dawnbringer
  Changes: initial commit - Added gov-policy-restrict-to-eu-access
  Email: angelique.dawnbringer.net
  Phone: +46727043617

Resources:
  GovEUonlyPolicy:
    Type: 'AWS::IAM::ManagedPolicy'
    Properties:
      ManagedPolicyName: gov-policy-restrict-api-to-eu-v0.1
      Description: Policy that restricts API access to EU-based resources and APIs only # https://www.dawnbringer.net/blog/1057/AWS:%20Limit%20API%20Access
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Sid: DenyAllOutsideEU
            Effect: Deny #explicit deny to force all non-eu. This will "hurt" cloudfront potentially. Needs checking / consideration
            Action:
              - '*'
            Resource:
              - '*'
            Condition:
              StringNotEquals:
                'aws:RequestedRegion':
                  - 'eu-central-1' # Frankfurt
                  - 'eu-west-1'    # Ireland
                  - 'eu-west-2'    # London
                  - 'eu-west-3'    # Paris

Outputs:
  GovEUonlyPolicy:
    Value: !Ref GovEUonlyPolicy
    Description: ARN of the policy
    Export: # optional
      Name: !Sub '${AWS::StackName}-GovEUonlyPolicy'
Author: Angelique Dawnbringer Published: 2018-04-26 19:12:15 Keywords:
  • AWS
  • IAM
  • Limit API Access
  • Policy Condition
  • Region Limit
Modified: 2018-05-03 15:59:45