Create an Azure Function
This guide walks you through the process of creating your first Azure Function using the Azure portal. Azure Functions provides a serverless compute experience that enables you to build and run application code without provisioning or managing infrastructure.
Prerequisites
Before you begin, ensure you have:
- An Azure account. If you don't have one, you can create a free account.
- An Azure resource group. If you don't have one, you can create one in the Azure portal.
Steps to Create a Function
-
Sign in to the Azure portal
Navigate to the Azure portal and sign in with your Azure account credentials.
-
Create a new Function App
A Function App is the execution container for your individual functions. To create one:
- In the Azure portal search bar, type "Function App" and select it from the results.
- Click + Create.
- On the Basics tab, configure the following settings:
- Subscription: Select your Azure subscription.
- Resource Group: Select an existing resource group or create a new one.
- Function App name: Enter a globally unique name for your function app. This name becomes the DNS domain name. For example,
my-unique-function-app-name. - Publish: Choose Code.
- Runtime stack: Select your preferred language (e.g., .NET, Node.js, Python, Java). For this example, let's choose Node.js.
- Version: Select the appropriate version for your chosen runtime.
- Region: Choose the Azure region closest to your users.
- Click Next: Hosting.
- Configure your hosting options (Storage account, Operating System, Plan type). For simplicity, we'll use the Consumption plan.
- Click Next: Monitoring, then Next: Tags, and finally Review + create.
- Review your configuration and click Create. The deployment may take a few minutes.
Once deployed, navigate to your new Function App resource.
-
Create your first function
Inside your Function App, select Functions from the left-hand menu, then click + Create.
You will be presented with several options for creating a function:
- Develop in portal: Quick creation and editing directly in the browser.
- Develop from package: Deploy from a ZIP package.
- Other quick templates: Use pre-built templates for common scenarios.
For this guide, we'll use the Develop in portal option.
On the Create a function page:
- Choose a template. Select HTTP trigger. This template creates a function that is triggered by an HTTP request.
- Provide a New function name, for example,
MyHttpTriggerFunction. - Set the Authorization level. For simplicity, choose Anonymous. This means anyone can call the function without a key. For production, consider Function or Admin.
- Click Create.
-
View and test your function
Once your function is created, you will see its code editor. You can now test your function:
- Click Get Function URL from the top menu. Copy the URL provided.
- Open a new browser tab or use a tool like
curlto make a GET request to the URL. You should see a response like:Hello, Your Function App name. This HTTP triggered function executed successfully.You can also test with a POST request and pass a name in the request body:
curl -w "\n" -X POST "{your_function_url}" -d '{"name": "World"}' -H "Content-Type: application/json"The response should be:
Hello, World. This HTTP triggered function executed successfully.
Next Steps
Congratulations on creating your first Azure Function! You can now explore other trigger types, integrate with various Azure services using bindings, and learn about deployment strategies.
- Explore Triggers and Bindings to understand how your functions can interact with other services.
- Learn about Deployment options to deploy your code from your local machine or CI/CD pipelines.
- Understand Monitoring to keep track of your function's performance and troubleshoot issues.