2020-06-29 19:28:42 +08:00
|
|
|
"""
|
|
|
|
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 .
|
|
|
|
"""
|
|
|
|
from netaddr import *
|
|
|
|
import boto3
|
2020-07-02 11:36:28 +08:00
|
|
|
import cfnresponse
|
2020-06-29 19:28:42 +08:00
|
|
|
import json
|
2020-07-02 11:36:28 +08:00
|
|
|
import os
|
2020-06-29 19:28:42 +08:00
|
|
|
|
|
|
|
def lambda_handler(event, context):
|
2020-07-02 11:36:28 +08:00
|
|
|
my_vpc = {
|
|
|
|
'eu-west-1': 'vpc-55a5a930',
|
|
|
|
'us-west-2': 'vpc-7155ca14'
|
|
|
|
}
|
|
|
|
session = boto3.session.Session()
|
|
|
|
my_region = session.region_name
|
2020-06-29 19:28:42 +08:00
|
|
|
ec2 = session.resource('ec2')
|
2020-07-02 11:36:28 +08:00
|
|
|
vpc = ec2.Vpc(my_vpc[my_region])
|
2020-06-29 19:28:42 +08:00
|
|
|
subnets = []
|
|
|
|
for subnet in vpc.subnets.all():
|
|
|
|
subnets.append(IPNetwork(subnet.cidr_block))
|
|
|
|
|
|
|
|
lastSubnet = sorted(subnets).pop()
|
2020-06-29 19:45:51 +08:00
|
|
|
nextSubnet = str(lastSubnet.next())
|
2020-06-29 19:28:42 +08:00
|
|
|
|
|
|
|
return {
|
2020-07-02 11:36:28 +08:00
|
|
|
'region': my_region,
|
|
|
|
'body': json.dumps(nextSubnet)
|
2020-06-29 19:28:42 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:45:51 +08:00
|
|
|
# response_data = {}
|
|
|
|
# response_data = {"cidr": nextSubnet}
|
|
|
|
# my_data = nextSubnet
|
|
|
|
# cfnresponse.send(event,context,cfnresponse.SUCCESS, response_data, my_data)
|
2020-07-02 11:36:28 +08:00
|
|
|
|
|
|
|
|