DevOps-Automation with Python 13

Handling Errors

ZONGRU Li
Nov 14, 2023

前面完成以下程式:

  1. Automated Backups
  2. Restore Volume from Backup

但是在實際使用這類程式進行自動化過程,都有可能發生異常

Handling Errors in My Python scripts:

  1. Snapshot creation failed
  2. Volume Attachment failed
  3. Volume Creation from Snapshot failed
  4. …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相關處理攔截錯誤

例如:

可以做一些處理

參考課程reference

--

--

ZONGRU Li

2022/11/17 開源部分個人筆記給LINE "Java程式語言討論區"社群,希望能對社群的技術學習做一點點貢獻.(掩面....記得退訂閱!