Azure Functions is a serverless compute service that enables you to run code on-demand without explicitly provisioning or managing infrastructure. With Azure Functions, you can build applications by writing code in your preferred language and pay only for the time your code runs.
Prerequisites
Before you begin, ensure you have the following:
- An Azure subscription. If you don't have one, create a free account before you begin.
- The Azure Functions Core Tools installed locally.
- A Node.js runtime installed (version 14 or later recommended).
- A code editor like Visual Studio Code (recommended).
Step 1: Create a new Azure Functions project
You can create a new Azure Functions project using the Azure Functions Core Tools or your IDE.
Using Azure Functions Core Tools
Open a terminal or command prompt, navigate to a folder where you want your project to reside, and run the following command:
func init MyFunctionProj --worker-runtime node --language javascript
This command initializes a new project named MyFunctionProj with a Node.js runtime and JavaScript as the language.
Using Visual Studio Code
- Install the Azure Functions extension for Visual Studio Code.
- Open VS Code and select File > Open Folder....
- Create a new folder for your project and open it.
- In the Azure activity bar, select the Azure Functions icon.
- In the Workspace (local) section, select the Create New Project... button.
- Choose the folder you opened and then select your desired language (e.g., JavaScript).
- Select a template for your function (e.g., HTTP trigger).
- Provide a name for your function (e.g.,
HttpExample) and an authorization level.
Step 2: Create a function
Once your project is created, you can add new functions to it. A common scenario is to create an HTTP-triggered function.
Creating an HTTP Triggered Function
To create a new function within your existing project:
- In Visual Studio Code, open the Azure Functions extension sidebar.
- Click the Create Function... button.
- Select the function project directory.
- Choose the template, for example, HTTP trigger.
- Provide a name for your function, such as
Greetings. - Choose an authorization level (e.g., Function or Anonymous).
This will generate a new file (e.g., Greetings/index.js) and an associated configuration file (Greetings/function.json).
The function.json file defines your function's triggers, bindings, and other configuration. For HTTP triggers, it specifies the HTTP method and route.
Step 3: Write your function code
Open the generated JavaScript file for your HTTP trigger function. Here's a basic example:
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + "!"
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.res = {
status: 200,
body: responseMessage
};
};
In this code:
context: Provides information about the function's execution and allows you to log messages.req: Represents the incoming HTTP request.context.res: Used to set the HTTP response.
Step 4: Run your function locally
To test your function locally, navigate to your project directory in the terminal and run:
func start
The Azure Functions host will start, and you'll see output indicating the URL of your HTTP-triggered function.
You can use tools like cURL, Postman, or your web browser to send requests to your local function URL. For example, if your function is at http://localhost:7071/api/HttpExample, you can test it with:
curl http://localhost:7071/api/HttpExample?name=Azure
Step 5: Deploy to Azure
Once you're satisfied with your local testing, you can deploy your function to Azure.
Using Visual Studio Code
- Ensure you are signed into Azure in VS Code.
- Right-click on your project folder in the Azure Functions explorer.
- Select Deploy to Function App....
- Choose an existing Function App or create a new one. Follow the prompts to configure it.
Using Azure Functions Core Tools
func azure functionapp publish
Replace <YourFunctionAppName> with the name of your Azure Function App.
Ensure your Function App is created with the correct runtime stack (e.g., Node.js) and operating system.
Next Steps
Congratulations! You've created and deployed your first Azure Function. Consider exploring:
- Other trigger types (e.g., Timer, Blob, Queue).
- Input and Output Bindings to integrate with other Azure services.
- Monitoring and logging for your functions.
- Error handling and debugging strategies.