3

I am working on an AWS Lambda script written in Python where I am currently getting all the instances with specific tags, and removing the oldest one from them. After that, from the remaining instances, I would like to call a linux command on the instances. The only thing I require is to call crontab -r , as the oldest instance will have the cron set, and adding those crons in the ASG generated instances will cause duplicate emails being sent.

I am done till the part of getting all the instances except the oldest one, but how can I call crontab -r on each of those instances? Any ideas. Thank you.

Code :

import boto.ec2
import boto3
conn=boto.ec2.connect_to_region("eu-central-1")
reservations = conn.get_all_instances()
instances_list = []
process_instance_list = []
for res in reservations:
    for inst in res.instances:
        if 'Name' in inst.tags:
            if inst.tags['Name'] == 'PROJECT_NAME' :
               instances_list.append(inst);


instances_list.sort(key=lambda x: x.launch_time, reverse=False)
non_processed_id=instances_list[0]

for val in instances_list:
    if val.id != non_processed_id.id:
       // Call crontab -r here.

Thank you. :-)

2

1 Answer 1

3

Use boto3 send_command to execute a command on ec2.

Example for your case:

boto3.client('ssm').send_command(
    InstanceIds=[val.id], 
    DocumentName='AWS-RunShellScript', 
    Parameters={'commands': ['crontab -r']}, 
    Comment='Crontab remove'
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.