Best Python code snippet using localstack_python
VPC 2 public and 2 private subnets.py
Source:VPC 2 public and 2 private subnets.py  
...79    components_id['nat_id'] = nat_id  # inserting the id to the id dict80    client.create_tags(Resources=[nat_id], Tags=[{'Key': 'Name', 'Value': name}])818283def create_route_table(name):84    """creating a route table in the vpc"""8586    route = client.create_route_table(VpcId=components_id['vpc_id'])87    time.sleep(5)88    route_id = route['RouteTable']['RouteTableId']89    components_id['route_id'] = route_id  # inserting the id to the id dict90    client.create_tags(Resources=[route_id], Tags=[{'Key': 'Name', 'Value': name}])919293def create_route(Nat_or_igw, nat_id=0):94    """95    creating a route to the internet from NAT gateway or IGW96    :param Nat_or_igw: is the route going to be through NAT or internet gateway97    :param nat_id: the NAT id98    """99    if Nat_or_igw == 'igw':100        client.create_route(101            DestinationCidrBlock='0.0.0.0/0',102            GatewayId=components_id['igw_id'],103            RouteTableId=components_id['route_id'])104    elif Nat_or_igw == 'NAT':105        client.create_route(106            DestinationCidrBlock='0.0.0.0/0',107            NatGatewayId=nat_id,108            RouteTableId=components_id['route_id'])109110111def associate_route_table(subnet_id):112    """associating a subnet with a specific route table"""113114    client.associate_route_table(115        RouteTableId=components_id['route_id'],116        SubnetId=subnet_id)117118119def main():120    """121    This is where the magic happens, the things that will be created are:122    1 main vpc, 4 subnets(2 public and 2 private), 3 route tables(1 public through igw123    and 2 private through NAT gateway), 1 internet gateway and 2 NAT gateways.124    the public subnet will be connected to the internet through igw and the private through NAT gateway.125    this main function is written to create this specific infrastructure but you can use the functions above126    and create whatever infrastructure you desire, have fun!127    """128129    create_vpc('main_vpc')130    create_internet_gateway('main_igw')131    # create 4 subnets in 2 different AZ for high availability132    # type 'yes' in the last argument if the subnet is public and 'no' if its private133    create_subnet('us-east-1a', 'public_sub1', '10.0.1.0/24', 'yes')134    create_subnet('us-east-1b', 'public_sub2', '10.0.2.0/24', 'yes')135    create_subnet('us-east-1a', 'private_sub1', '10.0.3.0/24', 'no')136    create_subnet('us-east-1b', 'private_sub2', '10.0.4.0/24', 'no')137138    #  if the route table is going to be connected to igw insert in the create route func 'igw'139    #  if the route table is going to be connected to NAT gateway insert in the create route func 'NAT'140    create_route_table('public_route_table')141    create_route('igw')142    associate_route_table(public_subnets_id[0])143    associate_route_table(public_subnets_id[1])144145    # create 2 NAT gateways in 2 different AZ for high availability146    create_NAT_gateway(public_subnets_id[0], 'NAT1test')  # first NAT gateway147148    create_route_table('private_route_table 1test')149    create_route('NAT', components_id['nat_id'])  # second argument is the NAT id that just have been created150    associate_route_table(private_subnets_id[0])151152    create_NAT_gateway(public_subnets_id[1], 'NAT2test')  # second NAT gateway153154    create_route_table('private_route_table 2test')155    create_route('NAT', components_id['nat_id'])  # second argument is the NAT id that just have been created156    associate_route_table(private_subnets_id[1])157158159if __name__ == '__main__':160    main()161
...task.py
Source:task.py  
...4    parser = ArgumentParser()5    parser.add_argument('-f', '--first_id', type=str, required=True)6    parser.add_argument('-s', '--second_id', type=str, required=True)7    return parser.parse_args()8def create_route_table(ec2_client, name):9    response = ec2_client.create_route_table(VpcId='VPC_ID')10    route_table = response.get("RouteTable")11    route_table_id = route_table.get("RouteTableId")12    ec2_client.create_tags(13        Resources=[route_table_id],14        Tags=[{"Key": "Name", "Value": name}],15    )16    return route_table_id17def attach_route_table_to_subnet(ec2_client, route_table_id, subnet_id):18    ec2_client.associate_route_table(19       RouteTableId=route_table_id,20       SubnetId=subnet_id21    )22def main():23    args = parse_args()24    ec2_client = boto3.client('ec2')25    first_route_table_id = create_route_table(ec2_client, 'public-rtb-1')26    second_route_table_id = create_route_table(ec2_client, 'private-rtb-1')27    attach_route_table_to_subnet(ec2_client, first_route_table_id, args.first_id)28    attach_route_table_to_subnet(ec2_client, second_route_table_id, args.second_id)29if __name__ == '__main__':...advanced_vpc.py
Source:advanced_vpc.py  
...11    print "Creating Internet Gateway and attach to above VPC..."12    create_internet_gw = ec2.create_internet_gateway()13    create_internet_gw.attach_to_vpc(VpcId=create_vpc.id)14    print "Creating Router and add the gateway..."15    create_route_table= create_vpc.create_route_table()16    create_route = create_route_table.create_route(17        DestinationCidrBlock='0.0.0.0/0',18        GatewayId=create_internet_gw.id19    )20    21    return  create_vpc.id,create_subnet.id, create_internet_gw.id, create_route_table.id...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!!
