【 Cloud 】透過 Logic App 定時啟動 Azure VM

【 Cloud 】透過 Logic App 定時啟動 Azure VM
Photo by Alexander Kunze / Unsplash

內容

  • 學習目標
  • 前置準備作業
  • 建立與設定 Logic App
  • 設定 Azure VM
  • 測試與驗證

學習目標

  • 如何透過 Logic App 定時啟動 Azure VM

前置準備作業

  • 已建立 Azure 帳號並能正常登入
  • 已於 Azure 建立 Virtual machine

建立與設定 Logic App

Step 1. 開啟 Logic App

  • 在上方搜尋框輸入 logic 並點選下方 Logic Apps 的搜尋結果

---2022-01-04---8.36.12


Step 2. 建立 Logic Apps

  • 點選左上角的 + Create

---2022-01-04---8.38.13


Step 3. 輸入相關資訊

---2022-01-04---8.39.34

  • Subscription 欄位請選擇所要使用哪個訂閱帳號
  • Resource Group 欄位請選擇使用舊有的或新建立一個新的
  • Type 欄位請選擇 Consumption
  • Logic App name 欄位請輸入一個唯一且可識別的名稱
    • 本範例使用 boot-logic-app
  • Region 欄位請選擇此服務所要建立在哪個資料中心
  • 以上資訊輸入完成後請點選左下角的 Review + create

---2022-01-04---8.43.00


Step 4. 再次確認相關資訊後請按左下角的 Create

---2022-01-04---8.43.42


Step 5. 建立完成畫面並點選 Go to resource

---2022-01-04---8.44.57


Step 6. 點選 Recurrence

---2022-01-04---8.46.46


Step 7. 設定 Recurrence

  • 本範例預計 每日早上 8:30 自動開啟 Azure VM

---2022-01-04---8.50.19

  • Interval 欄位請輸入 1
  • Frequency 欄位請輸入 Day
  • 點選 Add new parameter 中的 Time zoneAt these hoursAt these minutes

---2022-01-04---9.02.56

---2022-01-04---9.03.32

  • 設定時區與時間
    • Time zone 欄位請選擇所在的時間
      • 本範例為 (UTC+08:00) Taipei
    • At these hours 欄位請選擇 8
    • At these minutes 欄位請輸入 30

---2022-01-04---9.06.37

---2022-01-04---9.12.31


Step 8. 設定啟動 Azure VM Logic

  • 點選 + New step

---2022-01-04---9.12.31--

  • 點選 HTTP

---2022-01-04---9.15.35

  • 輸入相關資訊
    • Method 欄位請選擇 POST
    • URI 欄位請輸入下方 URI 並修改其中的 Subscription-IDResource-GroupComputer-Name 為 Azure VM 所對應的屬性
    https://management.azure.com/subscriptions/Subscription-ID/resourceGroups/Resource-Group/providers/Microsoft.Compute/virtualMachines/Computer-Name/start?api-version=2019-12-01
    
    ---2022-01-04---9.17.18
    ---2022-01-04---9.22.21
    • 點選 Add new parameter
      ---2022-01-04---9.26.29
      • 選擇 Authentication
        ---2022-01-04---9.28.39
      • Authentication type 欄位選擇 Managed identity
        ---2022-01-04---9.30.30
        ---2022-01-04---9.31.25
        ---2022-01-04---9.33.34
    • 以上資訊輸入完成後點選 Save
      ---2022-01-04---9.33.34--

Step 9. 啟用 Logic App Identity

  • 回到所建立的 Logic App 頁點再點選左側的 Identity > 再點選 On > 點選 Save

---2022-01-04---9.37.35

  • 點選 Yes

---2022-01-04---9.38.57

設定 Azure VM

Step 1. 回到所建立的 Azure VM 並把 VM 關機

---2022-01-04---9.41.28


Step 2. 點選左側的 Access control (IAM)

---2022-01-04---9.41.28--


Step 3. 點選 + Add

---2022-01-04---9.44.22


Step 4. 點選 Add role assignment

---2022-01-04---9.47.06


Step 5. 點選 Virtual Machine Contributor 後再點選 Next

---2022-01-04---9.48.43


Step 6. 設定 Member 資訊

---2022-01-04---9.50.59

  • 輸入相關資訊
    • Assign access to 欄位點選 Managed identity
      ---2022-01-04---9.52.31
    • Members 點選 + Select members 再點選所建立的 Logic App 後點選 Select
      ---2022-01-04---9.54.05-1
    • 以上資訊輸入完成後請點選左下角的 Review + assign
      ---2022-01-04---9.56.55
    • 再次確認資訊後請點選左下角的 Review + assign
      ---2022-01-04---9.58.36

測試與驗證

Step 1. 回到所建立的 Logic App

---2022-01-04---10.00.47


Step 2. 點選上方的 Run Trigger

---2022-01-04---10.00.47--


Step 3. 點選 Run

---2022-01-04---10.02.47


Step 4. Trigger 結果

  • Trigger Running

---2022-01-04---10.03.56

  • Trigger Success

---2022-01-04---10.04.41

  • 此時 Azure VM 已開機完成

---2022-01-04---10.06.00

GitHub

List of blogs