Amazon web services 如何自动停止和启动AWS EC2实例

Amazon web services 如何自动停止和启动AWS EC2实例,amazon-web-services,amazon-ec2,instance,Amazon Web Services,Amazon Ec2,Instance,我是AWS的初学者 我只想自动和定期地停止和启动几个EC2实例(而不是重新启动) 有什么推荐的方法可以做到这一点吗?是的,您可以使用AWS Lambda做到这一点。您可以在Cloudwatch中选择触发器,该触发器在UTC上的Cron表达式上运行 这里是一个相关链接 另一种选择是使用awscli,它可从pip、apt get、yum或brew,然后使用IAM提供的凭据运行aws configure,并执行以下bash脚本,停止已标记有Name:Appname和Value:Appname Prod

我是AWS的初学者

我只想自动和定期地停止和启动几个EC2实例(而不是重新启动)


有什么推荐的方法可以做到这一点吗?

是的,您可以使用AWS Lambda做到这一点。您可以在Cloudwatch中选择触发器,该触发器在UTC上的Cron表达式上运行

这里是一个相关链接

另一种选择是使用
awscli
,它可从
pip
apt get
yum
brew
,然后使用IAM提供的凭据运行
aws configure
,并执行以下bash脚本,停止已标记有
Name:Appname
Value:Appname Prod
的EC2。您可以使用
awscli
标记实例或从AWS控制台手动标记实例
aws ec2 stop instances
将停止实例,
jq
用于过滤json查询,并使用
aws ec2 Descripte instances
中的标记获取正确的实例id

要验证
aws configure
是否成功并返回json输出,请运行
aws ec2 descripe instances
,并且您的运行实例id应该在输出中。下面是一个示例输出

{
    "Reservations": [
        {
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "PublicDnsName": "ec2-xxx.ap-south-1.compute.amazonaws.com",
                    "State": {
                        "Code": xx,
                        "Name": "running"
                    },
                    "EbsOptimized": false,
                    "LaunchTime": "20xx-xx-xxTxx:16:xx.000Z",
                    "PublicIpAddress": "xx.127.24.xxx",
                    "PrivateIpAddress": "xxx.31.3.xxx",
                    "ProductCodes": [],
                    "VpcId": "vpc-aaxxxxx",
                    "StateTransitionReason": "",
                    "InstanceId": "i-xxxxxxxx",
                    "ImageId": "ami-xxxxxxx",
                    "PrivateDnsName": "ip-xxxx.ap-south-1.compute.internal",
                    "KeyName": "node",
                    "SecurityGroups": [
                        {
                            "GroupName": "xxxxxx",
                            "GroupId": "sg-xxxx"
                        }
                    ],
                    "ClientToken": "",
                    "SubnetId": "subnet-xxxx",
                    "InstanceType": "t2.xxxxx",
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use",
                            "MacAddress": "0x:xx:xx:xx:xx:xx",
                            "SourceDestCheck": true,
                            "VpcId": "vpc-xxxxxx",
                            "Description": "",
                            "NetworkInterfaceId": "eni-xxxx",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-xx.ap-south-1.compute.internal",
                                    "PrivateIpAddress": "xx.31.3.xxx",
                                    "Primary": true,
                                    "Association": {
                                        "PublicIp": "xx.127.24.xxx",
                                        "PublicDnsName": "ec2-xx.ap-south-1.compute.amazonaws.com",
                                        "IpOwnerId": "xxxxx"
                                    }
                                }
                            ],
                            "PrivateDnsName": "ip-xxx-31-3-xxx.ap-south-1.compute.internal",
                            "Attachment": {
                                "Status": "attached",
                                "DeviceIndex": 0,
                                "DeleteOnTermination": true,
                                "AttachmentId": "xxx",
                                "AttachTime": "20xx-xx-30Txx:16:xx.000Z"
                            },
                            "Groups": [
                                {
                                    "GroupName": "xxxx",
                                    "GroupId": "sg-xxxxx"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "OwnerId": "xxxx",
                            "PrivateIpAddress": "xx.xx.xx.xxx",
                            "SubnetId": "subnet-xx",
                            "Association": {
                                "PublicIp": "xx.xx.xx.xxx",
                                "PublicDnsName": "ec2-xx.ap-south-1.compute.amazonaws.com",
                                "IpOwnerId": "xxxx"
                            }
                        }
                    ],
                    "SourceDestCheck": true,
                    "Placement": {
                        "Tenancy": "default",
                        "GroupName": "",
                        "AvailabilityZone": "xx"
                    },
                    "Hypervisor": "xxx",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xxx",
                            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-xxx",
                                "AttachTime": "20xxx-xx-xxTxx:16:xx.000Z"
                            }
                        }
                    ],
                    "Architecture": "x86_64",
                    "RootDeviceType": "ebs",
                    "RootDeviceName": "/dev/xxx",
                    "VirtualizationType": "xxx",
                    "Tags": [
                        {
                            "Value": "xxxx centxx",
                            "Key": "Name"
                        }
                    ],
                    "AmiLaunchIndex": 0
                }
            ],
            "ReservationId": "r-xxxx",
            "Groups": [],
            "OwnerId": "xxxxx"
        }
    ]
}
以下bash脚本位于
/home/centos/cron scripts/

(instance=$(aws ec2 describe-instances | jq '.Reservations[].Instances | select(.[].Tags[].Value | startswith("Appname Prod") ) |  select(.[].Tags[].Key == "Appname") |  {InstanceId: .[].InstanceId, PublicDnsName: .[].PublicDnsName, State: .[].State, LaunchTime: .[].LaunchTime, Tags: .[].Tags}  | [.]' | jq -r .[].InstanceId) && aws ec2 stop-instances --instance-ids ${instance} )
使用
sh/home/centos/cron scripts/stop-ec2.sh运行该文件,并验证ec2实例是否已停止。要调试运行
aws ec2 description instances | jq'。保留[]。实例|选择([].Tags[].Value | startswith(“Appname Prod”))|选择([].Tags[].Key==“Appname”)|{InstanceId:[].InstanceId,PublicDnsName:[].PublicDnsName,State:[].State,LaunchTime:[].LaunchTime,Tags:[].Tags:[.].InstanceId
,并查看它是否返回已标记的正确实例ID

然后在
crontab-e
中可以添加以下行

3014***sh/home/centos/cron scripts/stop-ec2.sh>/tmp/stop


它将把输出记录到
/tmp/stop
3014***
是您可以签入的UTC cron表达式
https://crontab.guru/

AWS有一个很好的文档,解释了如何使用Lambda和Cloudwatch事件实现这一点。你可以参考它-

可以修改此解决方案以动态获取EC2列表,或者对一组实例进行操作,这些实例可以根据特定标记进行识别。

Amazon最近(2018年2月)发布了EC2实例调度器工具:

AWS实例调度器是AWS提供的一个简单解决方案 使客户能够轻松配置自定义启动和停止计划 对于他们的Amazon弹性计算云(Amazon EC2)和Amazon 关系数据库服务(AmazonRDS)实例。解决办法是 易于部署,有助于降低两者的运营成本 开发和生产环境。使用此功能的客户 在正常工作时间运行实例的解决方案可以节省多达 与每天24小时运行这些实例相比,提高了70%

我在15分钟内就在我的账户上安装并运行了这个程序;使用非常简单,而且实际上是免费的


要停止实例的Lambda脚本:

import json
import boto3

# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)

    filter = [{'Name': 'tag:Name', 'Values': ['****-env']}]  //give instance name here in place of ****-env

    instances = ec2.describe_instances(Filters=filter)

    #ec2.stop_instances(InstanceIds=instances)


    stop_instance = instances.get('Reservations')[0].get('Instances')[0].get('InstanceId')
    stop_instances = []
    stop_instances.append(stop_instance)

    ec2.stop_instances(InstanceIds=stop_instances)
import json
import boto3

# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)

    filter = [{'Name': 'tag:Name', 'Values': ['****-env']}]

    instances = ec2.describe_instances(Filters=filter)

    #ec2.stop_instances(InstanceIds=instances)


    start_instance = instances.get('Reservations')[0].get('Instances')[0].get('InstanceId')
    start_instances = []
    start_instances.append(start_instance)

    ec2.start_instances(InstanceIds=start_instances)
要启动实例的Lambda脚本:

import json
import boto3

# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)

    filter = [{'Name': 'tag:Name', 'Values': ['****-env']}]  //give instance name here in place of ****-env

    instances = ec2.describe_instances(Filters=filter)

    #ec2.stop_instances(InstanceIds=instances)


    stop_instance = instances.get('Reservations')[0].get('Instances')[0].get('InstanceId')
    stop_instances = []
    stop_instances.append(stop_instance)

    ec2.stop_instances(InstanceIds=stop_instances)
import json
import boto3

# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)

    filter = [{'Name': 'tag:Name', 'Values': ['****-env']}]

    instances = ec2.describe_instances(Filters=filter)

    #ec2.stop_instances(InstanceIds=instances)


    start_instance = instances.get('Reservations')[0].get('Instances')[0].get('InstanceId')
    start_instances = []
    start_instances.append(start_instance)

    ec2.start_instances(InstanceIds=start_instances)

如果您正在使用ASG,ASG调度程序是管理EC2实例的最佳且最简单的选项。如果不使用ASG,那么您可以使用AWS实例调度器CF解决方案或带有cloudwatch Cron事件的lambda。

AWS lambda是一个不错的选择,因为它有足够的空闲层访问权限,如果您正确编程,您可以每天使用Cron启动和停止EC2实例,而无需任何费用。查看lambda pricing,了解他们新的收费单位GB second::RAM-执行时间为什么不想重新启动?您是否了解,当您停止一个实例(而不是终止它)时,该实例将被关闭,而不是进入睡眠状态?AWS中没有睡眠选项(这会占用资源)。我想建议计划自动缩放,但我不确定是否可以定义0最小实例计数。实例调度器看起来确实很简单Cloudformation非常不直观,可能会更新这个答案,以了解如何使用Cloudformation进行调度,因为他们的文档已经添加。这里有一个更详细的答案