Best Python code snippet using localstack_python
transit-gateway.py
Source:transit-gateway.py  
...86        DryRun=False87    )88    return response89#Associate transit gateway route table90def associate_transit_gateway_route_table(tgw_rt_id, tgw_attach_id):91    client = boto3.client('ec2', region_name='us-east-1')92    response = client.associate_transit_gateway_route_table(93        TransitGatewayRouteTableId=tgw_rt_id,94        TransitGatewayAttachmentId=tgw_attach_id,95        DryRun=False96    )97    return response98#Enable transit gateway route table propogation99def enable_transit_gateway_route_table_propagation(tgw_rt_id, tgw_att_id):100    client = boto3.client('ec2', region_name='us-east-1')101    response = client.enable_transit_gateway_route_table_propagation(102        TransitGatewayRouteTableId=tgw_rt_id,103        TransitGatewayAttachmentId= tgw_att_id,104        DryRun=False105    )106    return response107#Create transit gateway multi-cast domain108def create_transit_gateway_multicast_domain(transit_gateway_id):109    client = boto3.client('ec2', region_name='us-east-1')110    response = client.create_transit_gateway_multicast_domain(111        TransitGatewayId=transit_gateway_id,112        TagSpecifications=[113            {114                'ResourceType':  'transit-gateway-multicast-domain',115                'Tags': [116                    {117                        'Key': 'Name',118                        'Value': 'aquis-terraform-boto3-tgw-multicast-domain'119                    },120                ]121            },122        ],123        DryRun=False124    )125    return response126#Associate transit gateway multi cast domain127def associate_transit_gateway_multicast_domain(tgw_multicast_domain_id, tgw_attachment_id, subnetID):128    client = boto3.client('ec2', region_name='us-east-1')129    response = client.associate_transit_gateway_multicast_domain(130    TransitGatewayMulticastDomainId=tgw_multicast_domain_id,131    TransitGatewayAttachmentId=tgw_attachment_id,132    SubnetIds=[133        subnetID,134    ],135    DryRun=False136    )137    return response138#Delete transit gateway multi-cast domain139def delete_transit_gateway_multicast_domain(tgwy_multicast_domain_id):140    client = boto3.client('ec2', region_name='us-east-1')141    response = client.delete_transit_gateway_multicast_domain(142        TransitGatewayMulticastDomainId=tgwy_multicast_domain_id,143        DryRun=False144    )145    return response146#Delete transit gateway vpc attachment147def delete_transit_gateway_vpc_attachment(transit_gateway_attachment_id):148    client = boto3.client('ec2', region_name='us-east-1')149    response = client.delete_transit_gateway_vpc_attachment(150        TransitGatewayAttachmentId=transit_gateway_attachment_id,151        DryRun=False152    )153    return response154#Delete transit gateway155def delete_transit_gateway(transit_gateway_id):156    client = boto3.client('ec2', region_name='us-east-1')157    response = client.delete_transit_gateway(158        TransitGatewayId=transit_gateway_id,159        DryRun=False160    )161    return response162# dissacotiate transit gateway multicast domain163def disassociate_transit_gateway_multicast_domain(tgw_multicast_domain_id, tgw_attachment_id, subnet_id):164    client = boto3.client('ec2', region_name='us-east-1')165    response = client.disassociate_transit_gateway_multicast_domain(166        TransitGatewayMulticastDomainId=tgw_multicast_domain_id,167        TransitGatewayAttachmentId=tgw_attachment_id,168        SubnetIds=[169            subnet_id,170        ],171        DryRun=False172    )173    return response174#register transit gateway multi-cast members175def register_transit_gateway_multicast_group_members(tgw_multicast_domain_id, group_address, network_interface3, network_interface4):176    client = boto3.client('ec2', region_name='us-east-1')177    response = client.register_transit_gateway_multicast_group_members(178    TransitGatewayMulticastDomainId = tgw_multicast_domain_id,179    GroupIpAddress = group_address,180    NetworkInterfaceIds = [181                            network_interface3, network_interface4,182                          ],183    DryRun = False184    )185    return response186#register transit gateway multi cast group sources187def register_transit_gateway_multicast_group_sources(tgw_multicast_domain_id, nic1id, group_address):188    client = boto3.client('ec2', region_name='us-east-1')189    response = client.register_transit_gateway_multicast_group_sources(190        TransitGatewayMulticastDomainId=tgw_multicast_domain_id,191        GroupIpAddress=group_address,192        NetworkInterfaceIds=[193            nic1id,194        ],195        DryRun=False196    )197    return response198#Delete transit gateway route table199def delete_transit_gateway_route_table(rtb_id):200    client = boto3.client('ec2', region_name='us-east-1')201    response = client.delete_transit_gateway_route_table(202        TransitGatewayRouteTableId=rtb_id,203        DryRun=False204    )205    return response206if __name__ == '__main__':207    action = sys.argv[1]208    if action == "apply":209        #Get values from python input when calling python and assign to variables using sys.argv210        vpc_id = sys.argv[2]211        subnet_id = sys.argv[3]212        processor1nic = sys.argv[4]213        processor2nic = sys.argv[5]214        processor3nic = sys.argv[6]215        #Create transit gateway216        tgw_response = create_transit_gateway()217        #Collect transit gateway id from response218        tgw_id = tgw_response['TransitGateway']['TransitGatewayId']219        time.sleep(120)220        #Create transit gateway attachment221        tgw_attachment_response = create_transit_gateway_vpc_attachment(tgw_id, vpc_id, subnet_id)222        #get tgw attachment id response223        tgw_attachment_id = tgw_attachment_response['TransitGatewayVpcAttachment']['TransitGatewayAttachmentId']224        time.sleep(90)225        #Create transit gateway route table and get response226        tgw_rt_response = create_transit_gateway_rt(tgw_id)227        #Get transit gateway route table id from the response228        tgw_rt_id = tgw_rt_response ['TransitGatewayRouteTable']['TransitGatewayRouteTableId']229        time.sleep(90)230        #associate transit gateway route table231        associate_transit_gateway_route_table(tgw_rt_id,tgw_attachment_id)232        time.sleep(60)233        #enable transit gateway route table propogation234        enable_transit_gateway_route_table_propagation(tgw_rt_id, tgw_attachment_id)235        time.sleep(30)236        #Create transit gateway route237        create_transit_gateway_route(tgw_rt_id, "10.123.112.0/24", tgw_attachment_id)238        time.sleep(60)239        #Create transit gateway multi cast domain240        tgw_multicast_domain_response = create_transit_gateway_multicast_domain(tgw_id)241        #Get transit gateway multi cast domain id242        tgw_multicast_domain_id = tgw_multicast_domain_response['TransitGatewayMulticastDomain']['TransitGatewayMulticastDomainId']243        time.sleep(90)244        #Associate transit gateway multi-cast domain245        associate_transit_gateway_multicast_domain(tgw_multicast_domain_id, tgw_attachment_id, subnet_id)...state_machine_router.py
Source:state_machine_router.py  
...33        response = tgw.tgw_attachment_crud_operations()34    elif function_name == 'describe_transit_gateway_route_tables':35        response = tgw.describe_transit_gateway_route_tables()36    elif function_name == 'disassociate_transit_gateway_route_table':37        response = tgw.disassociate_transit_gateway_route_table()38    elif function_name == 'associate_transit_gateway_route_table':39        response = tgw.associate_transit_gateway_route_table()40    elif function_name == 'get_transit_gateway_attachment_propagations':41        response = tgw.get_transit_gateway_attachment_propagations()42    elif function_name == 'enable_transit_gateway_route_table_propagation':43        response = tgw.enable_transit_gateway_route_table_propagation()44    elif function_name == 'disable_transit_gateway_route_table_propagation':45        response = tgw.disable_transit_gateway_route_table_propagation()46    elif function_name == 'get_transit_gateway_vpc_attachment_state':47        response = tgw.get_transit_gateway_vpc_attachment_state()48    else:49        message = "Function name does not match any function in the handler file."50        logger.info(message)51        return {"Message": message}52    logger.info(response)53    return response...tgw_routing_automation.py
Source:tgw_routing_automation.py  
...31        ssm_client = boto3.client('ssm', config=boto3_config)32        tgw_hub_route_table = ssm_client.get_parameter(Name="/networking/{}/route-table/hub".format(AWS_REGION))['Parameter']['Value']33        ec2_client = boto3.client('ec2', config=boto3_config)34        if request_type == 'Create':35            ec2_client.associate_transit_gateway_route_table(36                TransitGatewayRouteTableId=route_table_id,37                TransitGatewayAttachmentId=tgw_attachment_id38            )39            ec2_client.enable_transit_gateway_route_table_propagation(40                TransitGatewayRouteTableId=tgw_hub_route_table,41                TransitGatewayAttachmentId=tgw_attachment_id42            )43            send(msg, context, Status.SUCCESS, {}, physical_resource_id=str(uuid.uuid4()))44        if request_type == 'Delete':45            ec2_client.disassociate_transit_gateway_route_table(46                TransitGatewayRouteTableId=route_table_id,47                TransitGatewayAttachmentId=tgw_attachment_id48            )49            ec2_client.disable_transit_gateway_route_table_propagation(50                TransitGatewayRouteTableId=tgw_hub_route_table,51                TransitGatewayAttachmentId=tgw_attachment_id52            )53            send(msg, context, Status.SUCCESS, {}, physical_resource_id=str(uuid.uuid4()))54        if request_type == 'Update':55            send(msg, context, Status.FAILED, {}, reason='Update on this resource is not supported')56    except Exception as e:57        print(str(e))58        send(msg, context, Status.FAILED, {}, reason=str(e))59        raise(e)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
