Deploying API Management (APIM) with Terraform

In addition to our Logic App, deploying an Azure API Management (APIM) service helps manage APIs securely, monitor traffic, and enforce policies. Let's dive into the specifics of setting up APIM with Terraform.

Step-by-Step Guide for APIM

1. Set up the Azure API Management (APIM) Service

To start, we'll create an APIM instance using the following Terraform configuration:

hcl

resource "azurerm_api_management" "apim" {
  name                = "example-apim"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  publisher_name      = "Company Name"
  publisher_email     = "[email protected]"
  sku_name            = "Developer_1"
}
        

2. Define an API

We'll define an API and import the OpenAPI Specification to manage the API effectively:

hcl

resource "azurerm_api_management_api_version_set" "car_api_version" {
  name                  = "car-api-v1"
  resource_group_name   = azurerm_api_management.apim.resource_group_name
  api_management_name   = azurerm_api_management.apim.name
  display_name          = "Car API"
  versioning_scheme     = "Segment"
}

resource "azurerm_api_management_api" "car_api" {
  name                = "car-api"
  resource_group_name = azurerm_api_management.apim.resource_group_name
  api_management_name = azurerm_api_management.apim.name
  revision            = "1"
  display_name        = "Car API"
  path                = "cars"
  protocols           = ["https"]
  version             = "v1"
  version_set_id      = azurerm_api_management_api_version_set.car_api_version.id

  import {
    content_format = "openapi+json"
    content_value  = file("${path.module}/../tf-deploy/apim-deployment-artifacts/api-definitions/car-api-definition.openapi+json.json")
  }
}
        

3. Set API Policies

To manage the API traffic and enforce policies, we'll configure API policies:

hcl

resource "azurerm_api_management_api_policy" "Cars_policy" {
  api_name            = azurerm_api_management_api.car_api.name
  resource_group_name = azurerm_api_management.apim.resource_group_name
  api_management_name = azurerm_api_management.apim.name
  xml_content         = file("${path.module}/../tf-deploy/apim-deployment-artifacts/api-policies/car-api-policy.xml")
}
        

4. Create Products and Associate APIs

Next, we'll define a product and associate it with the API:

hcl

resource "azurerm_api_management_product" "product" {
  name                = "example-product"
  resource_group_name = azurerm_api_management.apim.resource_group_name
  api_management_name = azurerm_api_management.apim.name
  display_name        = "Example Product"
  description         = "This is an example product"
  terms               = "Terms of use..."
  subscription_required = true
  approval_required     = false
  subscriptions_limit   = 1
}

resource "azurerm_api_management_api_product" "api_product" {
  api_name            = azurerm_api_management_api.car_api.name
  product_name        = azurerm_api_management_product.product.name
  resource_group_name = azurerm_api_management.apim.resource_group_name
  api_management_name = azurerm_api_management.apim.name
}
        

Conclusion

With these configurations, you've successfully deployed an Azure API Management (APIM) service along with a Logic App using Terraform. This setup provides a robust infrastructure to manage APIs and automate workflows. Terraform's infrastructure-as-code approach ensures that your deployments are consistent and easy to manage.

Feel free to extend this setup to meet your specific needs, and happy deploying!

要查看或添加评论,请登录

Amit Halder - AzureCloud的更多文章

社区洞察

其他会员也浏览了