如何在Python脚本中运行aws schedulercli?

2024-10-02 22:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Python脚本运行AWS Instance scheduler的scheduler cli。我试着通过boto3来做,但可能在那里不支持。由于我能够通过windows命令提示符运行scheduler cli,因此尝试使用subprocess从python脚本运行scheduler cli时出错。以下是代码段和错误:

                cmd='scheduler-cli create-period --name "testperiod" --begintime 07:00 --endtime 20:00 --weekdays mon-fri --stack instance-scheduler --description "7 AM to 8 PM Monday to Friday" --region ap-southeast-2'
                try:
                subprocess.check_output(cmd, shell=True).decode()
                except Exception as e:
                    print(e)
                    print("Could not create period.")

我得到的错误如下:

subprocess.CalledProcessError: Command 'scheduler-cli create-period --name "testperiod" --begintime 07:00 --endtime 20:00 --weekdays mon-fri --stack instance-scheduler --description "7 AM to 8 PM Monday to Friday" --region ap-southeast-2' returned non-zero exit status 1.

命令在windows shell中运行良好。可能我需要在脚本中以某种方式提供aws凭据,但是由于我正在迭代不同的帐户,所以需要提供不同的sts凭据,但不确定如何为子进程设置这些凭据。你知道吗

谢谢。你知道吗


Tags: toname脚本cmdcliwindows错误create
1条回答
网友
1楼 · 发布于 2024-10-02 22:36:12

我可以通过从操作系统包运行scheduler cli命令来解决这个问题,因为AWS的API还不可用。你知道吗

           cmdschedule='scheduler-cli create-schedule  name testschedule  periods testperiod  timezone Australia/Sydney  stack teststack  description "6 AM to 8 PM Monday to Sunday"  region ap-southeast-2'
            try:
                myCmdperiod = os.popen(cmdperiod).read()

相关问题 更多 >