AWS Lambda to Disable EC2 termination protection

Posted: September 25, 2018 in AWS, Scripts

This code will list all EC2 instances for every region and if termination protection is enabled, it will be disabled

import json
import boto3
def lambda_handler(event, context):
  client = boto3.client('ec2')
  ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
  for region in ec2_regions:
     client = boto3.client('ec2', region_name=region)
     conn = boto3.resource('ec2',region_name=region)
     instances = conn.instances.filter()
     for instance in instances:
       if instance.state["Name"] == "running":
       #print # , instance.instance_type, region)
        terminate_protection=client.describe_instance_attribute(InstanceId,Attribute = 'disableApiTermination')
        if protection_value == True:
          client.modify_instance_attribute(,Attribute="disableApiTermination",Value= "False" )
  1. AWS Training says:

    Nice! Thanks for sharing this information


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s