Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example integration between azurerm_api_management and azurerm_linux_function_app #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform_configs/*
2 changes: 2 additions & 0 deletions quickstart/201-azure-api-integration-function-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/

92 changes: 92 additions & 0 deletions quickstart/201-azure-api-integration-function-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Azure API Management Integration with Azure Function App

This template provisions an Azure API Management instance and a Linux Function App, integrating them seamlessly. It includes the creation of API Management operations, the application of policies, and the configuration of front-end and back-end URLs for a fully functional API setup.

# Requirements

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

# How to Run

## Deploy Infrastructure

1. Log in to your Azure account:
```
az login
```

2. Retrieve the subscription ID and resource group name from your Azure environment. Update the `subscription_id` and `resource_group_name` values in the `terraform_configs/variables.tf` file accordingly. To obtain the subscription ID and resource group name, you can use:
```
az group list
```

3. Navigate to the `terraform_configs` folder:
```
cd terraform_configs
```

4. Initialize and deploy the Terraform configuration:
```
terraform init
terraform plan -out main.tfplan
terraform apply main.tfplan
```

**Note:** Deploying the Azure API Management instance can take up to 90 minutes. Please be patient.

5. Take note of the following outputs:
- **`function_app_name`**: The name of your Function App, such as `myfuncappsbigwbgyzdync`, which you will need to deploy your function code.
- **`frontend_url`**: The frontend URL of your API Management instance.

## Deploy the Code to the Function App

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.

1. Navigate to the `function_code` folder:
```
cd ../function_code
```

2. Install the code dependencies:
```
npm install
```

3. Deploy the code:
```
func azure functionapp publish your_function_app_name
```
Replace `your_function_app_name` with the `function_app_name` from the Terraform outputs.

**Note:** The `func` CLI may occasionally encounter errors, even if it reports `Deployment completed successfully`. If the `func azure functionapp publish` command does not return the `Invoke URL`, rerun the command.

4. 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.

5. 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.

# Additional Notes

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_template` property in the `azurerm_api_management_api_operation` resource aligns with the function name specified in the `Invoke URL`. Avoid appending slashes to the `url_template` value.
- Ensure that `https://your-function-app-name.azurewebsites.net/api` is consistently used in both the policy and the `Service URL` in 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!
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.js.map
*.ts
.git*
.vscode

test
getting_started.md
node_modules/@types/
node_modules/azure-functions-core-tools/
node_modules/typescript/
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
bin
obj
csx
.vs
edge
Publish

*.user
*.suo
*.cscfg
*.Cache
project.lock.json

/packages
/TestResults

/tools/NuGet.exe
/App_Data
/secrets
/data
.secrets
appsettings.json


node_modules
dist

# Local python packages
.python_packages/

# Python Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node"
}
}

Loading