Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Last active March 13, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save patrickbrandt/4d8297cd2314d9737e714dd702b5c7fc to your computer and use it in GitHub Desktop.
Save patrickbrandt/4d8297cd2314d9737e714dd702b5c7fc to your computer and use it in GitHub Desktop.
Restricting Lambda function by IP address
import json
import os
def ping(event, context):
ip1 = event['headers']['X-Forwarded-For'].split(',')[0]
ip2 = event['requestContext']['identity']['sourceIp']
print('two different referring IP address parsing techniques ip1: %s and ip2: %s') % (ip1, ip2)
referringIP = event['requestContext']['identity']['sourceIp']
#TODO: use a comma-delimited string to store multiple IP address values in the environment variable
if (os.environ['IP_WHITELIST'] != referringIP):
return {
'statusCode': 401,
'body': 'This IP address is not allowed %s' % referringIP
}
return {
'statusCode': 200,
'body': 'This IP address is allowed %s' % referringIP
}
service: ip-restrict
provider:
name: aws
runtime: python2.7
stage: dev
region: us-west-2
profile: ko-playground-admin
environment:
IP_WHITELIST: "44.99.53.83"
functions:
ping:
handler: handler.ping
events:
- http:
path: ping
method: GET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment