Best Python code snippet using localstack_python
key_pair.py
Source:key_pair.py
...4import boto35import boto3.ec26import os7app = None8def create_key_pair(key_name, key_dir='~/.ssh', region=None):9 client = boto3.client('ec2', region_name=region)10 key = client.create_key_pair(KeyName=key_name)11 if 'KeyMaterial' not in key:12 print('Key could not be created\n')13 raise14 else:15 fname = os.path.join(key_dir, '{}.pem'.format(key_name))16 with open(fname, 'w') as f:17 f.write(key['KeyMaterial'])18 return key19def describe_key_pairs():20 """21 Returns all key pairs for region22 """23 region_keys = {}24 for r in boto3.client('ec2', 'us-west-2').describe_regions()['Regions']:25 region = r['RegionName']26 client = boto3.client('ec2', region_name=region)27 try:28 pairs = client.describe_key_pairs()29 if pairs:30 region_keys[region] = pairs31 except Exception as e:32 app.logger.info(e)33 return region_keys34def run(dapp, cmd):35 global app36 app = dapp37 def cmd_describe_key_pairs():38 t = PrettyTable([39 'KeyName', 'KeyFingerprint', 'Region'40 ])41 for region, keys in describe_key_pairs().items():42 for pair in keys['KeyPairs']:43 t.add_row([pair['KeyName'], pair['KeyFingerprint'], region])44 print(t)45 def cmd_create_key_pair():46 parser = argparse.ArgumentParser()47 parser.add_argument(48 '--region', required=True, help='AWS Region')49 parser.add_argument('--key_name', required=True)50 parser.add_argument(51 '--key_dir', required=False, default='~/.ssh',52 help='Directory where key will be stored')53 args, extra_params = parser.parse_known_args()54 pair = create_key_pair(55 args.key_name, key_dir=args.key_dir, region=args.region)56 t = PrettyTable([57 'KeyName', 'KeyFingerprint', 'Region'58 ])59 t.add_row([pair['KeyName'], pair['KeyFingerprint'], args.region])60 print(t)61 actions = {62 'create_key_pair': cmd_create_key_pair,63 'describe_key_pairs': cmd_describe_key_pairs,64 }...
create_key_pair.py
Source:create_key_pair.py
1import os2import sys3import boto34ec2_client = boto3.client('ec2')5def create_key_pair(name="My_Test_KeyPair", outfile="My_Test_KeyPair.pem"):6 resp = ec2_client.create_key_pair(KeyName=name)7 key = resp['KeyMaterial']8 with open(outfile, 'w') as outf:9 outf.write(key)10 os.chmod(outfile, 0o400)11 print(f"Wrote key `{name}` to file `{outfile}`")12if __name__ == "__main__":13 if len(sys.argv) == 2:14 name = sys.argv[1]15 create_key_pair(name, f"{name}.pem")16 else:...
CreateKeyPair.py
Source:CreateKeyPair.py
1#Script to create KeyPair2import boto3, sys3def create_key_Pair(pairName, kType):4 ec2 = boto3.client('ec2')5 response = ec2.create_key_pair(6 KeyName = pairName,7 KeyType = kType8 )9 return response10pairName = sys.argv[1]11KeyName = create_key_Pair (pairName,'rsa')12secret = KeyName.get('KeyMaterial')13file = open('{}.pem'.format(pairName), 'w')14file.write(secret)...
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!!