在 Azure 入口網站中建立您的第一個函式Create your first function in the Azure portal

Azure Functions 可讓您在無伺服器環境中執行程式碼,而不需要先建立 VM 或發佈 Web 應用程式。Azure Functions lets you execute your code in a serverless environment without having to first create a VM or publish a web application. 在本文中,了解如何使用 Functions 在 Azure 入口網站中建立 "hello world" 函式。In this article, learn how to use Functions to create a "hello world" function in the Azure portal.

在 Azure 入口網站中建立函式應用程式

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

注意

C# 開發人員應該考慮在 Visual Studio 2019 中建立第一個函式,而不是在入口網站中建立。C# developers should consider creating your first function in Visual Studio 2019 instead of in the portal.

登入 AzureLog in to Azure

使用您的 Azure 帳戶登入 Azure 入口網站 (https://portal.azure.com)。Sign in to the Azure portal at https://portal.azure.com with your Azure account.

建立函數應用程式Create a function app

您必須擁有函式應用程式以便主控函式的執行。You must have a function app to host the execution of your functions. 函式應用程式可讓您將多個函式群組為邏輯單位,以方便您管理、部署和共用資源。A function app lets you group functions as a logic unit for easier management, deployment, and sharing of resources.

  1. 選取 Azure 入口網站左上角的 [建立資源] 按鈕,然後選取 [計算] > [函式應用程式] 。Select the Create a resource button found on the upper left-hand corner of the Azure portal, then select Compute > Function App.

    在 Azure 入口網站中建立函式應用程式

  2. 請使用影像下面的資料表中指定的函式應用程式設定。Use the function app settings as specified in the table below the image.

    定義新的函式應用程式設定

    設定Setting 建議的值Suggested value 說明Description
    應用程式名稱App name 全域唯一的名稱Globally unique name 用以識別新函式應用程式的名稱。Name that identifies your new function app. 有效字元是 a-z0-9-Valid characters are a-z, 0-9, and -.
    訂用帳戶Subscription 您的訂用帳戶Your subscription 將在其下建立這個新函式應用程式的訂用帳戶。The subscription under which this new function app is created.
    資源群組Resource Group myResourceGroupmyResourceGroup 要在其中建立函式應用程式的新資源群組名稱。Name for the new resource group in which to create your function app.
    作業系統OS WindowsWindows Linux 上的無伺服器裝載目前處於預覽階段。Serverless hosting on Linux is currently in preview. 如需詳細資訊,請參閱此文章,了解各項應考量的內容。For more information, see this considerations article.
    主控方案Hosting plan 取用方案Consumption plan 會定義如何將資源配置給函式應用程式的主控方案。Hosting plan that defines how resources are allocated to your function app. 在預設取用方案中,您的函式會根據需要來動態新增資源。In the default Consumption Plan, resources are added dynamically as required by your functions. 在此無伺服器裝載中,您只需要針對函式有執行的時間來付費。In this serverless hosting, you only pay for the time your functions run. 在 App Service 方案中執行時,您必須管理函式應用程式的調整When you run in an App Service plan, you must manage the scaling of your function app.
    位置Location 西歐West Europe 選擇與您接近的區域,或選擇與函式將會存取之其他服務接近的區域。Choose a region near you or near other services your functions access.
    執行階段堆疊Runtime stack 慣用語言Preferred language 選擇支援您慣用函式程式設計語言的執行階段。Choose a runtime that supports your favorite function programming language. 針對 C# 和 F # 函式選擇 .NETChoose .NET for C# and F# functions.
    儲存體Storage 全域唯一的名稱Globally unique name 建立您函式應用程式使用的儲存體帳戶。Create a storage account used by your function app. 儲存體帳戶名稱必須介於 3 到 24 個字元的長度,而且只能包含數字和小寫字母。Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. 您也可以使用現有帳戶,條件是必須符合儲存體帳戶需求You can also use an existing account, which must meets the storage account requirements.
    Application InsightsApplication Insights 預設值Default 在最近的支援區域中,建立相同應用程式名稱的 Application Insights 資源。Creates an Application Insights resource of the same App name in the nearest supported region. 您可以展開此設定,變更 [新資源名稱] 或在 Azure 地理位置中依您希望儲存資料的地點,選擇不同的位置By expanding this setting, you can change the New resource name or choose a different Location in an Azure geography where you want to store your data.
  3. 選取 [建立] 以佈建並部署函式應用程式。Select Create to provision and deploy the function app.

  4. 選取入口網站右上角的 [通知] 圖示,查看是否有部署成功訊息。Select the Notification icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

    定義新的函式應用程式設定

  5. 選取 [前往資源] ,以檢視您新的函式應用程式。Select Go to resource to view your new function app. 您也可以選取 [釘選到儀表板] 。You can also select Pin to dashboard. 釘選可讓您更輕鬆地從儀表板返回此函式應用程式資源。Pinning makes it easier to return to this function app resource from your dashboard.

接下來,您要在新的函式應用程式中建立函式。Next, you create a function in the new function app.

建立由 HTTP 觸發的函式Create an HTTP triggered function

  1. 展開新的函式應用程式,接著選取 [Functions] 旁的 + 按鈕、選擇 [入口網站內] ,然後選取 [繼續] 。Expand your new function app, then select the + button next to Functions, choose In-portal, and select Continue.

    選擇平台的 Functions 快速入門。

  2. 選擇 [WebHook + API] ,然後選取 [建立] 。Choose WebHook + API and then select Create.

    Azure 入口網站中的 Functions 快速入門。

系統隨即會使用由 HTTP 觸發的函式特定語言範本來建立函式。A function is created using a language-specific template for an HTTP triggered function.

現在,您可以藉由傳送 HTTP 要求來執行新的函式。Now, you can run the new function by sending an HTTP request.

測試函式Test the function

  1. 在新的函式中,按一下右上方的 [</> 取得函式 URL] ,選取 [預設 (函式索引鍵)] ,然後按一下 [複製] 。In your new function, click </> Get function URL at the top right, select default (Function key), and then click Copy.

    從 Azure 入口網站複製函式 URL

  2. 將函式 URL 貼入瀏覽器的網址列中。Paste the function URL into your browser's address bar. 將查詢字串值 &name=<yourname> 新增至此 URL 的結尾,並按鍵盤上的 Enter 鍵執行要求。Add the query string value &name=<yourname> to the end of this URL and press the Enter key on your keyboard to execute the request. 您應該會看到函式傳回的回應顯示在瀏覽器中。You should see the response returned by the function displayed in the browser.

    下列範例會顯示瀏覽器中的回應:The following example shows the response in the browser:

    瀏覽器中的函式回應。

    要求 URL 預設會包含所需金鑰,以便透過 HTTP 存取您的函式。The request URL includes a key that is required, by default, to access your function over HTTP.

  3. 當函式執行時,系統會將追蹤資訊寫入到記錄中。When your function runs, trace information is written to the logs. 若要查看上次執行的追蹤輸出,請在入口網站中返回您的函式,然後按一下畫面底部的箭號來展開記錄To see the trace output from the previous execution, return to your function in the portal and click the arrow at the bottom of the screen to expand the Logs.

    Azure 入口網站中的函式記錄檢視器。

清除資源Clean up resources

此集合中的其他快速入門會建置在本快速入門。Other quick starts in this collection build upon this quick start. 如果您打算繼續進行後續的快速入門、教學課程或者您在本快速入門中建立的任何服務,請勿清除資源。If you plan to work with subsequent quick starts, tutorials, or with any of the services you have created in this quick start, do not clean up the resources.

在 Azure 中「資源」 是指函式應用程式、函式、儲存體帳戶等等。Resources in Azure refers to function apps, functions, storage accounts, and so forth. 它們會分組為「資源群組」 ,您可以藉由刪除群組來刪除群組中的所有項目。They are grouped into resource groups, and you can delete everything in a group by deleting the group.

您已建立資源來完成這些快速入門。You created resources to complete these quickstarts. 您可能必須支付這些資源,取決於您的帳戶狀態服務定價You may be billed for these resources, depending on your account status and service pricing. 如果您不再需要資源,刪除方式如下:If you don't need the resources anymore, here's how to delete them:

  1. 在 Azure 入口網站中,移至 [資源群組] 分頁。In the Azure portal, go to the Resource group page.

    若要從函式應用程式分頁移至該分頁,選取 [概觀] 索引標籤,然後選取 [資源群組] 底下的連結。To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    選取要從函式應用程式分頁中刪除的資源群組。

    若要從儀表板移至該分頁,選取 [資源群組] ,然後選取您用於本快速入門的資源群組。To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. 在 [資源群組] 分頁中,檢閱包含資源的清單,並確認它們是您想要刪除的項目。In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  3. 選取 [刪除資源群組] ,並遵循指示。Select Delete resource group, and follow the instructions.

    刪除需要幾分鐘的時間。Deletion may take a couple of minutes. 完成時,通知會出現幾秒鐘的時間。When it's done, a notification appears for a few seconds. 您也可以選取分頁頂端的鈴鐺圖示以檢視通知。You can also select the bell icon at the top of the page to view the notification.

後續步驟Next steps

您已經使用簡單的 HTTP 觸發函式建立函式應用程式。You've created a function app with a simple HTTP triggered function.

既然您已建立第一個函式,讓我們將輸出繫結新增至函式,以將訊息寫入儲存體佇列。Now that you have created your first function, let's add an output binding to the function that writes a message to a Storage queue.

如需詳細資訊,請參閱 Azure Functions HTTP 繫結For more information, see Azure Functions HTTP bindings.