Last active
March 13, 2018 04:38
-
-
Save patrickbrandt/4d8297cd2314d9737e714dd702b5c7fc to your computer and use it in GitHub Desktop.
Restricting Lambda function by IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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