NEW: lambda function

This commit is contained in:
xpk 2020-06-29 19:28:42 +08:00
parent af2032a462
commit bcacaa9c90
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86

27
py/nextSubnetLambda.py Normal file
View File

@ -0,0 +1,27 @@
"""
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
import json
def lambda_handler(event, context):
session = boto3.Session(region_name='us-west-2')
ec2 = session.resource('ec2')
vpc = ec2.Vpc('vpc-7155ca14')
subnets = []
for subnet in vpc.subnets.all():
subnets.append(IPNetwork(subnet.cidr_block))
lastSubnet = sorted(subnets).pop()
# TODO implement
return {
'statusCode': 200,
'body': json.dumps(str(lastSubnet.next()))
}