Instance termination

The printed ID is used in CRUD operations to manage or terminate the instance later. For example, we can terminate the instance by using the terminate() method also provided to the ec2 resource created earlier:

import boto3ec2 = boto3.resource('ec2')instance_id = "i-0a81k3ndl29175220"instance = ec2.Instance(instance_id)instance.terminate()

Notice that we hardcoded instance_id in the preceding code (which is not always the case when you need to create a dynamic Python script that can be used in different environments). We can use other input methods that are available in Python, such as raw_input(), to take the input from the user or query the available instances in our accounts and make Python prompt us on which instances ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.