---
# System prepended metadata

title: AWS RDS/EC2 自動開關機排程
tags: [AWS]

---

# AWS Instance Scheduler

使用 Instance Scheduler on AWS
https://aws.amazon.com/tw/solutions/implementations/instance-scheduler-on-aws/
### 實作流程
1. 在CloudFormation建立stack
2. 到DynamoDB查詢建立好的table
3. 編輯configTable中的schedule與period來設定排程
4. add tag到要套用排程的RDS/EC2，
 此tag須與stack的tag相同，預設為Schedule，
 tag value須為要套用的schedule name

到
https://aws.amazon.com/tw/solutions/implementations/instance-scheduler-on-aws/
啟動cloudformation
![image](https://hackmd.io/_uploads/BySwypGS6.png)

開始建立stack:
stack建立時，時區需切換至Asia/Taipei
![image](https://hackmd.io/_uploads/HkAhJpzST.png)

建立好的stack:
![image](https://hackmd.io/_uploads/H1MWH3frT.png)

DynamoDB中的table:
![image](https://hackmd.io/_uploads/HkOMBhfH6.png)

configTable中的item
須在此編輯schedule與period
![image](https://hackmd.io/_uploads/rJ4mH3GBT.png)

編輯schedule:設定時區等屬性，一個schedule可套用多個period，
此處schedule name將為RDS/EC2的tag value
此例schedule name為weekday，則被套用的RDS/EC2 tag value須為weekday
![image](https://hackmd.io/_uploads/SJ6ESnMHa.png)

編輯period:在此處編輯排程時段
![image](https://hackmd.io/_uploads/rJULB2MHT.png)

add tag to RDS/EC2:
tag name為stack tag name，default為Schedule
tag value為要套用的schedule name，此處為weekday
![image](https://hackmd.io/_uploads/ByVgI2frp.png)


### 執行結果
可見此RDS instance在排程時間自動關閉:
![image](https://hackmd.io/_uploads/ByeTz2zHT.png)

scheduler會自動加上last action的tag來紀錄上次執行結果:
![image](https://hackmd.io/_uploads/BJ9Cf3MS6.png)

### cost
https://docs.aws.amazon.com/zh_tw/solutions/latest/instance-scheduler-on-aws/cost.html

### Reference
https://aws.amazon.com/tw/solutions/implementations/instance-scheduler-on-aws/

https://youtu.be/KPnKvvkte6g?si=YVL3JhuSYCKc5GcD