From 0d2cf13790d3c1d440bde93b8c0e25e3105d782e Mon Sep 17 00:00:00 2001 From: x p k Date: Thu, 2 Jul 2020 11:36:28 +0800 Subject: [PATCH] UPD: added vpc lookup --- py/nextSubnetLambda.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/py/nextSubnetLambda.py b/py/nextSubnetLambda.py index 799457c..319d074 100644 --- a/py/nextSubnetLambda.py +++ b/py/nextSubnetLambda.py @@ -2,18 +2,22 @@ To use this function in lambda, one needs to upload netaddr and this code as a package. here is how pip3 install netaddr -t ./ zip -r ../acrolinxNextSubnet.zip . - -The cloudformation call back is to be implemented. """ from netaddr import * import boto3 -#import cfnresponse +import cfnresponse import json +import os def lambda_handler(event, context): - session = boto3.Session(region_name='us-west-2') + my_vpc = { + 'eu-west-1': 'vpc-55a5a930', + 'us-west-2': 'vpc-7155ca14' + } + session = boto3.session.Session() + my_region = session.region_name ec2 = session.resource('ec2') - vpc = ec2.Vpc('vpc-7155ca14') + vpc = ec2.Vpc(my_vpc[my_region]) subnets = [] for subnet in vpc.subnets.all(): subnets.append(IPNetwork(subnet.cidr_block)) @@ -22,11 +26,13 @@ def lambda_handler(event, context): nextSubnet = str(lastSubnet.next()) return { - 'statusCode': 200, - 'body': json.dumps(str(lastSubnet.next())) + 'region': my_region, + 'body': json.dumps(nextSubnet) } # response_data = {} # response_data = {"cidr": nextSubnet} # my_data = nextSubnet # cfnresponse.send(event,context,cfnresponse.SUCCESS, response_data, my_data) + +