Introduction to Azure Functions and API Management

Azure Functions provide a serverless compute service that enables you to run code on-demand without explicit infrastructure provisioning and management. Azure API Management (APIM) is a fully managed service that enables customers to publish, secure, transform, maintain, and monitor APIs.

By combining these two powerful Azure services, you can:

  • Develop and deploy business logic as event-driven functions.
  • Expose these functions as robust APIs through API Management.
  • Implement security policies, rate limiting, and access control.
  • Gain insights into API usage and performance.
  • Simplify the developer experience for consuming your APIs.

Key Concepts

Before we dive into the implementation, let's define some core concepts:

  • Azure Functions: Event-driven compute, runs code in response to triggers (HTTP, Timer, etc.).
  • API Management Gateway: The single entry point for all client requests to your APIs.
  • Policies: Declarative XML snippets that define how requests and responses are processed by the gateway.
  • Products: Groupings of APIs that can be offered to developers.
  • Subscriptions: Allow developers to access APIs via an API key.

Scenario: A Simple To-Do List API

In this tutorial, we'll build a simple API to manage a to-do list. This will involve:

  1. Creating an Azure Function that exposes HTTP endpoints for CRUD operations (Create, Read, Update, Delete) on to-do items.
  2. Configuring an Azure API Management instance.
  3. Importing the Azure Function as an API in API Management.
  4. Applying basic policies to secure and manage the API.

Step 1: Create an Azure Function

First, let's create a basic Azure Function with HTTP triggers.

Prerequisites:

  • Azure Subscription
  • Azure Functions Core Tools installed
  • Node.js and npm (or your preferred language runtime)

Steps:

  1. Initialize a new Function App project:
  2. func init MyApiFunctions --worker-runtime node --language javascript
    cd MyApiFunctions
  3. Create an HTTP-triggered function:
  4. func new --name todo --template "HTTP trigger" --authlevel anonymous
  5. Modify the index.js file within the todo folder to handle basic CRUD operations. (For brevity, we'll assume this is implemented with in-memory storage for this example.)

You can test this function locally using:

func start

Step 2: Configure Azure API Management

Next, set up an API Management instance in your Azure portal.

  1. Navigate to the Azure portal and search for "API Management services".
  2. Click "Create".
  3. Fill in the required details (Subscription, Resource Group, Region, Service name, etc.). Choose a pricing tier suitable for your needs (Developer tier is good for testing).
  4. Click "Review + create" and then "Create".

Step 3: Import Azure Function into API Management

Now, link your Azure Function to your APIM instance.

  1. Once your APIM instance is deployed, navigate to its resource page.
  2. Under "APIs", click "+ Add API".
  3. Select "Azure Function".
  4. Click "Browse" to select your Function App.
  5. Choose the specific HTTP-triggered function (e.g., "todo").
  6. Give your API a name and an API URL suffix (e.g., todoapi).
  7. Click "Create".

API Management will automatically create operations for your function's HTTP methods (GET, POST, PUT, DELETE).

Step 4: Apply Policies

Let's add a simple policy to require a subscription key.

  1. In your APIM instance, navigate to the API you just created.
  2. Go to the "Design" tab.
  3. Select the "All operations" scope.
  4. In the "Inbound processing" section, click the "+" icon to add a policy.
  5. Select "Set request header" or "Check subscription key". For this example, we'll focus on securing it. API Management automatically adds a check subscription key policy by default when importing. If not, you can add it from the policy snippets.

Example Policy Snippet (for reference, usually pre-added):

<policies>
    <inbound>
        <base />
        <check-header name="Ocp-Apim-Subscription-Key" required="true" />
        <set-backend-service base-url="https://your-function-app-url.azurewebsites.net" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Testing Your API

You can test your API using the "Test" tab in the Azure portal:

  1. Go to the "Test" tab for your API.
  2. Select an operation (e.g., GET).
  3. Ensure you have a valid subscription key (you can find these under "Subscriptions" in your APIM instance).
  4. Click "Send".

Conclusion

You've successfully set up an Azure Function and exposed it through Azure API Management, gaining capabilities like centralized security, traffic management, and a developer portal. This is a foundational setup that can be extended with more advanced policies, authentication methods (like OAuth 2.0), and sophisticated function logic.

Get Started with Azure Functions