Below is an example project to create, test, and deploy an Azure Function using HTTP Trigger to process and return data. This project assumes you’re using Visual Studio Code with the Azure Functions Extension.
Create an Azure Function that accepts a JSON payload via an HTTP POST request, processes the data, and returns a response.
- Install Visual Studio Code.
- Install the Azure Functions Core Tools.
- Install the Azure CLI.
- Install the Azure Functions Extension in VS Code.
- Open VS Code.
- Use the Azure Functions extension:
- Click on "Azure" in the Activity Bar.
- Select "Create New Project".
- Choose a directory for your project.
- Select a language (e.g., Python, C#, JavaScript, etc.).
- Choose the template:
HTTP Trigger
. - Provide a function name:
ProcessDataFunction
. - Set authorization level to
Anonymous
for testing.
project/
│
├── ProcessDataFunction/
│ ├── function.json
│ ├── __init__.py (for Python)
│ ├── index.js (for JavaScript)
│ ├── main.cs (for C#)
├── requirements.txt (Python dependencies)
├── local.settings.json
Edit ProcessDataFunction/__init__.py
:
import json
import logging
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Processing HTTP request.')
try:
req_body = req.get_json()
name = req_body.get('name')
age = req_body.get('age')
if name and age:
response = {
"message": f"Hello {name}, you are {age} years old!"
}
return func.HttpResponse(json.dumps(response), status_code=200)
else:
return func.HttpResponse(
"Invalid input. Please provide 'name' and 'age'.",
status_code=400
)
except ValueError:
return func.HttpResponse(
"Invalid JSON format.",
status_code=400
)
- Open a terminal and navigate to your project folder.
- Run the following command:
func start
- Test the function using a tool like Postman or cURL:
curl -X POST http://localhost:7071/api/ProcessDataFunction \ -H "Content-Type: application/json" \ -d '{"name": "Atul", "age": 30}'
- Login to Azure:
az login
- Deploy the Function:
- In VS Code, click on the Azure icon in the Activity Bar.
- Right-click your function app and select "Deploy to Function App".
- Create a new Function App when prompted.
- Get the function's URL from the Azure Portal or VS Code.
- Test using the same JSON payload as above.
You can add input/output bindings (e.g., Cosmos DB, Blob Storage) by modifying the function.json
file and connecting your function to Azure resources.
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["post"]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
Create a GitHub repository for version control. Push your project using the following:
git init
git add .
git commit -m "Initial commit for Azure Functions project"
git branch -M main
git remote add origin <your-repo-url>
git push -u origin main
This is a basic setup for Azure Functions. Let me know if you'd like to explore advanced topics like bindings, deployment pipelines, or integrating with other Azure services! Here’s how you can run the Azure Functions project locally:
-
Install Required Tools:
- Visual Studio Code
- Azure Functions Core Tools
- Azure CLI
- Language-specific runtime:
- Node.js (for JavaScript)
- Python (for Python)
- .NET SDK (for C#)
-
Install Extensions:
- In Visual Studio Code, install the Azure Functions extension.
-
Verify Installation: Run these commands in your terminal to check installations:
func --version # Azure Functions Core Tools az --version # Azure CLI
- Open your Azure Functions project folder in Visual Studio Code.
- Navigate to your project folder in the terminal.
- Install the dependencies:
- JavaScript:
npm install
- Python:
pip install -r requirements.txt
- JavaScript:
- Ensure
local.settings.json
has the correct configuration:- Node.js (JavaScript):
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "node" } }
- Python:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "python" } }
- C#:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet" } }
- Node.js (JavaScript):
Run the following command in the terminal:
func start
This will start the Azure Functions host and display the local URL for your HTTP Trigger function, such as:
http://localhost:7071/api/ProcessDataFunction
Use a tool like Postman, cURL, or your browser to test the function.
-
Postman (for HTTP POST):
- Set the method to
POST
. - URL:
http://localhost:7071/api/ProcessDataFunction
- Body (JSON):
{ "name": "Atul", "age": 30 }
- Set the method to
-
cURL:
curl -X POST http://localhost:7071/api/ProcessDataFunction \ -H "Content-Type: application/json" \ -d '{"name": "Atul", "age": 30}'
After verifying it works locally, you can deploy it to Azure using Visual Studio Code or the Azure CLI:
-
From VS Code:
- Click the Azure icon in the Activity Bar.
- Right-click your Function App and select Deploy to Function App.
-
Using Azure CLI:
func azure functionapp publish <Your-Function-App-Name>
Replace <Your-Function-App-Name>
with the name of your Azure Function App.
Let me know if you encounter any issues during setup or testing!