title | description | ms.date | ms.topic | ms.devlang | ms.custom |
---|---|---|---|---|---|
Create a TypeScript function from the command line - Azure Functions |
Learn how to create a TypeScript function from the command line, then publish the local project to serverless hosting in Azure Functions. |
11/18/2021 |
quickstart |
typescript |
devx-track-azurecli, devx-track-azurepowershell, mode-other |
[!INCLUDE functions-language-selector-quickstart-cli]
In this article, you use command-line tools to create a TypeScript function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
There is also a Visual Studio Code-based version of this article.
Before you begin, you must have the following:
-
An Azure account with an active subscription. Create an account for free.
-
The Azure Functions Core Tools version 4.x.
-
One of the following tools for creating Azure resources:
-
Azure CLI version 2.4 or later.
-
The Azure Az PowerShell module version 5.9.0 or later.
-
-
Node.js version 14 or 16 (preview).
Verify your prerequisites, which depend on whether you are using Azure CLI or Azure PowerShell for creating Azure resources:
-
In a terminal or command window, run
func --version
to check that the Azure Functions Core Tools are version 4.x. -
Run
az --version
to check that the Azure CLI version is 2.4 or later. -
Run
az login
to sign in to Azure and verify an active subscription.
-
In a terminal or command window, run
func --version
to check that the Azure Functions Core Tools are version 4.x. -
Run
(Get-Module -ListAvailable Az).Version
and verify version 5.0 or later. -
Run
Connect-AzAccount
to sign in to Azure and verify an active subscription.
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
-
Run the
func init
command, as follows, to create a functions project in a folder named LocalFunctionProj with the specified runtime:func init LocalFunctionProj --typescript
-
Navigate into the project folder:
cd LocalFunctionProj
This folder contains various files for the project, including configurations files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
-
Add a function to your project by using the following command, where the
--name
argument is the unique name of your function (HttpExample) and the--template
argument specifies the function's trigger (HTTP).func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
func new
creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named function.json.
If desired, you can skip to Run the function locally and examine the file contents later.
index.ts exports a function that's triggered according to the configuration in function.json.
:::code language="typescript" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-TypeScript/index.ts":::
For an HTTP trigger, the function receives request data in the variable req
of type HttpRequest as defined in function.json. The return object, defined as $return
in function.json, is the response.
function.json is a configuration file that defines the input and output bindings
for the function, including the trigger type.
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-JavaScript/function.json":::
Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type httpTrigger
and output binding of type http
.
-
Run your function by starting the local Azure Functions runtime host from the LocalFunctionProj folder:
npm install npm start
Toward the end of the output, the following lines should appear:
... Now listening on: http://0.0.0.0:7071 Application started. Press Ctrl+C to shut down. Http Functions: HttpExample: [GET,POST] http://localhost:7071/api/HttpExample ...
[!NOTE] If HttpExample doesn't appear as shown below, you likely started the host from outside the root folder of the project. In that case, use Ctrl+C to stop the host, navigate to the project's root folder, and run the previous command again.
-
Copy the URL of your
HttpExample
function from this output to a browser and append the query string?name=<your-name>
, making the full URL likehttp://localhost:7071/api/HttpExample?name=Functions
. The browser should display a message likeHello Functions
:The terminal in which you started your project also shows log output as you make requests.
-
When you're ready, use Ctrl+C and choose
y
to stop the functions host.
[!INCLUDE functions-create-azure-resources-cli]
-
Create the function app in Azure:
az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location <REGION> --runtime node --runtime-version 14 --functions-version 4 --name <APP_NAME> --storage-account <STORAGE_NAME>
The az functionapp create command creates the function app in Azure. If you're using Node.js 16, also change
--runtime-version
to16
.New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount <STORAGE_NAME> -Runtime node -RuntimeVersion 14 -FunctionsVersion 4 -Location '<REGION>'
The New-AzFunctionApp cmdlet creates the function app in Azure. If you're using Node.js 16, change
-RuntimeVersion
to16
.
In the previous example, replace
<STORAGE_NAME>
with the name of the account you used in the previous step, and replace<APP_NAME>
with a globally unique name appropriate to you. The<APP_NAME>
is also the default DNS domain for the function app.This command creates a function app running in your specified language runtime under the Azure Functions Consumption Plan, which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see Monitor Azure Functions. The instance incurs no costs until you activate it.
Before you use Core Tools to deploy your project to Azure, you create a production-ready build of JavaScript files from the TypeScript source files.
-
Use the following command to prepare your TypeScript project for deployment:
npm run build:production
-
With the necessary resources in place, you're now ready to deploy your local functions project to the function app in Azure by using the func azure functionapp publish command. In the following example, replace
<APP_NAME>
with the name of your app.func azure functionapp publish <APP_NAME>
If you see the error, "Can't find app with name ...", wait a few seconds and try again, as Azure may not have fully initialized the app after the previous
az functionapp create
command.The publish command shows results similar to the following output (truncated for simplicity):
... Getting site publishing info... Creating archive for current directory... Performing remote build for functions project. ... Deployment successful. Remote build succeeded! Syncing triggers... Functions in msdocs-azurefunctions-qs: HttpExample - [httpTrigger] Invoke url: https://msdocs-azurefunctions-qs.azurewebsites.net/api/httpexample?code=KYHrydo4GFe9y0000000qRgRJ8NdLFKpkakGJQfC3izYVidzzDN4gQ==
[!INCLUDE functions-run-remote-azure-cli]
[!INCLUDE functions-streaming-logs-cli-qs]
[!INCLUDE functions-cleanup-resources-cli]
[!div class="nextstepaction"] Connect to an Azure Storage queue