API Gatewayのリソースポリシーで特定IPからのアクセス制限を行う

API Gatewayのリソースポリシーで特定IPからのアクセス制限を行う

API GatewayのリソースポリシーでIP制限をしてみます。

リソースポリシー

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-northeast-1:123456789012:ry67q79e3g/*/*/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": "176.32.250.250"
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:ap-northeast-1:123456789012:ry67q79e3g/*/*/*"
        }
    ]
}

176.32.250.250のIPからアクセスがあった場合に制限をかけます。52.95.36.0/22というようなCIDR表記も可能です。

Resourceはワイルドカードにしていますがこちらも設定が可能です。

VPC内のプライベートIPからの制限は、aws:VpcSourceIpを使用します。

アクセス制限の画面

アクセス拒否したIPからアクセスした画面です。

API Gatewayのリソースポリシーで特定IPからのアクセス制限を行う

参考サイト

特定の IP アドレスが API Gateway REST API にアクセスすることを許可する
IAM JSON policy elements: Condition operators - AWS Identity and Access Management
Describes the operators that you can use in the Condition element of the IAM JSON policy language.
AWS condition keys that can be used in API Gateway resource policies - Amazon API Gateway
The following table contains AWS condition keys that can be used in resource policies for APIs in API Gateway for each authorization type.

コメント

タイトルとURLをコピーしました