Enable/Disable Termination Protection in EC2 For 100 Instances / Blogs / Perficient


Manually enable/disable for 100 Instances is a difficult task. To overcome this task we have some automated processes.

It’s possible to automate the process in one short using Python SDK.

List of Ec2 instances:

Python Code:

import boto3

 

def disable_termination_protection(instance_id):
    ec2 = boto3.client('ec2')
    response = ec2.modify_instance_attribute(
        InstanceId=instance_id,
        DisableApiTermination={
            'Value': False
        }
    )
    print(f"Termination protection disabled for instance {instance_id}")

 

# Get running instances
ec2 = boto3.client('ec2')
response = ec2.describe_instances(
    Filters=[
        {
            'Name': 'instance-state-name',
            'Values': ['running']
        }
    ]
)

 
# Disable termination protection for each running instance
for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        instance_id = instance['InstanceId']
        disable_termination_protection(instance_id)

The above code you will find the running instance and disable the instance termination.

After executing we can check this manually for any random 5 instances.

Happy learning!!





Source link

Social media & sharing icons powered by UltimatelySocial
error

Enjoy Our Website? Please share :) Thank you!