DevOps-Automation with Python 13
前面完成以下程式:
- Automated Backups
- Restore Volume from Backup
但是在實際使用這類程式進行自動化過程,都有可能發生異常
Handling Errors in My Python scripts:
- Snapshot creation failed
- Volume Attachment failed
- Volume Creation from Snapshot failed
- …etc
也就是前面幾篇寫的程式可能都會有出錯的狀況
所以都要針對出現錯誤的狀況作處理
Write code that also handles possible errors
if errors not handled correctly, it can lead to data loss, corrupt data etc.
→Handle them specifically
→Log the error for debugging
Think about critical vs. non critical
例如在建立Snapshot過程出現問題
叫好的解決方式是刪除這個可能有問題的Snapshot,然後re-try重新建立
這時候就不應該保留這個可能有問題的Snapshot
另外也有可能寫的Python程式進行EC2機台的相關處理
要盡量是可以做到Roll-Back
因為例如Terraform在做相關作業會有state的概念
但是Python程式沒有紀錄state,變成我們要自己管控EC2機台狀態
這時候就要仰賴Python前面學過的try error相關處理攔截錯誤
例如:
可以做一些處理