The Lambda Blog

Serverless Cloud Guide

Menu
  • Home
  • Categories
  • Projects
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact
Menu
Restricting API Gateway calls with an IP white list using Cloudformation policies

Restricting API Gateway calls with an IP white list using Cloudformation policies

Posted on December 17, 2021August 14, 2022 by user
Navigation » Home » API Gateway

This post will demonstrate how to set up an IAM policy for API Gateway that restricts access to the API based on an IP whitelist – meaning only calls to the API will only be allowed if they originate from the IPs defined in the list.

The definition is different depending on the type of API Gateway – we will cover both the REST and Http variants of API Gateway.

Just mentioning that Http APIs should be the default choice for any new API Gateway implementation – they are faster, cheaper and easier to define and work with. But there are situations where REST APIs may have to be used like if a 3rd party callback service only works with basic authentication and not JWT.

Creating an IP Whitelist for an API Gateway REST API

For Rest APIs, this is done in the Auth property of the cloudformation template.yaml

RESTAPI:
    Type: AWS::Serverless::Api
    Properties:
      Name: rest-api
      StageName: api
      Description: REST API with an IP Whitelist
      Auth:
        ResourcePolicy:
          CustomStatements: [ {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "execute-api:/*/*/*"
          },
            {
              "Effect": "Deny",
              "Principal": "*",
              "Action": "execute-api:Invoke",
              "Resource": "execute-api:/*/*/*",
              "Condition": {
                "NotIpAddress": {
                  "aws:SourceIp": [
                      "xx.xxx.xxx.xxx",
                      "xx.xxx.xxx.x" ]
                }
              }
            }
          ]

Creating an IP Whitelist for an API Gateway Http API

The definition is much simpler for Http APIs. We can replace the IAM style logic with a simple “IpRangeWhitelist” property.

HttpAPI:
    Type: AWS::Serverless::HttpApi
    Properties:
      Description: REST API with an IP Whitelist
      ResourcePolicy:
      	CustomStatements: [ {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "execute-api:/*/*/*"
          }]
        IpRangeWhitelist:
        	- "xx.xxx.xxx.xxx"
        	- "xx.xxx.xxx.x"

Similar definition strategies can be used to create IP block lists.

Recent Posts

  • Coding a JSON format logger in Python for use as a Lambda Layer package
  • Configuring an S3 Bucket to send events to a Lambda destination for processing
  • How to request a public SSL certificate for a domain name from the AWS Certificate Manager Console
  • Creating automated CloudFormation Stack Build and Deployments with AWS CodePipeline and CodeBuild
  • A concise guide to setting up the AWS command-line libraries on your local development environment
  • How to implement a Lambda Authorizer for an AWS AppSync API and invoke the API with the required Authorization Token
  • Filtering CloudWatch Logs by LogGroups and LogStreams and reading them using Python and the Boto3 SDK
  • Azure AD Multi Tenancy issue in AWS Cognito
  • Setting up Enterprise Federation from Azure Active Directory to Amazon Cognito using Open ID Connect
  • How to Setup IAM Multifactor Authentication (MFA) for the AWS CLI

Categories

  • Amplify
  • API Gateway
  • AppSync
  • AWS CLI
  • CloudFormation
  • CloudWatch
  • Cognito
  • DynamoDB
  • EventBridge
  • KMS
  • Lambda
  • Projects
  • Route 53
  • SES
  • SNS

Post Tags

ACM Amplify API Gateway AppSync AWS CLI Azure Boto3 CloudFormation CloudWatch CodeBuild CodePipeline Cognito DynamoDB EventBridge Firebase IAM KMS Lambda OIDC Project Python Rekognition Route53 S3 SAM SES SNS VPC

©2022 The Lambda Blog