Azure Functions: Hello World
This tutorial guides you through creating your first Azure Function that responds with "Hello, World!". We'll cover the essentials of setting up your local development environment and deploying your function to Azure.
- Azure Account: If you don't have one, create a free account.
- Azure Functions Core Tools: Install globally using npm:
npm install -g azure-functions-core-tools@4 --unsafe-perm true - Node.js: Install the LTS version from nodejs.org.
Step 1: Create a New Project
Open your terminal or command prompt and navigate to the directory where you want to create your project. Run the following command to initialize a new Azure Functions project:
func init MyHelloWorldFunction --worker-runtime node
This command creates a new folder named MyHelloWorldFunction and initializes it as an Azure Functions project with Node.js as the runtime.
Step 2: Create a New Function
Navigate into your new project directory:
cd MyHelloWorldFunction
Now, create a new HTTP-triggered function. You'll be prompted for a name and authorization level. For this tutorial, we'll use "HttpTrigger" and "Anonymous" authorization.
func new --template HttpTrigger --name HttpTrigger1
This creates a new folder named HttpTrigger1 within your project, containing the necessary files for an HTTP-triggered function.
Step 3: Implement the "Hello World" Logic
Open the index.js file inside the HttpTrigger1 folder. Replace its contents with the following code:
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!"
: "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, /* Defaults to 200 */
body: responseMessage
};
};
This code checks for a name parameter in the query string or request body and returns a personalized greeting.
Step 4: Run the Function Locally
Start the Azure Functions host by running:
func start
The output will show the URLs where your function is accessible. Look for a URL similar to this:
Http Functions:HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1
Open your web browser and navigate to the URL. You can test it with or without a name parameter:
http://localhost:7071/api/HttpTrigger1http://localhost:7071/api/HttpTrigger1?name=Azure
You should see the "Hello, World!" message in your browser.
Step 5: Deploy to Azure
To deploy your function to Azure, you need to log in to your Azure account:
az login
If you haven't already, create an Azure Resource Group and a Storage Account. You can do this via the Azure portal or using the Azure CLI.
Then, deploy your function app:
func azure functionapp publish <YourFunctionName>
Replace <YourFunctionName> with a unique name for your function app in Azure.
Note: The first time you deploy, you may be prompted to create a new Function App resource in Azure. Follow the prompts.
Conclusion
Congratulations! You've successfully created and deployed your first Azure Function. You can now access your deployed function from anywhere by navigating to its public URL.