How to Write and Deploy a Basic Azure Function in Python with an HTTP Trigger
Azure Functions is a serverless compute service that lets you run event-driven code without the need for managing infrastructure. In this blog, we’ll explore how to write a basic Azure Function in Python with an HTTP trigger and deploy it using the Azure Portal.
Prerequisites
Before we begin, ensure you have the following:
- An active Azure account.
- Basic knowledge of Python.
- Familiarity with the Azure Portal.
Step 1: Set Up an Azure Function App
- Log in to the Azure Portal: Navigate to Azure Portal and log in with your credentials.
- Create a New Function App:
- Search for “Function App” in the search bar.
- Click on Create to start the process.
- Fill in the required details:
- Subscription: Select your Azure subscription.
- Resource Group: Create a new resource group or use an existing one.
- Function App Name: Provide a globally unique name for your function app.
- Publish: Select “Code”.
- Runtime Stack: Select Python (or Node.js, if that’s your preference).
- Version: Choose Python 3.8 or above.
- Region: Choose a region closest to you.
3. Hosting:
- For Operating System, select Linux.
- For Plan type, choose Consumption (Serverless) unless you have specific scaling needs.
4. Monitoring:
- Enable Application Insights for monitoring (optional, but recommended).
5. Review and Create:
- After reviewing your settings, click Create to deploy your Function App.
Step 2: Create a New Azure Function with HTTP Trigger
Once your Function App is created, it’s time to add the function.
- Go to the Function App:
- In the Azure portal, navigate to Function Apps and select the Function App you just created.
2. Add a New Function:
- In the Functions tab, click on + Add.
- Choose HTTP trigger from the template options.
- Provide a Function Name (e.g.,
HttpTriggerFunction
). - Set the Authorization level to Anonymous (for testing purposes, you can later change this based on security requirements).
- Click Create to add the function.
Step 3: Write Your Python Code
Now that the function is created, let’s write the code for the HTTP trigger.
- Navigate to the Code Editor:
- Go to your newly created function.
- Select Code + Test from the left menu to access the code editor.
2. Write the Function Code: The default code will be a basic HTTP trigger, but we can modify it to suit our needs. Here’s an example Python code for an Azure Function:
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 is your Azure Function!")
else:
return func.HttpResponse(
"Please pass a name in the query string or in the request body",
status_code=400
)
- This function checks for a
name
parameter in the query string or request body. If a name is provided, it responds with a greeting. If not, it returns an error message.
Step 4: Test the Function Locally (Optional)
You can test the function locally before deploying it by using the Azure Functions Core Tools.
- Install Azure Functions Core Tools: If you want to test locally, install the Azure Functions Core Tools.
- Run the Function: Run the following command in your project folder:
func start
Your function should now be available at http://localhost:7071
.
Step 5: Deploy the Function via Azure Portal
Now that we have written our function, we will deploy it.
- Navigate to the Function App: In the Azure Portal, go to Deployment Center under your Function App.
- Choose Deployment Option:
- You can choose various deployment options like GitHub, Azure Repos, or Local Git.
- If you’re deploying from the Azure Portal directly, you can use Code + Test to directly edit and deploy your code.
3. Deploy the Code: The code you edited in Code + Test will be deployed once saved.
Step 6: Test the Function in Azure
- Get the Function URL:
- In the Function App, go to Functions.
- Select your function, and click on Get Function URL.
2. Test the Function:
- Open the URL in your browser or use Postman to send an HTTP GET or POST request.
- Example URL:
https://<Your_Function_App_Name>.azurewebsites.net/api/HttpTriggerFunction?name=John
Check the Response: If everything is working correctly, you should see:
Hello, John. This is your Azure Function!
Conclusion
In this blog, we covered the steps to create a simple HTTP-triggered Azure Function in Python, deploy it via the Azure Portal, and test it. Azure Functions offers an excellent serverless platform to run code in response to events, without worrying about infrastructure management.