问题
I create my API Management Instance and import the Swagger API with Terraform like this:
#Create the API Management layer
resource "azurerm_api_management" "apim" {
name = "${var.prefix}-apim"
resource_group_name = var.resource_group_name
location = var.resource_group_location
sku {
name = "Developer"
capacity = 1
}
}
resource "azurerm_api_management_api" "swagger" {
name = "ensurex-transaction-api"
resource_group_name = var.resource_group_name
api_management_name = azurerm_api_management.apim.name
revision = "1"
display_name = "My API"
path = "api"
protocols = ["https"]
import {
content_format = "swagger-json"
#TODO: Put this in a better place during build/tests
content_value = file("../../web/out/test/swagger.json")
}
}
However, when I open the developer page there is an api called "Echo API" and products called "Starter" and "Unlimited".
Is it possible to prevent Terraform from creating these in the first place?
Or is it possible to add something to the Terraform script to delete them after they have been created?
My next step after terraform is some configuration of the resources with ansible so I am OK with a solution that does it there.
However, I don't want to use Powershell or replace terraform with an ARM template.
回答1:
It doesn't appear to be possible to prevent terraform from creating these in the first place as they are created by the underlying SDK that terraform uses.
It is not possible to directly use the Azure CLI as it doesn't yet support API Management.
However, the REST API does support it.
- Delete an API
- Delete a product
There is a module in the Azure CLI that lets you call the REST API in cross platform way.
e.g.
az rest -m delete -u "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/my-resource-group-name/providers/Microsoft.ApiManagement/service/my-apim-name/apis/echo-api?api-version=2019-01-01"
This has advantages over solutions like curl as it is handling the authentication for you.
Another key point is that the {subscriptionId}
is automatically substituted for you with the correct value (assuming you are logged in with the correct account) and you don't have to look up the value yourself.
These commands can then be embedded in terraform using the local-exec with a null-resource.
# Create a resource group
resource "azurerm_resource_group" "resource-group" {
name = "${var.prefix}_rg"
location = var.resource_group_location
tags = var.tags
}
resource "azurerm_api_management" "apim" {
name = "${var.prefix}-apim"
resource_group_name = azurerm_resource_group.resource-group.name
location = var.resource_group_location
sku {
name = "Developer"
capacity = 1
}
}
resource "null_resource" "clean-apim-api" {
provisioner "local-exec" {
command = "az rest -m delete -u \"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/apis/echo-api?api-version=2019-01-01\""
}
depends_on = ["azurerm_api_management.apim"]
}
resource "null_resource" "clean-apim-product-starter" {
provisioner "local-exec" {
command = "az rest -m delete -u \"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/products/Starter?api-version=2019-01-01\""
}
depends_on = ["azurerm_api_management.apim"]
}
resource "null_resource" "clean-apim-product-unlimited" {
provisioner "local-exec" {
command = "az rest -m delete -u \"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/${azurerm_resource_group.resource-group.name}/providers/Microsoft.ApiManagement/service/${azurerm_api_management.apim.name}/products/Unlimited?api-version=2019-01-01\""
}
depends_on = ["azurerm_api_management.apim"]
}
来源:https://stackoverflow.com/questions/57530592/how-can-i-remove-demo-products-from-apim-created-with-terraform