【问题标题】:How to deploy automatically an Azure API management?如何自动部署 Azure API 管理?
【发布时间】:2019-05-03 08:21:33
【问题描述】:

在 API 管理中,我创建了一个能够调用无服务器函数应用的 API。现在我想自动部署这个功能。以下是我在网上看到的可能性:

  • 通过门户创建和配置api管理(这不是我所说的自动部署)
  • 使用 Powershell 命令(不幸的是我正在使用 linux)
  • ARM(Azure 资源管理器):这并不容易,我确实找到了如何使用 Azure 函数应用创建 API
  • Terraform:与 ARM 相同,我不清楚如何使用 Azure 函数应用创建 API

如果有人有经验、链接或想法,我将非常感激。

问候,

【问题讨论】:

  • 您可以访问aka.ms/apimdevops,获取有关自动化部署的指导。目前,无法通过 API 导入 Function App。您可以在浏览器中打开开发人员工具并跟踪 API 管理在 Azure 门户中进行的调用,以从 Function App 创建 API,然后从您自己的脚本中重做这些调用。
  • 谢谢,我现在清楚了。
  • Azure API 管理的部署仍然是一个完全的讽刺。老实说,这是最糟糕的部分。 CI/CD 真的是事后才想到的,而 ARM 模板的故事非常复杂,还有很多需要考虑的事情没有解决。综上所述,一旦你完成了自动化工作,它就会得到回报。完全同意上述选项中使用 terraform 的建议。我确实尝试了 dotnet-apim 工具,看起来很有趣,并且自上次查看以来取得了进步。

标签: azure terraform azure-api-management


【解决方案1】:

我们目前正在将 Terraform 用于我们所有的 Azure 基础架构,包括 API 管理,我强烈推荐它。

它正在创建和更新我们想要的一切,包括 API 策略,并且具有相对较小的学习曲线

您可以从这里开始学习:

https://learn.hashicorp.com/terraform?track=azure#azure

APIM 的文档在这里:

https://www.terraform.io/docs/providers/azurerm/r/api_management.html

一旦完成初始学习曲线,剩下的就很容易了,而且好处很多。

【讨论】:

  • 您有使用 Terraform 和 APIM 的真实示例吗?该文档看起来对于已经知道他们在做什么的人来说是一个很好的参考。我发现自己从文档中复制/粘贴,编辑以匹配我尝试创建的资源,然后在我执行terraform apply 时一个又一个错误地调试错误。
【解决方案2】:

Azure Powershell 现在是 100% cross platform,所以这是一个选项。以下是一些示例:https://docs.microsoft.com/en-us/azure/api-management/powershell-samples

您也可以使用ARM Templates 来启动它。配置它要困难得多。您可以将任何these calls 映射到 ARM 模板。

Terraform - 我认为它仍在开发中。 https://github.com/terraform-providers/terraform-provider-azurerm/issues/1177。但我不会那样做。

【讨论】:

    【解决方案3】:

    ARM 是要走的路。

    您可以将其与:

    【讨论】:

      【解决方案4】:

      查看 Azure API 管理 DevOps 资源工具包:

      https://github.com/Azure/azure-api-management-devops-resource-kit

      【讨论】:

        【解决方案5】:

        我认为自动部署 Azure APIM 最方便的方法是dotnet-apim。这是一个跨平台的解决方案,您可以轻松地在您的开发机器或 ci/cd 管道上使用它。

        1. 确保您已安装 .NET Core。
        2. 安装dotnet-apim工具。
        3. 在 yaml 文件中,您可以定义 APIVersionSet、API、产品、后端、标签等列表。此 YAML 文件定义您要部署到 APIM 的内容。您可以将它放在源代码控制中以获取更改历史记录。以下 YAML 文件定义了 2 个集合、API 和产品及其策略。
        version: 0.0.1   # Required
        apimServiceName: $(apimServiceName)   # Required, must match name of an apim service deployed in the specified resource group
        
        apiVersionSets:
        
            - name: Set1
              displayName: API Set 1 
              description: Contains Set 1 APIs.
              versioningScheme: Segment 
        
            - name: Set2
              displayName: API Set 2
              description: Contains Set 2 APIs.
              versioningScheme: Segment 
        
        apis:
        
            - name: API1
              displayName: API v1
              openApiSpec: $(apimBasePath)\Apis\OpenApi.json    # Required, can be url or local file
              policy: $(apimBasePath)\Apis\ApiPolicy.xml
              path: api/sample1
              apiVersion: v1
              apiVersionSetId: Set1
              apiRevision: 1
              products: AutomationTests, SystemMonitoring
              protocols: https
              subscriptionRequired: true
              isCurrent: true
              operations:
                  customer_get: # it's operation id
                      policy: $(apimBasePath)\Apis\HealthCheck\HealthCheckPolicy.xml:::BackendUrl=$(attachmentServiceUrl)
              subscriptionKeyParameterNames: 
                 header: ProviderKey
                 query: ProviderKey 
        
        
            - name: API2
              displayName: API2 v1 [Staging]
              openApiSpec: $(apimBasePath)\Apis\OpenApi.json    # Required, can be url or local file
              policy: $(apimBasePath)\Apis\ApiPolicy.xml
              path: api/sample2
              apiVersion: v1
              apiVersionSetId: Set2
              apiRevision: 1
              products: AutomationTests, SystemMonitoring
              protocols: https
              subscriptionRequired: true
              isCurrent: true  
              subscriptionKeyParameterNames: 
                 header: ProviderKey
                 query: ProviderKey   
        
        products:
        
            - name: AutomationTests
              displayName: AutomationTests
              description: Product for automation tests
              subscriptionRequired: true
              approvalRequired: true
              subscriptionsLimit: 1
              state: published
              policy: $(apimBasePath)\Products\AutomationTests\policy.xml
        
            - name: SystemMonitoring
              displayName: SystemMonitoring
              description: Product for system monitoring
              subscriptionRequired: true
              approvalRequired: true
              subscriptionsLimit: 1
              state: published
              policy: $(apimBasePath)\Products\SystemMonitoring\policy.xml
        
        
        outputLocation: $(apimBasePath)\output
        linkedTemplatesBaseUrl : $(linkedTemplatesBaseUrl)  # Required if 'linked' property is set to true
        
        1. $(variableName) 是一种用于在 YAML 文件中定义变量的语法,这使得在 ci/cd 场景中的定制更加容易。

        2. 下一步是将 YAML 文件转换为 Azure 可以理解的 ARM。

        dotnet-apim --yamlConfig "c:/apim/definition.yml" 
        
        1. 然后您必须将生成的 ARM 模板部署到 Azure,这在 here 中进行了解释。

        【讨论】:

          【解决方案6】:

          使用 test-api(如果您为 API Mgmt 进行了 microsoft 演示,您应该会认出它),这里有一个可以工作的 terraform 的 sn-p。不包括资源组 (thisrg)

          resource "azurerm_api_management" "apimgmtinstance" {
            name                = "${var.base_apimgmt_name}-${var.env_name}-apim"
            location            = azurerm_resource_group.thisrg.location
            resource_group_name = azurerm_resource_group.thisrg.name
            publisher_name      = "Marc Pub"
            publisher_email     = "marc@trash.com"
          
            sku_name = var.apimgmt_size
          
           /* policy {
              xml_content = <<XML
              <policies>
                <inbound />
                <backend />
                <outbound />
                <on-error />
              </policies>
          XML
          
            } */
          }
          
          resource "azurerm_api_management_product" "apiMgmtProductContoso" {
            product_id    = "contoso-marc"
            display_name  = "Contoso Marc"
            description   = "this is a test"
            subscription_required = true
            approval_required = true
            api_management_name = azurerm_api_management.apimgmtinstance.name
            resource_group_name = azurerm_resource_group.thisrg.name
            published = true
            subscriptions_limit = 2
            terms = "you better accept this or else... ;-)"
          }
          
          
          resource "azurerm_api_management_api" "testapi" {
            description = "this is a mock test"
            display_name = "Test API"
            name = "test-api"
            protocols =  ["https"]
            api_management_name = azurerm_api_management.apimgmtinstance.name
            resource_group_name = azurerm_resource_group.thisrg.name
            // version = "0.0.1"
            revision = "1"
            path = ""
            subscription_required = true
          
          } 
          
          data "azurerm_api_management_api" "testapi_data" {
            name = azurerm_api_management_api.testapi.name
            api_management_name = azurerm_api_management.apimgmtinstance.name
            resource_group_name = azurerm_resource_group.thisrg.name
            revision  = "1"
          }
          
          resource "azurerm_api_management_api_operation" "testapi_getop" {
            operation_id = "test-call"
            api_name = data.azurerm_api_management_api.testapi_data.name
            api_management_name = data.azurerm_api_management_api.testapi_data.api_management_name
            resource_group_name = data.azurerm_api_management_api.testapi_data.resource_group_name
            display_name = "Test call"
            method = "GET"
            url_template = "/test"
            description = "test of call"
            response {
              status_code = 200
              description = ""
              representation {
                content_type = "application/json"
                sample = "{\"sampleField\": \"test\"}"
              }
            }
          }
          
          resource "azurerm_api_management_api_operation_policy" "testapi_getop_policy" {
            api_name            = azurerm_api_management_api_operation.testapi_getop.api_name
            api_management_name = azurerm_api_management_api_operation.testapi_getop.api_management_name
            resource_group_name = azurerm_api_management_api_operation.testapi_getop.resource_group_name
            operation_id        = azurerm_api_management_api_operation.testapi_getop.operation_id
            xml_content = <<XML
                          <policies>
                            <inbound>
                              <mock-response status-code="200" content-type="application/json"/>
                            </inbound>
                          </policies>
                          XML
          }
          

          【讨论】:

          • 这是错误的,没有回答原始问题(天蓝色函数导入)。我想这就是我!
          • 我可能会有答案,这取决于您的 azure 函数是否具有 swagger 或 openapi。如果是这样,您可以通过(假设为 powershell)curl.exe -H "Content-type: application/json" -H "x-functions-key: &lt;your app key value&gt;" &lt;url&gt; 调用您的 azure 函数,然后您可以在使用类似 'https://yourfunction.azurewebsites.net/api/OpenAPI?format=json&amp;code=&lt;your app key value&gt; 的方式进行 terraform api 导入时尝试适当的 url
          【解决方案7】:

          terraform 现在主要支持 azure api 管理。我一直在使用https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management 和 terraform import 的组合将大部分现有的 azure api 管理实现到 terraform 中(只需在单独的文件夹中执行,将 terraform import 导入到 terraform.tfstate 文件中,如果你混淆了您正在导入的资源以及您正在创建的 tf 文件作为结果的 terraform.tfstate 文件(通过 terraform plan/apply 生成),您可能会意外删除要从中导入的资源。耶...

          它主要是这样做的,除了你为“所有操作”修改的 API。我可以为特定操作(get blah,或 post blahblah)做它,但对于所有操作......我还不知道.

          【讨论】:

            猜你喜欢
            • 2023-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多