代码之家  ›  专栏  ›  技术社区  ›  balderman

控制AWS Lambda源IP

  •  0
  • balderman  · 技术社区  · 7 年前

    我有一个简单的Lambda函数,它的任务是让HTTP访问某个服务器。

    我需要运行许多副本(数百)的功能 同时 不同的源IP地址 对于每个HTTP,从每个Lambda获取。

    1. 如何确保Lambda函数的每个“副本”都有自己的IP地址?
    2. 如何使用botoapi调用来告诉AWS我需要N个Lambda的并发副本?我在找 here 但我找不到设置并发副本数的参数。

    谢谢

    阿维谢

    至于问题2,我使用下面的代码来调用Lambda函数的N个并发副本。

     import boto3, json
     from concurrent.futures import ThreadPoolExecutor
    
     N = 5
     unique_ips = set()
    
     lambda_client = boto3.client('lambda', region_name='us-west-2')
    
    
     def _lambda_caller(idx):
         test_event = dict(idx=idx)
         res = lambda_client.invoke(
            FunctionName='SimpleHTTPGetter',
            InvocationType='RequestResponse',
            Payload=json.dumps(test_event),
         )
         data = json.loads(res['Payload']._raw_stream.data)
         print('Thread {} is done'.format(idx))
         unique_ips.add(data['body'])
    
     with ThreadPoolExecutor(max_workers=N) as executor:
         for i in range(0,N):
             future = executor.submit(_lambda_caller,i)
         executor.shutdown()
        print('Done')
    

    我的Lambda代码(短版本)

    import json
    import socket    
    
    def lambda_handler(event, context):
        print('-- HTTP Client started')
        hostname = socket.gethostname()    
        ip = socket.gethostbyname(hostname) 
        print('My IP address is {}:'.format(ip))
        return {
            "statusCode": 200,
            "body": ip
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Ryan Breece    7 年前

    你需要创建一个专有网络,确保你附加了一个子网,允许互联网接入。然后将安全策略附加到lambda。 https://medium.com/@philippholly/aws-lambda-enable-outgoing-internet-access-within-vpc-8dd250e11e12