Launch an EC2 instance with boto3 and wait until it passes status checks, using a waiter.
IntermediateHands-onPython for DevOps
Answer
import boto3
ec2 = boto3.client("ec2")
resp = ec2.run_instances(ImageId="ami-0abcdef1234567890", InstanceType="t3.micro",
MinCount=1, MaxCount=1)
iid = resp["Instances"][0]["InstanceId"]
ec2.get_waiter("instance_status_ok").wait(
InstanceIds=[iid], WaiterConfig={"Delay": 15, "MaxAttempts": 40})
print(f"{iid} passed status checks")
Key trick → Waiters handle polling and timeouts for you. instance_running only means the state is running; instance_status_ok waits for the system and instance status checks to pass.
More on Python for DevOps
Write a Python script using boto3 that lists all running EC2 instances with their Name tag.BeginnerWrite a script that finds unattached EBS volumes and deletes them (with a dry-run flag).IntermediateCall a REST API with `requests`, handle timeouts and non-2xx responses properly.BeginnerAdd retry with exponential backoff to HTTP calls without writing the loop yourself.IntermediateWrite your own retry decorator with exponential backoff and jitter.AdvancedParse a YAML file and print every container image in a multi-document Kubernetes manifest.Intermediate