AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above. demonstrates how to terminate an Amazon EC2 instance.

# Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import logging import boto3 from botocore.exceptions import ClientError def terminate_instances(instance_ids): """Terminate one or more Amazon EC2 instances :param instance_ids: List of string IDs of EC2 instances to terminate :return: List of state information for each instance specified in instance_ids. If error, return None. """ # Terminate each instance in the argument list ec2 = boto3.client('ec2') try: states = ec2.terminate_instances(InstanceIds=instance_ids) except ClientError as e: logging.error(e) return None return states['TerminatingInstances'] def main(): """Exercise terminate_instances()""" # Assign these values before running the program ec2_instance_ids = ['EC2_INSTANCE_ID'] # Set up logging logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(asctime)s: %(message)s') # Terminate the EC2 instance(s) states = terminate_instances(ec2_instance_ids) if states is not None: logging.debug('Terminating the following EC2 instances') for state in states: logging.debug(f'ID: {state["InstanceId"]}') logging.debug(f' Current state: Code {state["CurrentState"]["Code"]}, ' f'{state["CurrentState"]["Name"]}') logging.debug(f' Previous state: Code {state["PreviousState"]["Code"]}, ' f'{state["PreviousState"]["Name"]}') if __name__ == '__main__': main()

Sample Details

Service: ec2

Last tested: 2019-2-11

Author: AWS

Type: full-example

On this page: