在 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 topic, learn how to use Functions to create a "hello world" function in the Azure portal.

在 Azure 门户中创建 Function App

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

Note

C# 开发人员应考虑在 Visual Studio 2017 中创建第一个函数,而不要在门户中这样做。C# developers should consider creating your first function in Visual Studio 2017 instead of in the portal.

登录 AzureLog in to Azure

使用 Azure 帐户登录到 https://portal.azure.cn 的 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn 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 门户左上角的“新建资源”按钮,然后选择“计算” > “Function App”。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 用于标识新 Function App 的名称。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 要在其中创建 Function App 的新资源组的名称。Name for the new resource group in which to create your function app.
    运行时堆栈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.
    应用服务计划/位置App Service plan/Location 按应用服务计划运行时,必须管理函数应用的缩放When you run in an App Service plan, you must manage the scaling of your function app.
  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.

Tip

如果在门户中找不到函数应用,请尝试将 Function App 添加到 Azure 门户中的收藏夹Having trouble finding your function apps in the portal, try adding Function Apps to your favorites in the Azure portal.

接下来,在新的 Function App 中创建一个函数。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 门户中的函数快速入门。

此时将使用 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 触发函数创建 Function App。You have created a function app with a simple HTTP triggered function.

创建第一个函数以后,即可向这个可以将消息写入存储队列的函数添加输出绑定。Now that you have a 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.