in functions/source/lambda_function.py [0:0]
def update_tgw_rt(vpn_routes, tgw_rt_id, tgw_attach_id):
region = os.environ['AWS_REGION']
ec2 = boto3.client('ec2', region_name=region)
uniq_vpn_routes = list(set(vpn_routes))
logger.info("EC2 TGW Route Update {0}".format(uniq_vpn_routes))
#Checking if the route already exsists, if so skip updating the TGW route table
for route in uniq_vpn_routes:
exsisting_route = ec2.search_transit_gateway_routes(
TransitGatewayRouteTableId= tgw_rt_id,
Filters=[
{ 'Name': 'route-search.exact-match',
'Values': [route]
}]
)
if bool(exsisting_route['Routes']):
logger.info("Transit Gateway RT: No update, route {0} exsists, skipping update".format(route))
pass
else:
logger.info("Transit Gateway RT: New route, adding route {0}".format(route))
ec2.create_transit_gateway_route(
DestinationCidrBlock= route,
TransitGatewayRouteTableId=tgw_rt_id,
TransitGatewayAttachmentId=tgw_attach_id
)