Complete the code to check if an AWS EC2 instance is currently running.
if instance.state['Name'] == '[1]': print('Instance is running')
The instance state 'running' means the EC2 instance is active and operational.
Complete the code to stop an AWS EC2 instance using boto3.
response = ec2_client.stop_instances(InstanceIds=[[1]])The InstanceIds parameter requires a list of strings representing instance IDs. The ID must be a string inside quotes.
Fix the error in the code that checks if an instance is terminated.
if instance.state['[1]'] == 'terminated': print('Instance is terminated')
The correct key to access the state name in the instance state dictionary is 'Name' with a capital N.
Fill both blanks to create a dictionary comprehension that maps instance IDs to their states for instances that are running.
running_instances = {inst['[1]']: inst['state']['[2]'] for inst in instances if inst['state']['Name'] == 'running'}'InstanceId' is the key for the instance ID, and 'Name' is the key for the state name inside the state dictionary.
Fill all three blanks to filter instances that are stopped and create a list of their IDs.
stopped_ids = [inst['[1]'] for inst in instances if inst['state']['[2]'] == '[3]']
We use 'InstanceId' to get the ID, 'Name' to access the state name, and 'stopped' to filter stopped instances.