skip to Main Content

I have a no. of API management services setup on Azure already but they are not provisioned using Terraform. Now I want to using terraform to update the backend HTTP endpoint and policies. Is it possible to do so? Is there any data sources for apim I can use? Any example I can refer to?

2

Answers


  1. I tried in my environment and got below results:

    Initially, I created Azure APIM management service using below terraform code.

    main.tf

    provider "azurerm" {
      features {}
       }                           
      
    
    resource "azurerm_resource_group" "example" {
      name     = "rg3"
      location = "West Europe"
    }
    
    resource "azurerm_api_management" "example" {
      name                = "venkat-demo"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      publisher_name      = "My Company"
      publisher_email     = "[email protected]"
    
      sku_name = "Developer_1"
    }
    
    resource "azurerm_api_management_api" "example" {
      name                = "venkatdemo326"
      resource_group_name = azurerm_resource_group.example.name
      api_management_name = azurerm_api_management.example.name
      revision            = "1"
      display_name        = "vjtestAPI"
      path                = "example"
      protocols           = ["https"]
    
      
    
      import {
        content_format = "swagger-link-json"
        content_value  = "http://conferenceapi.azurewebsites.net/?format=json"
      }
    }
    

    Portal:

    enter image description here

    Now I want to using terraform to update the backend HTTP endpoint and policies

    You can use the below terraform code to create resources that manage the backend HTTP endpoint and policies of that service.

    main.tf

    provider "azurerm" {
      features {}
       }   
       
    data "azurerm_api_management" "example" {
      name                = "venkat-demo"
      resource_group_name = data.azurerm_resource_group.example.name
    }
    
    resource "azurerm_api_management_backend" "example" {
      name                = "backend1"
      api_management_name = data.azurerm_api_management.example.name
      resource_group_name = data.azurerm_api_management.example.resource_group_name
      protocol            = "http"
      url                 = "https://example.com/api"
    }
    
    resource "azurerm_api_management_policy" "example" {
        api_management_id = data.azurerm_api_management.example.id
        xml_content = <<XML
    <policies>
      <inbound>
        <find-and-replace from="xyz" to="abc" />
      </inbound>
    </policies>
    XML
    }
    

    Console:

    enter image description here

    Portal:

    enter image description here

    Reference:

    Login or Signup to reply.
  2. If you want to change an Azure resource (in this case Azure APIM), you’ll have to first declare the resource with the corresponding resource type (in this case azurerm_api_management) and then to import your existing resource into your Terraform’s state.

    After importing, on the subsequent execution of terraform plan, you’ll see the changes terraform would like to execute based on the difference between your declaration and the current state of your APIM instance.

    You cannot use a terraform data source (like this) if you want to change (=manage) the resource. Data sources can only be used to read attributes from resources.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search