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. :-)
-
1stackoverflow.com/questions/44143310/…error404– error4042019年06月28日 15:31:04 +00:00Commented Jun 28, 2019 at 15:31
-
1Ideally use EC2 Run Command via boto3. Or use Paramiko.jarmod– jarmod2019年06月28日 16:51:31 +00:00Commented Jun 28, 2019 at 16:51
1 Answer 1
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'
)
Comments
Explore related questions
See similar questions with these tags.