AWS Lambda, Python : Call Shell script from Lambda or Linux command

0

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.

asked 5 years ago1579 views
1 Answer
0
Accepted Answer

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'
)

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions