Có, bạn có thể làm điều đó bằng AWS Lambda. Bạn có thể chọn trình kích hoạt trong Cloudwatch chạy trên biểu thức Cron trên UTC.
Đây là liên kết liên quan https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
Một lựa chọn khác là sử dụng awsclitrong đó có sẵn từ pip, apt-get, yumhay brew, và sau đó chạy aws configurevới các thông tin của bạn xuất khẩu từ IAM và thực hiện kịch bản bash sau, để ngăn chặn một EC2 đã được gắn thẻ với Name: Appnamevà Value: Appname Prod. Bạn có thể sử dụng awscliđể gắn thẻ các phiên bản của mình hoặc gắn thẻ thủ công từ bảng điều khiển AWS. aws ec2 stop-instancessẽ dừng phiên bản và jqđược sử dụng để lọc truy vấn json và tìm nạp id phiên bản chính xác bằng cách sử dụng các thẻ từ aws ec2 describe-instances.
Để xác minh điều đó aws configuređã thành công và trả về chạy đầu ra json aws ec2 describe-instancesvà id phiên bản đang chạy của bạn sẽ ở đó trong đầu ra. Đây là đầu ra mẫu
{
"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"
}
]
}
Các kịch bản bash Sau đây là stop-ec2.shở /home/centos/cron-scripts/đó được lấy cảm hứng từ bài này SO
(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} )
Chạy tệp bằng cách sử dụng sh /home/centos/cron-scripts/stop-ec2.shvà xác minh rằng phiên bản EC2 bị dừng. Để gỡ lỗi, hãy chạy 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 .[].InstanceIdvà thấy rằng nó trả về ID phiên bản chính xác đã được gắn thẻ.
Sau đó, trong crontab -edòng sau có thể được thêm
30 14 * * * sh /home/centos/cron-scripts/stop-ec2.sh >> /tmp/stop
sẽ ghi đầu ra vào /tmp/stop. Đây 30 14 * * *là biểu thức cron UTC mà bạn có thể kiểm tra https://crontab.guru/. Tương tự thay thế bằng aws ec2 start-instancescó thể bắt đầu một thể hiện.