Try   HackMD

新人資訊

技術-後台類-azure function #1-先測通再說

function as a service 真是很酷的架構,所有資訊服務的底層包含網路 (我曾經把網站架在家裡,光是搞固定對外 ip 和 dns 就快煩死了),作業系統 (windows or linux 你會裝嗎,有版權嗎?),運算平台 (dot net or java or python 運算環境等等) 全部不用煩惱,就專注寫一段邏輯達成一個特定功能就好,雲端服務業者如 azure 還附帶提供「擴展性」,用多少算多少,免費額度還超高,沒人用時不用負擔固定成本,或是有那麼一天需要爆量使用也不怕撐不住!我老早就想嘗試了,就是缺乏一個動機或小衝動,在家沉寂了許久之後,決定重拾這個小樂趣。

一個 azure 帳號是需要的,在點選 Create a resource 在數百項服務中找到 function app 建立之。必要選項為一個全球唯一的名字 (因為它會擁有一個獨一無二的公開網址,意思是,若你的程式邏輯夠酷到能夠吸引人以 api 的方式呼叫使用,可以即刻營運成為一門生意!這個時代對「有能力的開發者」而言,真是充滿機會啊),選擇一種運算平台,喜歡 python 簡潔的語法所以選用,不選最新版本 3.9 是因為經實測後續安裝爬蟲所需套件尚未相容,先選 3.8。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

第二步驟需要選用一個 storage account (如何運用此儲存空間並不需煩惱,是系統自動運作的),和一個計價模式 (小資開發者當然是選用多少算多少的 Consumption (Serverless) 囉。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後系統會顯示所有的設定值供最後 review,確認無誤後按「Create」即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

稍待片刻很快的資源就建立完成了,迫不及待的進入管理介面看看可以怎樣玩。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

想要建立一個 hello world 的 function,點選以下並未出現開發畫面,僅出現說明文件,要開發寫程式需要移駕 vs code

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

vs code 需要安裝一個 extension,另外為了在本機預覽測試,還需安裝 node.js,python 環境當然也需要的。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

若 extension 安裝正確就會在 Azure 頁籤中出現 FUNCTIONS 區域,點選「Crete New Project」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

建立專案依序需要選擇以下選項:運算平台為 Python,版本為 3.8,function 的種類以最容易測試的 Http Trigger 為首的範例 (網頁一開就可測試),function 的名稱為 HttpTrigger1,授權等級就先全部開放 (在此也可得知,若要真正營運時有嚴密的授權保護機制可選用,這若自己硬刻也是非常難的門檻)!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後功能強大的 IDE 就幫我們生成整個專案架構,基本功能就是讀取傳入的網址參數 name,然後回應文字;程式進入點很清楚,也可得知後續擴展邏輯需要在哪兒安插自己的程式。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如此即刻可以進行本機測試,點選「啟動偵錯」,若設定都對在系統一陣運作後會出現一段本機網址。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開啟 browser 貼上網址,發現系統已經回應正確的文字了!已經成功了。若在網址列傳入 name 參數,也會正確產生動態內容,證明程式運作正常。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接下來就可以把它 deploy 到稍早建立的 azure 雲端環境,對全世界提供服務了。在 vs code 中切回檔案瀏覽頁籤,右鍵點選 deploy 的主角特定 function HttpTrigger1,選取 Deploy To Function App,若已正確 login azure 帳號,稍早建立的 function app 會被帶出,選取即可。(2022/3/4 註:若此專案有其他的 function 也會一併 deploy,而不只是這個 HttpTrigger1)

回到 azure 主控台查看,剛才空空的 Functions 頁面已經多了一個項目。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在 Overview 頁面可以找到服務網址,取代稍早的 localhost:7071 部分,發現已經正式運作成功了!

azure function app 初體驗,測試成功。下一步就是安插自己的程式進去,待續。

By Newman 2022/3/1

參考資料:

https://docs.microsoft.com/zh-tw/azure/azure-functions/