AWS Lambda、Python:从 Lambda 或 Linux 命令调用 Shell 脚本

0

【以下的问题经过翻译处理】 我正在编写一个用 Python 编写的 AWS Lambda 脚本,目前正在获取带有特定标签的所有实例,并从中删除最旧的一个。之后,我想从剩余的实例中调用一个 Linux 命令。我唯一需要的是调用"crontab -r",因为最旧的实例将具有cron设置,并且在ASG生成的实例中添加这些cron将导致发送重复的电子邮件。

我已经完成了获取除最旧实例之外的所有实例的部分,但如何在这些实例上调用"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.
profile picture
EXPERT
asked 5 months ago29 views
1 Answer
0

【以下的回答经过翻译处理】 使用 boto3 send_command 在 ec2 上执行命令。

举例说明:

boto3.client('ssm').send_command(
InstanceIds=[val.id],
DocumentName='AWS-RunShellScript',
Parameters={'commands': ['crontab -r']},
Comment='Crontab remove'
)
profile picture
EXPERT
answered 5 months 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