How to use attach_network_interface method in localstack

Best Python code snippet using localstack_python

aws.py

Source:aws.py Github

copy

Full Screen

...67 while get_network_interface_state(network_interface) != 'detached':68 time.sleep(1)69 except KeyError:70 pass71def attach_network_interface(network_interface, instance_id):72 client = boto3.client('ec2')73 for _ in xrange(10):74 try:75 client.attach_network_interface(76 NetworkInterfaceId=network_interface,77 InstanceId=instance_id,78 DeviceIndex=DEVICE_INDEX79 )80 except ClientError:81 time.sleep(1)82def configure_local_interface(local_interface, ip, netmask):83 cmd = [84 'ifconfig',85 local_interface,86 'inet',87 ip,88 'netmask',89 netmask90 ]91 for _ in xrange(10):92 os.environ['PATH'] = "%s:/sbin" % os.environ['PATH']93 env = os.environ94 proc = Popen(cmd, env=env)95 proc.communicate()96 if proc.returncode:97 time.sleep(1)98 else:99 return100def aws_notify_master(cfg):101 """The function moves network interface to local instance and brings it up.102 Steps:103 - Detach network interface if attached to anywhere.104 - Attach the network interface to the local instance.105 - Configure IP address on this instance106 :param cfg: config object107 """108 try:109 os.environ['AWS_ACCESS_KEY_ID'] = cfg.get('aws', 'aws_access_key_id')110 os.environ['AWS_SECRET_ACCESS_KEY'] = cfg.get('aws',111 'aws_secret_access_key')112 os.environ['AWS_DEFAULT_REGION'] = cfg.get('aws', 'aws_default_region')113 except NoOptionError:114 LOG.error('aws_access_key_id, aws_secret_access_key and '115 'aws_default_region must be defined in '116 'aws section of the config file.')117 exit(-1)118 instance_id = get_my_instance_id()119 try:120 ip = cfg.get('proxysql', 'virtual_ip')121 netmask = cfg.get('proxysql', 'virtual_netmask')122 network_interface = get_network_interface(ip)123 if network_interface_attached(network_interface):124 detach_network_interface(network_interface)125 local_interface = "eth%d" % DEVICE_INDEX126 ensure_local_interface_is_gone(local_interface)127 ensure_network_interface_is_detached(network_interface)128 attach_network_interface(network_interface, instance_id)129 configure_local_interface(local_interface, ip, netmask)130 except NoOptionError as err:131 LOG.error('virtual_ip and virtual_netmask must be defined in '132 'proxysql section of the config file.')...

Full Screen

Full Screen

ultimate_attach_lambda.py

Source:ultimate_attach_lambda.py Github

copy

Full Screen

...52 )53 eni=response['NetworkInterface']['NetworkInterfaceId']54 55 ##Attach ENI to instance56 response1 = ec2.attach_network_interface(57 DeviceIndex=1,58 InstanceId=EC2InstanceId,59 NetworkInterfaceId=eni60 )61 62 eni_attach_id=response1['AttachmentId']63 responsex = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id,'DeleteOnTermination': True},64NetworkInterfaceId=eni,)65 #print(response1)66 67 68 ##Associate free EIP to ENI69 70 response3 = ec2.associate_address(71 AllocationId=eip,72 NetworkInterfaceId=eni,73 )74 ##Create ENI75 response = ec2.create_network_interface(76 Description='PythonENILam2',77 Groups=[78 'sg-084d045efc41c9924',79 ],80 SubnetId=subnet2,81 )82 eni2=response['NetworkInterface']['NetworkInterfaceId']83 84 ##Attach ENI to instance85 response1 = ec2.attach_network_interface(86 DeviceIndex=2,87 InstanceId=EC2InstanceId,88 NetworkInterfaceId=eni289 )90 eni_attach_id2=response1['AttachmentId']91 responsex = ec2.modify_network_interface_attribute(Attachment={'AttachmentId': eni_attach_id2,'DeleteOnTermination': True},92NetworkInterfaceId=eni2,)93 94 autoscaling = boto3.client('autoscaling')95 response = autoscaling.complete_lifecycle_action(96 LifecycleHookName='LaunchLC',97 AutoScalingGroupName='MyASG',98 LifecycleActionResult='CONTINUE',99 InstanceId=EC2InstanceId...

Full Screen

Full Screen

ec2-attach-network-interface.py

Source:ec2-attach-network-interface.py Github

copy

Full Screen

1#!/usr/bin/python2import boto.ec23import os4import argparse5def attach_network_interface(ec2region,instance):6 conn = boto.ec2.connect_to_region(ec2region,7 aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),8 aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'))9 instance = conn.get_only_instances(instance_ids=[instance])[0]10 network_interface = conn.create_network_interface(instance.subnet_id,description="internal",groups=[ group.id for group in instance.groups])11 print 'created network interface: ',network_interface.id12 if network_interface.attach(instance.id,1)== False:13 raise Exception('could not attach network interface to instance');14 network_interface = conn.get_all_network_interfaces(network_interface_ids=[network_interface.id])[0]15 if conn.modify_network_interface_attribute(network_interface.id,'deleteOnTermination',True, attachment_id=network_interface.attachment.id) == False:16 raise Exception('could not update network interface delete on instance');17if __name__ == "__main__":18 parser = argparse.ArgumentParser('ec2-attach-network-interface')19 parser.add_argument('ec2region', action='store',nargs=1, help='ec2 region')20 parser.add_argument('ec2instance', action='store',nargs=1, help='ec2 instance id')21 args = parser.parse_args()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful