Best Python code snippet using localstack_python
VPC 2 public and 2 private subnets.py
Source:VPC 2 public and 2 private subnets.py  
...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
...create_vpc.py
Source:create_vpc.py  
...28    routetable1 = vpc.create_route_table()29    route1 = routetable1.create_route(DestinationCidrBlock='0.0.0.0/0', GatewayId=internetgateway.id)30    routetag1 = routetable1.create_tags(Tags=[{"Key":"Name","Value":"public_route_table"}])31    subnet1 = ec2.create_subnet(AvailabilityZone="us-east-2a",CidrBlock=LLTD2, VpcId=vpc.id)32    ec2Client.associate_route_table(RouteTableId=routetable1.id,SubnetId=subnet1.id)33    subnet2 = ec2.create_subnet(AvailabilityZone="us-east-2a",CidrBlock=LLTD3, VpcId=vpc.id)34    subnet3 = ec2.create_subnet(AvailabilityZone="us-east-2b",CidrBlock=LLTD4, VpcId=vpc.id)35    subnet4 = ec2.create_subnet(AvailabilityZone="us-east-2b",CidrBlock=LLTD5, VpcId=vpc.id)36    ec2Client.associate_route_table(RouteTableId=routetable1.id,SubnetId=subnet4.id)37    ec2Client.modify_subnet_attribute(MapPublicIpOnLaunch={ 'Value': True},SubnetId=subnet1.id)38    ec2Client.modify_subnet_attribute(MapPublicIpOnLaunch={ 'Value': True},SubnetId=subnet4.id)39    subnet1tag = subnet1.create_tags(Tags=[{"Key":"Name","Value":"Public_Subnet1"}])40    subnet2tag = subnet2.create_tags(Tags=[{"Key":"Name","Value":"Private_Subnet1"}])41    subnet3tag = subnet3.create_tags(Tags=[{"Key":"Name","Value":"Private_Subnet2"}])42    subnet4tag = subnet4.create_tags(Tags=[{"Key":"Name","Value":"Public_Subnet2"}])43    elasticip1 = ec2Client.allocate_address()44    elasticip2 = ec2Client.allocate_address()45    natgateway1 = ec2Client.create_nat_gateway(AllocationId=elasticip1['AllocationId'],SubnetId=subnet1.id)46    natgateway2 = ec2Client.create_nat_gateway(AllocationId=elasticip2['AllocationId'],SubnetId=subnet4.id)47    time.sleep(20)48    routetable2 = vpc.create_route_table()49    route2 = routetable2.create_route(DestinationCidrBlock='0.0.0.0/0', NatGatewayId=natgateway1['NatGateway']['NatGatewayId'])50    routetag2 = routetable2.create_tags(Tags=[{"Key":"Name","Value":"private_route_table1"}])51    ec2Client.associate_route_table(RouteTableId=routetable2.id,SubnetId=subnet2.id)52    routetable3 = vpc.create_route_table()53    route3 = routetable3.create_route(DestinationCidrBlock='0.0.0.0/0', NatGatewayId=natgateway2['NatGateway']['NatGatewayId'])54    routetag3 = routetable3.create_tags(Tags=[{"Key":"Name","Value":"private_route_table2"}])  55    ec2Client.associate_route_table(RouteTableId=routetable3.id,SubnetId=subnet3.id)56    ...main.py
Source:main.py  
...30sg_private = create_security_group(client, vpc, 'Analitics_RDS_SG_Iaroslav_Kudrin', 'sg for access to ms sql server from ec2 instances', ingress_permissions_private, egress_permissions_private)31sg_public = create_security_group(client, vpc, 'Analitics_EC2_SG_Iaroslav_Kudrin', 'sg for access to ec2 from sql instances and from rdp', ingress_permissions_public, egress_permissions_public)32# Create Route Tables - Not Ready yet33route_table_id = create_route_table(client, vpc)34associate_route_table(client, route_table_id, subnet_id_public)35associate_route_table(client, route_table_id, subnet_id_private_1)36associate_route_table(client, route_table_id, subnet_id_private_2)37create_route(client, route_table_id, gateway_id, '0.0.0.0/0')38# Create NACL39# Assossiate NACLs with subnets40# Create NACL Rules41# create_nacl_rule('192.168.1.0/25', False, nacl_id_private, 1433, 1433, '6', 'allow', 100)42# create_nacl_rule('192.168.1.0/25', True, nacl_id_private, 1024, 65535, '6', 'allow', 100)43# create_nacl_rule('0.0.0.0/0', False, nacl_id_public, 1, 65535, '-1', 'allow', 100)44# create_nacl_rule('0.0.0.0/0', True, nacl_id_public, 1, 65535, '-1', 'allow', 100)45# Create RDS46create_db_subnet_group(rds, subnet_id_private_1, subnet_id_private_2)47create_rds(rds, sg_private)48print('subnet_id=', subnet_id_public)...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!!
