Sample Serverless Application on Azure: Integration Between Function App and API Management with Terraform
This repository provides a working example of the integration between Azure Function App and Azure API Management Instance. Currently, there is no fully functioning example available, as existing samples are outdated and the documentation for both the Terraform AzureRM provider and Azure API Management contains significant gaps and errors. To address these challenges, I have submitted several pull requests to improve the documentation and am publishing this sample code to assist others in navigating this integration.
The following versions were used and confirmed to work. While other versions may also work, they have not been tested with this setup:
- Azure CLI: Version 2.65.0
- Terraform CLI: Version 1.9.7
- Terraform AzureRM Provider: Version 4.0 or later
- Azure Functions Core Tools: Version 4.0.6543
- Node.js: Version 22.9.0
- Node Package Manager (NPM): Version 10.8.3
-
Log in to your Azure account:
az login -
Retrieve the subscription ID and resource group name from your Azure environment. Update the
subscription_idandresource_group_namevalues in theterraform_configs/variables.tffile accordingly. To obtain the subscription ID and resource group name, you can use:az group list -
Navigate to the
terraform_configsfolder:cd terraform_configs -
Initialize and deploy the Terraform configuration:
terraform init terraform plan -out main.tfplan terraform apply main.tfplanNote: Deploying the Azure API Management instance can take up to 90 minutes. Please be patient.
-
Take note of the following outputs:
function_app_name: The name of your Function App, such asmyfuncappsbigwbgyzdync, which you will need to deploy your function code.frontend_url: The frontend URL of your API Management instance.
Azure Functions Core Tools has been used to package and deploy the Function App code. While other tools can be used, Terraform currently does not fully integrate with such deployment tools, requiring the functions to be deployed separately after each Terraform deployment. Automation can simplify this process using scripts.
-
Navigate to the
function_codefolder:cd ../function_code -
Install the code dependencies:
npm install -
Deploy the code:
func azure functionapp publish your_function_app_nameReplace
your_function_app_namewith thefunction_app_namefrom the Terraform outputs.Note: The
funcCLI may occasionally encounter errors, even if it reportsDeployment completed successfully. If thefunc azure functionapp publishcommand does not return theInvoke URL, rerun the command. -
Verify the deployment:
-
Backend URL (
Invoke URL): Send a GET request to this URL. The response should return a 200 status code with the following message:"Hello world, this is coming from Function App!" -
Frontend URL (
frontend_url): Send a GET request to this URL. If everything is correctly configured, the frontend should return the same 200 status code and message.
-
-
Remember: After each Terraform deployment, you may need to redeploy the Function App code.
Note
For easier development and debugging, CORS restrictions have been disabled by setting Access-Control-Allow-Origin: * within the function code. Once the application is running successfully, ensure CORS is re-enabled and properly configured to secure the application.
Integration between Azure Function App and Azure API Management can be challenging and prone to various issues. Due to the limited documentation, I have automated variables to reduce potential errors. If you plan to modify the code, keep the following considerations in mind:
- Ensure the
url_templateproperty in theazurerm_api_management_api_operationresource aligns with the function name specified in theInvoke URL. Avoid appending slashes to theurl_templatevalue. - Ensure that
https://your-function-app-name.azurewebsites.net/apiis consistently used in both the policy and theService URLin the backend configuration, and ensure it overrides any existing value in the backend.
This sample aims to bridge gaps in existing documentation and provide a working, reproducible example of Azure Function App and API Management integration. Contributions and feedback are welcome!