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.

gefragt vor 5 Jahren1598 Aufrufe
1 Antwort
0
Akzeptierte Antwort

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

beantwortet vor 5 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen