Quickstart: Create your first function from the command line using Azure CLI
This quickstart topic walks you through how to create your first function from the command line or terminal. You use the Azure CLI to create a function app, which is the serverless infrastructure that hosts your function. The function code project is generated from a template by using the Azure Functions Core Tools, which is also used to deploy the function app project to Azure.
You can follow the steps below using a Mac, Windows, or Linux computer.
Prerequisites
Before running this sample, you must have the following:
Install Azure Functions Core Tools version 2.6.666 or later.
Install the Azure CLI. This article requires the Azure CLI version 2.0 or later. Run
az --version
to find the version you have. You can also use the Azure Cloud Shell.An active Azure subscription.
If you don't have an Azure subscription, create a free account before you begin.
Create the local function app project
Run the following command from the command line to create a function app project in the MyFunctionProj
folder of the current local directory. A GitHub repo is also created in MyFunctionProj
.
func init MyFunctionProj
When prompted, select a worker runtime from the following language choices:
dotnet
: creates a .NET class library project (.csproj).node
: creates a Node.js-based project. Choose eitherjavascript
ortypescript
.python
: for a Python project, please instead complete Create an HTTP triggered function in Azure.powershell
: for a PowerShell project, please instead complete Create your first PowerShell function in Azure.
After the project is created, use the following command to navigate to the new MyFunctionProj
project folder.
cd MyFunctionProj
Enable extension bundles
The easiest way to install binding extensions is to enable extension bundles. When you enable bundles, a predefined set of extension packages is automatically installed.
To enable extension bundles, open the host.json file and update its contents to match the following code:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
Create a function
The following command creates an HTTP-triggered function named MyHttpTrigger
.
func new --name MyHttpTrigger --template "HttpTrigger"
When the command executes, you see something like the following output:
The function "MyHttpTrigger" was created successfully from the "HttpTrigger" template.
Run the function locally
The following command starts the function app. The app runs using the same Azure Functions runtime that is in Azure. The start command varies, depending on your project language.
C#
func start --build
JavaScript
func start
TypeScript
npm install
npm start
When the Functions host starts, it writes something like the following output, which has been truncated for readability:
%%%%%%
%%%%%%
@ %%%%%% @
@@ %%%%%% @@
@@@ %%%%%%%%%%% @@@
@@ %%%%%%%%%% @@
@@ %%%% @@
@@ %%% @@
@@ %% @@
%%
%
...
Content root path: C:\functions\MyFunctionProj
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
...
Http Functions:
HttpTrigger: http://localhost:7071/api/MyHttpTrigger
[8/27/2018 10:38:27 PM] Host started (29486ms)
[8/27/2018 10:38:27 PM] Job host started
Copy the URL of your HttpTrigger
function from the runtime output and paste it into your browser's address bar. Append the query string ?name=<yourname>
to this URL and execute the request. The following shows the response in the browser to the GET request returned by the local function:
Now that you have run your function locally, you can create the function app and other required resources in Azure.
Create a resource group
Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources like function apps, databases, and storage accounts are deployed and managed.
The following example creates a resource group named myResourceGroup
.
If you aren't using Cloud Shell, sign in first using az login
.
az group create --name myResourceGroup --location westeurope
You generally create your resource group and the resources in a region near you.
Create an Azure Storage account
Functions uses a general-purpose account in Azure Storage to maintain state and other information about your functions. Create a general-purpose storage account in the resource group you created by using the az storage account create command.
In the following command, substitute a globally unique storage account name where you see the <storage_name>
placeholder. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
az storage account create --name <storage_name> --location westeurope --resource-group myResourceGroup --sku Standard_LRS
Create a function app
You must have a function app to host the execution of your functions. The function app provides an environment for serverless execution of your function code. It lets you group functions as a logic unit for easier management, deployment, and sharing of resources. Create a function app by using the az functionapp create command.
In the following command, substitute a unique function app name where you see the <APP_NAME>
placeholder and the storage account name for <STORAGE_NAME>
. The <APP_NAME>
is used as the default DNS domain for the function app, and so the name needs to be unique across all apps in Azure. You should also set the <language>
runtime for your function app, from dotnet
(C#) or node
(JavaScript).
az functionapp create --resource-group myResourceGroup --consumption-plan-location westeurope \
--name <APP_NAME> --storage-account <STORAGE_NAME> --runtime <language>
Setting the consumption-plan-location parameter means that the function app is hosted in a Consumption hosting plan. In this serverless plan, resources are added dynamically as required by your functions and you only pay when functions are running. For more information, see Choose the correct hosting plan.
After the function app has been created, the Azure CLI shows information similar to the following example:
{
"availabilityState": "Normal",
"clientAffinityEnabled": true,
"clientCertEnabled": false,
"containerSize": 1536,
"dailyMemoryTimeQuota": 0,
"defaultHostName": "quickstart.azurewebsites.net",
"enabled": true,
"enabledHostNames": [
"quickstart.azurewebsites.net",
"quickstart.scm.azurewebsites.net"
],
....
// Remaining output has been truncated for readability.
}
Deploy the function app project to Azure
After the function app is created in Azure, you can use the func azure functionapp publish
Core Tools command to deploy your project code to Azure. In these examples, replace <APP_NAME>
with the name of your app from the previous step.
C# / JavaScript
func azure functionapp publish <APP_NAME>
Python
func azure functionapp publish <APP_NAME> --build remote
TypeScript
npm run build:production
func azure functionapp publish <APP_NAME>
You'll see output similar to the following, which has been truncated for readability:
Getting site publishing info...
...
Preparing archive...
Uploading content...
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...
Functions in myfunctionapp:
HttpTrigger - [httpTrigger]
Invoke url: https://myfunctionapp.azurewebsites.net/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....
Copy the Invoke url
value for your HttpTrigger
, which you can now use to test your function in Azure. The URL contains a code
query string value that is your function key. This key makes it difficult for others to call your HTTP trigger endpoint in Azure.
Verify the function in Azure
Use cURL to verify the deployed function. Using the URL, including the function key, that you copied from the previous step, append the query string &name=<yourname>
to the URL.
You can also paste the copied URL, including the function key, into the address bar of your web browser. Again, append the query string &name=<yourname>
to the URL before you execute the request.
Clean up resources
Other quickstarts in this collection build upon this quickstart. If you plan to continue on with subsequent quickstarts or with the tutorials, don't clean up the resources created in this quickstart. If you don't plan to continue, use the following command to delete all resources created in this quickstart:
az group delete --name myResourceGroup
Select y
when prompted.
Next steps
Learn more about developing Azure Functions locally using the Azure Functions Core Tools.
Feedback
Loading feedback...