-
-
Save samuraitruong/d465c2a3b2cf244f8782c3fc7d4f67d5 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