Build and deploy serverless applications with ease
Welcome to the Azure Functions documentation tutorial! This guide will walk you through the fundamental steps of creating and deploying your first Azure Function. Azure Functions is a serverless compute service that enables you to run code on-demand without explicit infrastructure management.
We'll start by creating a simple HTTP-triggered function. This function will respond to incoming HTTP requests.
Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:
azd init --template functions-python
This command will initialize a new project with a Python-based Azure Functions template. You'll be prompted to name your project and choose a location.
Next, create a new function within your project:
cd <your-project-name>
az func-python create --name MyHttpTrigger --template "HTTP trigger" --authlevel "anonymous"
This creates a function named MyHttpTrigger that's anonymously accessible.
Open Visual Studio Code and select "Create New Project..." from the Azure Functions extension.
MyHttpTrigger).Anonymous for easy testing).VS Code will generate the necessary files for your function.
(Illustrative image: VS Code Azure Functions creation interface)
Open the generated function file (e.g., MyHttpTrigger/__init__.py for Python). You'll see code similar to this (for Python):
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function executed successfully.",
status_code=200
)
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
This code:
Before deploying to Azure, it's essential to test your function locally.
In your project's root directory, run:
azd up
This command will build and run your function locally. It will also deploy it to Azure if you're connected to your subscription.
Ensure you have the Azure Functions Core Tools installed. In your project's root directory, run:
func start
The tools will compile your project and start a local development server. It will output the URL for your HTTP-triggered function.
Once the function is running, you should see output indicating the URL of your HTTP trigger. You can open this URL in your browser:
http://localhost:7071/api/MyHttpTrigger?name=World
You should see the message: "Hello, World. This HTTP triggered function executed successfully."
Deploying your function to Azure makes it accessible globally.
If you haven't already deployed with azd up, navigate to your project directory and run:
azd auth login
azd provision
azd deploy
azd auth login will prompt you to log in to your Azure account. azd provision creates the necessary Azure resources (like a Function App), and azd deploy uploads your code.
After deployment, the Azure Functions extension or the Azure CLI will provide you with the URL of your deployed function. You can test it from anywhere.
Congratulations! You've successfully created, tested, and deployed your first Azure Function. From here, you can explore: