[英]AWS Lambda, Python : Call Shell script from Lambda or Linux command
我正在使用Python编写的AWS Lambda脚本,我目前正在获取具有特定标记的所有实例,并从中删除最旧的实例。 之后,从剩下的实例中,我想在实例上调用linux命令。 我唯一需要的是调用crontab -r
,因为最老的实例将具有cron设置,并且在ASG生成的实例中添加这些crons将导致重复的电子邮件被发送。
除了最旧的实例之外,我已经完成所有实例,但是如何在每个实例上调用crontab -r
? 有任何想法吗。 谢谢。
代码:
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.
谢谢。 :-)
使用boto3 send_command在ec2上执行命令。
案例示例:
boto3.client('ssm').send_command(
InstanceIds=[val.id],
DocumentName='AWS-RunShellScript',
Parameters={'commands': ['crontab -r']},
Comment='Crontab remove'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.