Python批量开关AWS EC2脚本

原创 server  2024-09-02 16:33  阅读 18 次

前提条件:

  • 需要本机配置AWS CLI的AK/SK;
  • 本机安装Python3;
  • 本机安装boto3库(执行pip3 install boto3)。

Python脚本如下:

import boto3.ec2
client = boto3.client('ec2')
ec2action = boto3.resource('ec2')

response = client.describe_instance_status(
    IncludeAllInstances=True
)

for i in response['InstanceStatuses']:
    instance = ec2action.Instance(i['InstanceId'])
    response1 = instance.stop(
            #DryRun=True,
            Force=True
    )
    print("instance: ", i['InstanceId'], "is being stopped." )
    print(response1)

以上为关机脚本。如果需要批量开机,替换stop为start即可。

本文地址:https://www.91hosts.com/aws/872.html
关注我们:请关注一下我们的微信公众号:扫描二维码谷歌云GCP/亚马逊AWS/微软云AZURE/阿里云国际代理商-91hosts的公众号
版权声明:本文为原创文章,版权归 server 所有,欢迎分享本文,转载请保留出处!

评论已关闭!