-
Notifications
You must be signed in to change notification settings - Fork 830
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mahdi
committed
Dec 15, 2024
1 parent
a7f992e
commit 9617ab4
Showing
17 changed files
with
828 additions
and
1 deletion.
There are no files selected for viewing
Submodule 201-azure-api-integration-function-app
deleted from
5416ab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform_configs/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
|
92 changes: 92 additions & 0 deletions
92
quickstart/201-azure-api-integration-function-app/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
10 changes: 10 additions & 0 deletions
10
quickstart/201-azure-api-integration-function-app/function_code/.funcignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
48 changes: 48 additions & 0 deletions
48
quickstart/201-azure-api-integration-function-app/function_code/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
quickstart/201-azure-api-integration-function-app/function_code/host.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
quickstart/201-azure-api-integration-function-app/function_code/local.settings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"FUNCTIONS_WORKER_RUNTIME": "node" | ||
} | ||
} | ||
|
Oops, something went wrong.