Quickstart: Create your first Azure Function
This quickstart guide will walk you through the steps to create your first Azure Function, a serverless compute service that enables you to run code on demand without explicitly provisioning or managing infrastructure.
Prerequisites
- An Azure subscription: If you don't have one, create a free account.
- Azure Functions Core Tools installed.
- Azure CLI installed.
- A code editor like Visual Studio Code (recommended).
Step 1: Create a new project
Open your terminal or command prompt and run the following command to create a new Functions project:
func init MyFunctionProject --worker-runtime node --language javascript
This command initializes a new project named MyFunctionProject
using Node.js as the runtime and JavaScript as the language.
Step 2: Create a new function
Navigate into your project directory and create a new function:
cd MyFunctionProject
func new --template "HTTP trigger" --name MyHttpTrigger
This command creates a new function named MyHttpTrigger
with an HTTP trigger template.
Step 3: Explore the project files
Your project structure will now look something like this:
MyFunctionProject/
├── MyHttpTrigger/
│ ├── function.json
│ └── index.js
├── host.json
├── local.settings.json
└── package.json
MyHttpTrigger/index.js
: Contains your function code.MyHttpTrigger/function.json
: Configures your function's triggers and bindings.host.json
: Configures global settings for the Azure Functions host.local.settings.json
: Stores app settings and connection strings for local development.
Step 4: Run your function locally
Start the Functions host locally:
func start
You should see output indicating that your function is running and the URL it's listening on. Typically, it will be something like http://localhost:7071/api/MyHttpTrigger
.
Open your browser
Navigate to the URL provided in the func start
output. You can also send a GET or POST request to this URL.
Test the function
If you try accessing http://localhost:7071/api/MyHttpTrigger?name=Azure
in your browser, you should see a response like:
Hello, Azure. This HTTP triggered function executed successfully.
Step 5: Deploy your function
To deploy your function to Azure, you first need to create an Azure Functions app resource. You can do this using the Azure CLI:
az functionapp create --resource-group MyResourceGroup --consumption-plan-location westus --runtime node --functions-version 4 --name MyUniqueFunctionAppName --storage-account MyStorageAccountName
Replace MyResourceGroup
, westus
, MyUniqueFunctionAppName
, and MyStorageAccountName
with your own values.
Once the app is created, you can deploy your code using the Azure Functions Core Tools:
func azure functionapp publish MyUniqueFunctionAppName
Get Started with Azure Functions