Python Forum
lambda-Amazon EC2:Remove instance termination protection if enabled and terminate ins
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lambda-Amazon EC2:Remove instance termination protection if enabled and terminate ins
#1
I have following task:

1.Check if the instance has a tag "Terminate_On"

If yes:

2.Check if the instance should be terminated today

If yes:

3.Remove instance termination protection if enabled Terminate the instance

I'm stuck on part 3: don't know how to remove protection if enabled and to terminate instance


import boto3   
import collections     
import datetime     
import time     
import sys 

ec = boto3.client('ec2', 'eu-west-1')     
ec2 = boto3.resource('ec2', 'eu-west-1')     
from datetime import datetime
from dateutil.relativedelta import relativedelta

date_after_month = datetime.now()+ relativedelta(days=7)
#print date_after_month.strftime('%d/%m/%Y')
today=datetime.now().strftime('%d/%m/%Y')


def lambda_handler(event, context):           
    instance_ids = []
    reservations = ec.describe_instances(     
        Filters=[     
            {'Name': 'tag:Owner', 'Values': ['Unknown', 'unknown']},     
        ]     
    ).get('Reservations', []) 

    for reservation in reservations:
          instances = reservation['Instances']
          for instance in instances:
              instance_ids.append(instance['InstanceId'])
              tags = {}
              for tag in instance['Tags']:
                tags[tag['Key']] = tag['Value']  
                if 'TerminateOn' in tags:  
                  if tags["TerminateOn"]==today:
                    #remove termination protection if enabled
                    #terminate instance
                  else: 
                    print "No need for termination"
                if not 'TerminateOn' in tags:  
                     ec2.create_tags(Resources=instance_ids,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])



print("Stopping instances: {}".format(','.join(instance_ids)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected termination of program when using JDBC drivers in python with jaydebeapi skarface19 2 312 Feb-17-2024, 12:01 PM
Last Post: skarface19
  Keep trying link until enabled kamaleon 4 1,387 Jul-30-2022, 07:11 AM
Last Post: kamaleon
  Delete files from amazon s3 bucket python_student 0 3,918 Jan-14-2022, 04:51 PM
Last Post: python_student
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,451 Jul-22-2021, 03:24 AM
Last Post: rango
  Password Protection to the file San 0 1,424 Jun-09-2021, 02:00 PM
Last Post: San
  Is there a way to not terminate a running thread in python Contra_Boy 3 1,970 May-05-2020, 09:38 PM
Last Post: SheeppOSU
  Terminate a process when hotkey is pressed 66Gramms 0 2,240 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  How to terminate the caller fuction through callee? 100k 2 2,143 Nov-27-2019, 06:49 PM
Last Post: jefsummers
  boto3 Amazon SQS raghuvamsi59 1 1,538 Oct-01-2019, 04:32 AM
Last Post: raghuvamsi59
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,910 Sep-22-2019, 05:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020