您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用面向学生的 Azure 入门版创建函数Create a function using Azure for Students Starter

在本教程中,我们将在面向学生的 Azure 入门版订阅中创建一个“hello world”HTTP 函数。In this tutorial, we'll create a "hello world" HTTP function in an Azure for Students Starter subscription. 此外,我们还将演练 Azure Functions 在此类订阅中的功能。We'll also walk through what's available in Azure Functions in this subscription type.

借助面向学生的 Microsoft Azure 入门版,可以免费体验在云中进行开发所需的 Azure 产品。Microsoft Azure for Students Starter gets you started with the Azure products you need to develop in the cloud at no cost to you. 在此处详细了解此产品/服务。Learn more about this offer here.

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。Learn more about Functions here.

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

创建函数Create a function

本文介绍如何使用 Azure Functions 在 Azure 门户中创建一个“hello world”HTTP 触发器函数。In this article, learn how to use Azure Functions to create an "hello world" HTTP trigger function in the Azure portal.

在 Azure 门户中创建 Function App

登录 AzureSign in to Azure

使用 Azure 帐户登录到 Azure 门户Sign in to the Azure portal with your Azure account.

创建函数应用Create a function app

必须使用 Function App 托管函数的执行。You must have a function app to host the execution of your functions. 函数应用可将函数分组为逻辑单元,以便更轻松地管理、部署、缩放和共享资源。A function app lets you group functions as a logical unit for easier management, deployment, scaling, and sharing of resources.

  1. 在 Azure 门户菜单或“主页”页中,选择“创建资源” 。From the Azure portal menu or the Home page, select Create a resource.

  2. “新建” 页面,选择 “计算” > “函数应用”In the New page, select Compute > Function App.

  3. 在“基本信息”页上,使用下表中指定的函数应用设置。On the Basics page, use the function app settings as specified in the following table.

    设置Setting 建议的值Suggested value 说明Description
    订阅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.
    函数应用名称Function App name 全局唯一名称Globally unique name 用于标识新 Function App 的名称。Name that identifies your new function app. 有效字符为 a-z(不区分大小写)、0-9-Valid characters are a-z (case insensitive), 0-9, and -.
    发布Publish 代码Code 用于发布代码文件或 Docker 容器的选项。Option to publish code files or a Docker container.
    运行时堆栈Runtime stack 首选语言Preferred language 选择支持你喜欢的函数编程语言的运行时。Choose a runtime that supports your favorite function programming language. 门户中编辑仅适用于 JavaScript、PowerShell、TypeScript 和 C# 脚本。In-portal editing is only available for JavaScript, PowerShell, TypeScript, and C# script. C# 类库、Java 和 Python 函数必须在本地开发C# class library, Java, and Python functions must be developed locally.
    版本Version 版本号Version number 选择已安装的运行时的版本。Choose the version of your installed runtime.
    区域Region 首选区域Preferred region 选择离你近或离函数访问的其他服务近的区域Choose a region near you or near other services your functions access.
  4. 选择“下一步: 托管”。Select Next : Hosting. 在“托管”页上,输入以下设置。On the Hosting page, enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    存储帐户Storage account 全局唯一名称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 can contain numbers and lowercase letters only. 也可使用现有帐户,但该帐户必须符合存储帐户要求You can also use an existing account, which must meet the storage account requirements.
    操作系统Operating system WindowsWindows 系统会根据你的运行时堆栈选择为你预先选择一个操作系统,但你可以根据需要更改该设置。An operating system is pre-selected for you based on your runtime stack selection, but you can change the setting if necessary. 仅在 Windows 上支持门户内编辑。In-portal editing is only supported on Windows.
    计划Plan 消耗(无服务器)Consumption (Serverless) 定义如何将资源分配给 Function App 的托管计划。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 pay only for the time your functions run. 按应用服务计划运行时,必须管理函数应用的缩放When you run in an App Service plan, you must manage the scaling of your function app.
  5. 选择“下一步: 监视”。Select Next : Monitoring. 在“监视”页上,输入以下设置。On the Monitoring page, enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    Application InsightsApplication Insights 默认Default 在最近的受支持的区域中,创建一个具有相同应用名称的 Application Insights 资源。Creates an Application Insights resource of the same App name in the nearest supported region. 通过展开此设置或选择“新建”,可以更改 Application Insights 名称或在 Azure 地理位置中选择要存储数据的其他区域。By expanding this setting or selecting Create new, you can change the Application Insights name or choose a different region in an Azure geography where you want to store your data.
  6. 选择“查看 + 创建”,以便查看应用配置选择。Select Review + create to review the app configuration selections.

  7. 在“查看 + 创建”页上查看设置,然后选择“创建”来预配并部署函数应用 。On the Review + create page, review your settings, and then select Create to provision and deploy the function app.

  8. 选择门户右上角的“通知”图标,留意是否显示了“部署成功”消息。 Select the Notifications icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

  9. 选择“转到资源”,查看新的函数应用。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.

    部署通知

接下来,在新的 Function App 中创建一个函数。Next, you create a function in the new function app.

创建 HTTP 触发器函数Create an HTTP trigger function

  1. 从“Functions”窗口的左侧菜单中选择“Functions”,然后从顶部菜单中选择“添加”。From the left menu of the Functions window, select Functions, then select Add from the top menu.

  2. 在“新建函数”窗口中,选择“Http 触发器”。From the New Function window, select Http trigger.

    选择 HTTP 触发器函数

  3. 在“新建函数”窗口中,接受新函数的默认名称,或输入新名称。In the New Function window, accept the default name for New Function, or enter a new name.

  4. 从“授权级别”下拉列表中选择“匿名”,然后选择“创建函数”。Choose Anonymous from the Authorization level drop-down list, and then select Create Function.

    Azure 会创建该 HTTP 触发器函数。Azure creates the HTTP trigger function. 现在,可以通过发送 HTTP 请求来运行新函数了。Now, you can run the new function by sending an HTTP request.

测试函数Test the function

  1. 在新的 HTTP 触发器函数中,从左侧菜单中选择“代码 + 测试”",然后从顶部菜单中选择“获取函数 URL”。In your new HTTP trigger function, select Code + Test from the left menu, then select Get function URL from the top menu.

    选择“获取函数 URL”

  2. 在“获取函数 URL”对话框的下拉列表中选择“默认值”,然后选择“复制到剪贴板”图标。In the Get function URL dialog box, select default from the drop-down list, and then select the Copy to clipboard icon.

    从 Azure 门户复制函数 URL

  3. 将函数 URL 粘贴到浏览器的地址栏中。Paste the function URL into your browser's address bar. 将查询字符串值 ?name=<your_name> 添加到该 URL 尾部,然后按 Enter 来运行请求。Add the query string value ?name=<your_name> to the end of this URL and press Enter to run the request.

    以下示例显示了浏览器中的响应: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.

  4. 运行函数时,会在日志中写入跟踪信息。When your function runs, trace information is written to the logs. 若要查看跟踪输出,请返回到门户中的“代码 + 测试”页,并展开页面底部的“日志”箭头。To see the trace output, return to the Code + Test page in the portal and expand the Logs arrow at the bottom of the page.

    Azure 门户中的“函数日志”查看器。

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quickstarts in this collection build upon this quickstart. 如果打算使用后续的快速入门、教程或者在本快速入门中创建的任何服务,请勿清理这些资源。If you plan to work with subsequent quickstarts, tutorials, or with any of the services you have created in this quickstart, do not clean up the resources.

Azure 中的资源是指函数应用、函数、存储帐户等。Resources in Azure refer to function apps, functions, storage accounts, and so forth. 这些资源可以组合到资源组中,删除该组即可删除组中的所有内容。They're 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 article.

  2. 在“资源组”页中查看所包括的资源的列表,然后验证这些资源是否是要删除的。In the Resource group page, review the list of included resources, and verify that they're 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.

面向学生的 Azure 入门版支持的功能Supported features in Azure for Students Starter

在面向学生的 Azure 入门版中,可以访问 Azure Functions 运行时的大部分功能,但也存在下面所列的一些重要限制:In Azure for Students Starter, you have access to most of the features of the Azure Functions runtime, with several key limitations listed below:

  • HTTP 触发器是唯一受支持的触发器类型。The HTTP trigger is the only trigger type supported.
  • 支持的语言:Languages Supported:
  • Windows 是唯一受支持的操作系统。Windows is the only supported operating system.
  • 缩放限制为每天最长运行 60 分钟的一个免费层实例Scale is restricted to one free tier instance running for up to 60 minutes each day. 收到 HTTP 流量时,会自动以无服务器的方式从 0 个实例扩展到 1 个实例,但无法扩展到更多的实例。You'll serverlessly scale from 0 to 1 instance automatically as HTTP traffic is received, but no further.
  • 仅支持 版本 2.x 和更高版本的 Functions 运行时。Only version 2.x and later of the Functions runtime is supported.
  • 支持使用所有开发人员工具来编辑和发布函数。All developer tooling is supported for editing and publishing functions. 这包括 VS Code、Visual Studio、Azure CLI 和 Azure 门户。This includes VS Code, Visual Studio, the Azure CLI, and the Azure portal. 若要使用除门户以外的任何工具,首先需要在门户中创建一个应用,然后在首选工具中选择该应用作为部署目标。If you'd like to use anything other than the portal, you'll need to first create an app in the portal, and then choose that app as a deployment target in your preferred tool.

后续步骤Next steps

现在已完成使用简单的 HTTP 触发器函数创建函数应用的操作。You've now finished creating a function app with a simple HTTP trigger function. 接下来,可以探索本地工具、更多语言以及监视和集成功能。Next, you can explore local tooling, more languages, monitoring, and integrations.