|
1 | | -# Apim-Samples |
2 | | -This repository provides a playground to safely experiment with and learn Azure API Management (APIM) policies in various architectures. |
| 1 | +# Azure API Management Samples |
| 2 | + |
| 3 | +This repository provides a playground to safely experiment with and learn Azure API Management (APIM) policies in various architectures. |
| 4 | + |
| 5 | +## Objectives |
| 6 | + |
| 7 | +1. Educate you on common APIM architectures we see across industries and customers. |
| 8 | +1. Empower you to safely experiment with APIM policies. |
| 9 | +1. Provide you with high-fidelity building blocks to further your APIM integration efforts. |
| 10 | + |
| 11 | +_Try it out, learn from it, apply it in your setups._ |
| 12 | + |
| 13 | +## Repo Structure |
| 14 | + |
| 15 | +### High-level |
| 16 | + |
| 17 | +- All _samples_ can be found in the `samples` folder. Samples showcase functionality and provide a baseline for your experimentation. |
| 18 | +- All _infrastructures_ can be found in the `infrastructure` folder. They provide the architectural underpinnings. |
| 19 | +- All shared code, modules, functionality, policies, etc. can be found in the `shared` folder. |
| 20 | + - Bicep _modules_ are versioned in the `bicep/modules` folder. Major changes require versioning. |
| 21 | + - Python _modules_ are found in the `python` folder. _They are not versioned yet but may be in the future._ |
| 22 | + - Reusable _APIM policies_ are found in the `apim-policies` folder. |
| 23 | + - Reusable Jupyter notebooks are found in the `jupyter` folder. |
| 24 | + |
| 25 | +### Sample Setup |
| 26 | + |
| 27 | +- Each sample uses an architecture infrastructure. This keeps the samples free of almost all setup. |
| 28 | +- Each infrastructure and sample features a `create.ipynb` for creation (and running) of the sample setup and a `main.bicep` file for IaC configuration. |
| 29 | +- Each infrastructure contains a `clean-up.ipynb` file to tear down everything in the infrastructure and its resource group. This reduces your Azure cost. |
| 30 | +- Samples (and infrastructures) may contain additional files specific to their use cases. |
| 31 | + |
| 32 | +### Infrastructure Architectures |
| 33 | + |
| 34 | +We provide several common architectural approaches to integrating APIM into your Azure ecosystem. While these are high-fidelity setups, they are not production-ready. Please refer to the [Azure API Management landing zone accelerator](https://learn.microsoft.com/azure/cloud-adoption-framework/scenarios/app-platform/api-management/landing-zone-accelerator) for up-to-date production setups. |
| 35 | + |
| 36 | +- [Simple API Management](./infrastructure/simple-apim) |
| 37 | + - Just the basics with a publicly accessible API Management intance fronting your APIs. This is the innermost way to experience and experiment with the APIM policies. |
| 38 | +- [API Management & Container Apps](./infrastructure/apim-aca) |
| 39 | + - APIs are often times implemented in containers that are running in Azure Container Apps. This architecture accesses the container apps publicly. It's beneficial to test both APIM and container app URLs here to contrast and compare experiences of API calls through and bypassing APIM. It is not intended to be a security baseline. |
| 40 | +- [Secure Front Door & API Management & Container Apps](./infrastructure/afd-apim) |
| 41 | + - A higher-fidelity implementation of a secured setup in which Azure Front Door connects to APIM via the new private link integration. This traffic, once it traverses through Front Door, rides entirely on Microsoft-owned and operated networks. Similarly, the connection from APIM to Container Apps is secured but through a VNet configuration (it is also entirely possible to do this via private link). It's noteworthy that we are using APIM Standard V2 here as we need the ability to accept a private link from Front Door. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Getting Started |
| 46 | + |
| 47 | +### Prerequisites |
| 48 | + |
| 49 | +These prerequisites apply broadly across all infrastructure and samples. If there are specific deviations, expect them to be noted there. |
| 50 | + |
| 51 | +- [Python 3.12](https://www.python.org/) installed |
| 52 | + - Python 3.13 may not have all dependencies ready yet. There have been issues during installs. |
| 53 | +- [VS Code](https://code.visualstudio.com/) installed with the [Jupyter notebook extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) enabled |
| 54 | +- [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) installed |
| 55 | +- [An Azure Subscription](https://azure.microsoft.com/free/) with Owner or Contributor+UserAccessAdministrator permissions. Execute [shared/jupyter/verify-az-account.ipynb](shared/jupyter/verify-az-account.ipynb) to verify. |
| 56 | +- [Sign in to Azure with Azure CLI](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively) |
| 57 | + |
| 58 | +### Initialization |
| 59 | + |
| 60 | +Run through the following steps to create a Python virtual environment before doing anything else: |
| 61 | + |
| 62 | +1. Open VS Code. |
| 63 | +1. Invoke the _Command Palette_ via the _View_ menu or a shortcut (on Windows: Ctrl + Shift + P). |
| 64 | +1. Select _Python: Create Environment_. |
| 65 | +1. Select _Venv_ as we want a local virtual environment. |
| 66 | +1. Select the desired, installed Python version. |
| 67 | +1. Check _requirements.txt_ to install the Python dependencies we need for this repo, then press _OK_. The install may take a few minutes. You can check on progress in the _OUTPUT_ window. |
| 68 | +1. Verify the virtual environment is set up. You should see a new _.venv_ directory with a _pyveng.cfg_ file and the Python version you selected earlier. |
| 69 | + |
| 70 | +The first time you run a Jupyter notebook, you'll be asked to install the Jupyter kernel package (ipykernel). |
| 71 | + |
| 72 | +### Running a Sample |
| 73 | + |
| 74 | +1. Locate the specific sample's `create.ipynb` file and adjust the parameters under the `User-defined Parameters` header as you see fit. |
| 75 | +1. Ensure that the specified infrastructure already exists in your subscription. If not, proceed to the desired infrastructure folder and execute its `create.ipynb` file. Wait until this completes before continuing. |
| 76 | +1. Execute the sample's `create.ipynb` file. |
| 77 | + |
| 78 | +Now that infrastructure and sample have been stood up, you can experiment with the policies, make requests against APIM, etc. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Development |
| 83 | + |
| 84 | +As you work with this repo, you will likely want to make your own customizations. There's little you need to know to be successful. |
| 85 | + |
| 86 | +The repo uses the bicep linter and has rules defined in `bicepconfig.json`. See the [bicep linter documentation](https://learn.microsoft.com/azure/azure-resource-manager/bicep/bicep-config-linter) for details. |
| 87 | + |
| 88 | +We welcome contributions! Please consider forking the repo and creating issues and pull requests to share your samples. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details. Thank you! |
| 89 | + |
| 90 | +### Testing |
| 91 | + |
| 92 | +Python modules and files in the `shared` folder are supported with tests in the `python-tests` folder. They can be invoked manually via `pytest -v` but are also run upon push to GitHub origin. The `pytest.ini` file in the root sets up the test structure. |
| 93 | + |
| 94 | +Additional information can be found [here](# https://docs.pytest.org/en/8.2.x/). |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Supporting Resources |
| 99 | + |
| 100 | +The APIM team maintains an [APIM policy snippets repo](https://github.com/Azure/api-management-policy-snippets) with use cases we have seen. They are not immediately executable samples and require integrations such as in this repo. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Acknowledgements |
| 105 | + |
| 106 | +This project has its roots in work done by [Alex Vieira](https://github.com/vieiraae) on the excellent Azure API Management [AI Gateway](https://github.com/Azure-Samples/AI-Gateway) GitHub repository. Much of the structure is similar and its reuse resulted in significant time savings. Thank you, Alex! |
| 107 | + |
| 108 | +Furthermore, [Houssem Dellai](https://github.com/HoussemDellai) was instrumental in setting up a working Front Door to API Management [private connectivity lab](https://github.com/Azure-Samples/AI-Gateway/tree/main/labs/private-connectivity). This created a working baseline for one of this repository's infrastructures. Thank you, Houssem! |
0 commit comments