Creating and Deploying Your First Azure Function: A Step-by-Step Guide
Azure Functions provides a serverless compute platform that allows you to run code without managing infrastructure. In this tutorial, we’ll create a simple HTTP-triggered Azure Function using Python and deploy it via the Azure portal.
Prerequisites
- An Azure account (If you don’t have one, you can create a free account)
- Basic knowledge of Python
Step 1: Create a Function App
- Log in to the Azure portal (https://portal.azure.com/).
- Click on “Create a resource” and search for “Function App”.
3. Click “Create” on the Function App page.
4. Fill in the basics:
- Subscription: Choose your subscription
- Resource Group: Create new or select existing
- Function App name: Enter a unique name
- Publish: Choose “Code”
- Runtime stack: Select “Python”
- Version: Choose the latest version (e.g., 3.9)
- Region: Select a region close to you
5. Click “Review + create”, then “Create”.
Step 2: Create a Function
- Once your Function App is deployed, go to the resource.
- In the left menu, under “Functions”, click “Functions”.
- Click “Add” to create a new function.
- Choose “HTTP trigger” as the template.
- Name your function (e.g., “HttpTrigger1”) and set Authorization level to “Anonymous” for this example.
- Click “Add” to create the function.
Step 3: Write the Function Code
- Click on your newly created function.
- In the left menu, under “Developer”, click “Code + Test”.
- Replace the default code with the following Python code:
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.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
- Click “Save” to save your changes.
Step 4: Test Your Function
- Click “Test/Run” in the top menu.
- In the “Query” tab, add a parameter: Key = “name”, Value = “YourName”.
- Click “Run” to test the function.
- You should see a successful response with your name in the output.
Step 5: Get the Function URL
- Click “Get Function URL” at the top of the page.
- Copy the URL provided.
Step 6: Test the Deployed Function
- Open a new browser tab and paste the function URL.
- Add “?name=YourName” to the end of the URL and press Enter.
- You should see the personalized response in your browser.
Conclusion
Congratulations! You’ve successfully created, deployed, and tested your first Azure Function using Python. This serverless approach allows you to run code efficiently without managing infrastructure. As you become more familiar with Azure Functions, you can explore more complex triggers and bindings to build powerful, scalable applications.
So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.
Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”
Connect me on Social Media: https://linktr.ee/mdshamsfiroz
Happy coding! Happy learning!