Oracle Cloud Infrastructure (OCI) Always Free 資源自動化工具,包含:
- 搶資源 — 自動重試建立 ARM A1.Flex 免費實例(搶不到就一直試)
- 花費監控 — 定期查詢帳單,超過免費額度自動停機
- 自動備份 — 定期 Boot Volume 備份 + 輪替,一鍵還原
- 資源報表 — 一鍵查看實例、儲存、網路、花費等使用狀況
| 資源 | 免費上限 |
|---|---|
| ARM Ampere A1.Flex | 4 OCPU / 24 GB RAM(可分多台) |
| AMD E2.1.Micro | 2 台(各 1/8 OCPU / 1 GB) |
| Boot Volume | 200 GB(所有實例合計) |
| Block Volume | 2 個 / 200 GB |
| Object Storage | 20 GB |
| 出站流量 | 10 TB/月 |
注意:ARM A1 資源非常搶手,特別是大阪、東京等亞太區域。建立時常出現
Out of host capacity,需要持續重試。
搶資源前,請確認你的 OCI 帳戶已升級為 Pay As You Go (PAYG):
# macOS / Linux
./scripts/oci-report.sh account
# Windows PowerShell
powershell -File scripts\oci-report.ps1 account| Free Trial(免費試用) | Pay As You Go(隨用隨付) | |
|---|---|---|
| 免費額度 | $300 USD / 30 天 | Always Free 資源永久免費 |
| ARM A1 搶到後 | 試用到期後可能被���收 | 永久保留 |
| 超出免費額度 | 不收費(到期停用) | 超出部分按量收費 |
| 需要信用卡 | 不需要 | 需要(不主動扣款) |
強烈建議:升級為 PAYG 再搶資源,否則搶到的 ARM A1 在試用期結束後會被回收。 升級方式:OCI Console → Billing → Upgrade to Paid
升級後只要不超出 Always Free 額度,不會產生任何費用。腳本內建帳戶類型檢查,非 PAYG 時會提示。
| 平台 | 架構 | 腳本格式 |
|---|---|---|
| macOS | ARM (M1/M2/M3) / Intel | .sh (Bash) |
| Linux | AMD64 / ARM64 | .sh (Bash) |
| Windows 11 | AMD64 | .ps1 (PowerShell) |
macOS / Linux:
./scripts/install-tools.shWindows PowerShell:
powershell -ExecutionPolicy Bypass -File scripts\install-tools.ps1腳本會自動偵測平台並安裝 OCI CLI、Python 3,同時檢查設定是否正確。
# macOS / Linux
bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"
# Windows(PowerShell 或 pip)
pip install oci-clioci setup config依照提示輸入 Tenancy OCID、User OCID、Region 等。產生的 key 需到 OCI Console → Profile → API Keys 上傳公鑰。
# macOS / Linux
cp env.example .env
# Windows PowerShell
copy env.example .env編輯 .env,填入你的 OCI 資訊:
# macOS / Linux
vim .env
# Windows
notepad .env��填欄位:
| 欄位 | 說明 | 取得方式 |
|---|---|---|
TENANCY_ID |
Tenancy OCID | OCI Console → Profile → Tenancy |
COMPARTMENT_ID |
Compartment OCID | 通常跟 Tenancy 相同 |
REGION |
區域代碼 | 如 ap-osaka-1、ap-tokyo-1 |
AVAILABILITY_DOMAIN |
可用性網域 | oci iam availability-domain list |
SUBNET_ID |
子網路 OCID | oci network subnet list --compartment-id <ID> |
IMAGE_ID |
映像檔 OCID | ./scripts/oci-report.sh images |
SSH_KEY_FILE |
SSH 公鑰路徑 | 預設 ~/.ssh/id_rsa.pub |
NTFY_TOPIC |
ntfy.sh 通知 topic | 自訂,如 my-oci-notify |
確認設定正確,先跑報表看看:
# macOS / Linux
./scripts/oci-report.sh
# Windows PowerShell
powershell -File scripts\oci-report.ps1可單獨查看特定項目:
# macOS / Linux
./scripts/oci-report.sh instances # 實例
./scripts/oci-report.sh cost # 花費
./scripts/oci-report.sh volumes # 儲存
./scripts/oci-report.sh network # 網路
./scripts/oci-report.sh images # 可用映像檔(建立實例時需要 IMAGE_ID)
./scripts/oci-report.sh backups # 備份列表
./scripts/oci-report.sh limits # 免費額度上限
# Windows PowerShell
powershell -File scripts\oci-report.ps1 instances# macOS / Linux
./scripts/grab-free-a1.sh
# Windows PowerShell
powershell -ExecutionPolicy Bypass -File scripts\grab-free-a1.ps1腳本會每 30 秒重試一次,直到建立成功。搶到後會自動綁定 Reserved Public IP(固定 IP),並透過 ntfy.sh 推播通知。
Reserved IP 邏輯:腳本啟動時會自動查詢帳戶中是否已有 Reserved IP,有就重複使用(IP 不變),沒有則自動建立。Free Tier 包含 1 個免費 Reserved IP。
提示:macOS / Linux 建議用
nohup或tmux在背景執行,可能需要數小時甚至數天:nohup ./scripts/grab-free-a1.sh &Windows 建議開一個獨立的 PowerShell 視窗執行,或使用 Task Scheduler。
搶資源前可查詢該 Region 所有 ARM image,選擇你要的 OS:
./scripts/list-arm-images.sh # 列出所有 OS
./scripts/list-arm-images.sh ubuntu # 只看 Ubuntu
./scripts/list-arm-images.sh oracle # 只看 Oracle Linux找到目標 image 後,把 OCID 填入 .env 的 IMAGE_ID。
互動式查看 / 終止實例:
./scripts/manage-instances.sh會列出所有實例(含狀態、公網 IP、規格),可選擇要終止的編號。
搶到實例後,用 setup-cron 把監控部署到遠端:
# Ubuntu image 預設使用者為 ubuntu,Oracle Linux 為 opc
./scripts/setup-cron.sh ubuntu@<your-instance-ip>這會自動:
- 在遠端安裝 OCI CLI
- 複製設定檔和腳本
- 設定 cron 排程(每小時檢查花費、每天 3 次通知)
SSH 到你的 OCI 實例,加入排程:
crontab -e# OCI 花費通知(每天 3 次,依你的時區調整時間)
0 0,8,16 * * * /path/to/scripts/check-cost.sh >> /path/to/logs/check-cost.log 2>&1
# OCI 花費守衛(每小時,超過 $1 自動停機)
0 * * * * /path/to/scripts/cost-guard.sh >> /path/to/logs/cost-guard.log 2>&1./scripts/backup-instance.sh # 互動模式:選擇實例、類型、確認
./scripts/backup-instance.sh --auto # 自動模式:直接備份(cron 用)
./scripts/backup-instance.sh <instance-id> # 指定實例直接備份備份使用 OCI Boot Volume Backup(Always Free 包含 5 個額度),預設保留最新 2 個,自動輪替舊備份。
透過 setup-cron.sh 部署後會自動設定每週日 02:00 備份。也可手動加入 cron:
0 */3 * * * ~/oci-monitor/scripts/backup-instance.sh --auto >> ~/oci-monitor/logs/backup-instance.log 2>&1./scripts/restore-instance.sh # 互動模式:列出所有備份讓你選
./scripts/restore-instance.sh <backup-id> # 直接指定備份 OCID 還原還原流程:
- 終止現有實例(舊 Boot Volume 保留以防萬一)
- 從備份建立新的 Boot Volume
- 用新 Boot Volume 啟動新實例
- 自動重新綁定 Reserved IP
- 確認成功後可選擇刪除舊 Boot Volume
./scripts/oci-report.sh backups.env 備份相關設定:
| 變數 | 說明 | 預設值 |
|---|---|---|
BACKUP_KEEP |
保留備份數量 | 5 |
BACKUP_TYPE |
備份類型 (INCREMENTAL / FULL) |
INCREMENTAL |
# macOS / Linux
./scripts/check-cost.sh # 花費通知(會發送 ntfy 推播)
./scripts/cost-guard.sh # 花費守衛
# Windows PowerShell
powershell -File scripts\check-cost.ps1
powershell -File scripts\cost-guard.ps1所有腳本透過共用的 notify.sh / notify.ps1 發送通知。ONS email 為預設,ntfy.sh 為選填,兩者可同時啟用。
- OCI Console → Application Integration → Notifications
- Create Topic → 記下 Topic OCID
- 在 Topic 底下 Create Subscription → 選 Email → 輸入你的信箱 → 收信確認
- 把 Topic OCID 填入
.env的ONS_TOPIC_ID
OCI ONS 免費額度:每月 100 萬則,足夠日常監控使用。
如果也想收手機推播:
- 手機安裝 ntfy app(iOS / Android)
- 在
.env設定NTFY_TOPIC="your-topic-name" - 在 app 中訂閱同名 topic
- 不需要註冊帳號
NTFY_TOPIC留空則不發送 ntfy 通知。
- 查詢 OCI Usage API 取得當月累計花費
- 列出所有 RUNNING 狀態的實例
- 花費 $0 → 低優先通知(Free Tier 正常)
- 花費 > $0 → 高優先通知(可能超出免費額度)
- 查詢當月花費是否超過
COST_LIMIT(預設 $1 USD) - 超過時自動停止所有實例,防止持續計費
- 發送緊急通知
- 透過 PAM 在 SSH 登入時自動觸發
- 發送 ntfy 通知,包含登入使用者、來源 IP、時間
- 由
setup-cron.sh自動安裝,或手動安裝:
sudo cp scripts/ssh-login-notify.sh /usr/local/bin/
sudo chmod +x /usr/local/bin/ssh-login-notify.sh
echo 'session optional pam_exec.so seteuid /usr/local/bin/ssh-login-notify.sh' | sudo tee -a /etc/pam.d/sshd相關 .env 設定:
| 變數 | 說明 | 預設值 |
|---|---|---|
SSH_NOTIFY_ENABLED |
啟用 SSH 登入通知 | true |
SSH_NOTIFY_PRIORITY |
通知優先級 | high |
oracle_cloud_monitor/
├── README.md
├── env.example # 設定檔範本
├── .env # 你的設定(不會進 git)
├── .gitignore
├── scripts/
│ ├── install-tools.sh # 工具安裝(Mac/Linux)
│ ├── install-tools.ps1 # 工具安裝(Windows PowerShell)
│ ├── grab-free-a1.sh # 搶 ARM A1(Mac/Linux)
│ ├── grab-free-a1.ps1 # 搶 ARM A1(Windows)
│ ├── check-cost.sh # 花費通知(Mac/Linux)
│ ├── check-cost.ps1 # 花費通知(Windows)
│ ├── cost-guard.sh # 花費守衛(Mac/Linux)
│ ├── cost-guard.ps1 # 花費守衛(Windows)
│ ├── oci-report.sh # 資源報表(Mac/Linux)
│ ├── oci-report.ps1 # 資源報表(Windows)
│ ├── notify.sh # 共用通知函式(Bash)
│ ├── notify.ps1 # 共用通知函式(PowerShell)
│ ├── ssh-login-notify.sh # SSH 登入通知(PAM)
│ ├── backup-instance.sh # Boot Volume 自動備份 + 輪替
│ ├── restore-instance.sh # 從備份還原實例
│ ├── setup-cron.sh # 一鍵部署監控到遠端
│ ├── list-arm-images.sh # 查詢可用 ARM 映像檔
│ └── manage-instances.sh # 互動式實例管理(查看/終止)
└── logs/ # 日誌(不會進 git)
ARM A1 免費資源非常搶手,特別是亞太區域。建議:
- 選擇較冷門的 region(如
us-phoenix-1、ap-melbourne-1) - 在離峰時段執行(凌晨 2-6 點)
- 保持腳本持續運行,通常數小時到數天內可搶到
會!ARM A1 免費額度是 4 OCPU / 24 GB 合計,不是每台。如果你已經有一台 4/24 的 A1,再搶一台就會收費。請先確認現有資源:
./scripts/oci-report.sh instances- 先到 OCI Console 確認是什麼產生了費用
- 移除超額資源
- 手動啟動需要的實例:
oci compute instance action --instance-id <INSTANCE_OCID> --action START可以,但不建議。Mac 不一定 24 小時開機,可能漏掉檢查。建議部署到 OCI 實例上(用 setup-cron.sh),讓它 7×24 監控。
MIT