Best Python code snippet using localstack_python
imdsv2_set.py
Source:imdsv2_set.py
...11 instance_metadata_options = instance["MetadataOptions"]["HttpTokens"]12 instance_id = instance["InstanceId"]13 if instance_metadata_options == "required":14 try:15 response = ec2_client.modify_instance_metadata_options(InstanceId=instance_id,HttpTokens='optional')16 print(f"Updated instance to not require HTTP Tokens: {instance_id}")17 except Exception as e:18 return f"The following exception occured: {e}"19 elif instance_metadata_options == "optional":20 print(f"Instance is already not required to use HTTP Tokens: {instance_id}")21def update_imdsv2(reservations):22 instance_tags = []23 for reservation in reservations:24 is_ecs = False25 for instance in reservation["Instances"]: 26 instance_metadata_options = instance["MetadataOptions"]["HttpTokens"]27 instance_id = instance["InstanceId"]28 if "Tags" in instance:29 instance_tags = instance["Tags"]30 for tag in instance_tags:31 if "Name" in tag["Key"]:32 if "ecs" in tag["Value"].lower():33 is_ecs = True34 if instance_metadata_options == "optional":35 if is_ecs:36 print(f"Skipping ECS Instance {instance_id}")37 continue38 try:39 response = ec2_client.modify_instance_metadata_options(InstanceId=instance_id,HttpTokens='required')40 print(f"Updated instance to use IMDSv2: {instance_id}")41 except Exception as e:42 return f"The following exception occured: {e}"43 elif instance_metadata_options == "required":44 print(f"Instance is already requiring IMDSv2: {instance_id}")45def lambda_handler(event, context):46 47 instances = list_instances()48 update_imdsv2(instances)...
modify.py
Source:modify.py
...30 )31 logging.info(f"imds_updated_dry_run,{action},{instance_id},{status_text}")32 else:33 try:34 response = ec2_client.modify_instance_metadata_options(35 InstanceId=instance_id,36 HttpTokens=httptoken,37 HttpEndpoint=status,38 )39 status_color = convert_green("SUCCESS")40 status_text = "SUCCESS"41 except:42 status_color = convert_red("FAILED")43 status_text = "FAILED"44 print(45 f"IMDS updated : {action} in {region} for {instance_id:<80} {status_color:>20}"46 )...
enforce_ec2_metadata_v2.py
Source:enforce_ec2_metadata_v2.py
...9# This section will update the EC2 Settings to Enforce using V2 for Metadata service i.e10# https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-standards-fsbp-controls.html#ec2-8-remediation11print(instanceList)12for instance in instanceList:13 response = ec2_client.modify_instance_metadata_options(14 InstanceId=instance,15 HttpTokens='required',16 HttpEndpoint='enabled',17 DryRun=False18 )...
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!!