如何自动部署Azure API管理?

时间:2018-12-01 10:51:43

标签: azure terraform azure-api-management

在API管理中,我创建了一个API,该API可以调用无服务器功能应用程序。现在,我想自动部署此功能。这是我在互联网上看到的可能性:

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

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

此致

7 个答案:

答案 0 :(得分: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。但我不会那样走。

答案 1 :(得分:1)

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

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

答案 2 :(得分: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

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

答案 3 :(得分:0)

ARM是必经之路。

您可以将其与:

答案 4 :(得分:0)

我相信自动执行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

答案 5 :(得分:0)

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 生成),您可能会不小心删除您正在从中导入的资源。耶...< /p>

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

答案 6 :(得分:0)

使用 test-api(如果你为 API Mgmt 做 microsoft 演示,你应该认识它),这里有一个 terraform 的片段,它确实有效。不包括资源组(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
}