0
0
AWScloud~10 mins

Instance states (running, stopped, terminated) in AWS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if an AWS EC2 instance is currently running.

AWS
if instance.state['Name'] == '[1]':
    print('Instance is running')
Drag options to blanks, or click blank then click option'
Aterminated
Bstopped
Crunning
Dpending
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stopped' instead of 'running' will check for a stopped instance, not a running one.
Using 'terminated' will check for an instance that is deleted, not active.
2fill in blank
medium

Complete the code to stop an AWS EC2 instance using boto3.

AWS
response = ec2_client.stop_instances(InstanceIds=[[1]])
Drag options to blanks, or click blank then click option'
A'i-1234567890abcdef0'
Bi-1234567890abcdef0
C'instance_id'
Dinstance_id
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the instance ID string causes a syntax error.
Using a variable name without quotes when a string literal is expected.
3fill in blank
hard

Fix the error in the code that checks if an instance is terminated.

AWS
if instance.state['[1]'] == 'terminated':
    print('Instance is terminated')
Drag options to blanks, or click blank then click option'
Aname
Bstatus
Cstate
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'name' causes a KeyError.
Using 'state' or 'status' keys which do not exist in the state dictionary.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps instance IDs to their states for instances that are running.

AWS
running_instances = {inst['[1]']: inst['state']['[2]'] for inst in instances if inst['state']['Name'] == 'running'}
Drag options to blanks, or click blank then click option'
AInstanceId
BName
CState
DStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect keys like 'instanceid' or 'state' causes errors.
Confusing the keys for instance ID and state name.
5fill in blank
hard

Fill all three blanks to filter instances that are stopped and create a list of their IDs.

AWS
stopped_ids = [inst['[1]'] for inst in instances if inst['state']['[2]'] == '[3]']
Drag options to blanks, or click blank then click option'
AInstanceId
BName
Cstopped
DState
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'state' or 'status' instead of 'Name'.
Using 'stopping' or 'terminated' instead of 'stopped' to filter.