UPD: added vpc lookup

This commit is contained in:
xpk 2020-07-02 11:36:28 +08:00
parent 04a1641132
commit 0d2cf13790
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86

View File

@ -2,18 +2,22 @@
To use this function in lambda, one needs to upload netaddr and this code as a package. here is how To use this function in lambda, one needs to upload netaddr and this code as a package. here is how
pip3 install netaddr -t ./ pip3 install netaddr -t ./
zip -r ../acrolinxNextSubnet.zip . zip -r ../acrolinxNextSubnet.zip .
The cloudformation call back is to be implemented.
""" """
from netaddr import * from netaddr import *
import boto3 import boto3
#import cfnresponse import cfnresponse
import json import json
import os
def lambda_handler(event, context): 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') ec2 = session.resource('ec2')
vpc = ec2.Vpc('vpc-7155ca14') vpc = ec2.Vpc(my_vpc[my_region])
subnets = [] subnets = []
for subnet in vpc.subnets.all(): for subnet in vpc.subnets.all():
subnets.append(IPNetwork(subnet.cidr_block)) subnets.append(IPNetwork(subnet.cidr_block))
@ -22,11 +26,13 @@ def lambda_handler(event, context):
nextSubnet = str(lastSubnet.next()) nextSubnet = str(lastSubnet.next())
return { return {
'statusCode': 200, 'region': my_region,
'body': json.dumps(str(lastSubnet.next())) 'body': json.dumps(nextSubnet)
} }
# response_data = {} # response_data = {}
# response_data = {"cidr": nextSubnet} # response_data = {"cidr": nextSubnet}
# my_data = nextSubnet # my_data = nextSubnet
# cfnresponse.send(event,context,cfnresponse.SUCCESS, response_data, my_data) # cfnresponse.send(event,context,cfnresponse.SUCCESS, response_data, my_data)