Terraform - Simplify Azure API Management Deployment
API management is becoming increasingly important, where seamless integration and efficient management of APIs can have a significant impact on the success of our projects. Azure API Management offers us a powerful solution that allows us to publish, manage, protect and analyze our APIs in a secure and scalable environment.
Azure provides us with a flexible and powerful platform to implement API Management resources, allowing us to take advantage of the capabilities of the cloud to improve the lifecycle of our APIs. The integration of these services into the Azure ecosystem allows us to have advanced functions such as security, monitoring and developer participation through developer portals.
Terraform, a widely adopted infrastructure-as-code tool, simplifies the deployment and management of our resources in Azure, including API management. By defining our infrastructure as code, Terraform allows us consistent and reproducible deployments, making it easier to manage complex cloud environments. Terraform automation not only streamlines the provisioning process, but also helps us maintain the health of our cloud resources, ensuring our deployments are as planned.
In this post, I’ll show you how to deploy Azure API Management resources using Terraform. This post can be useful as a starting point and will guide you through the necessary steps to get your API Management instance up and running. If you are just getting started with Azure API Management or looking to automate the deployment process, here you will find information that may be useful to you. Let’s start!
Prerequisites #
- You need Terraform CLI on your local machine, if you’re new to using Terraform to deploy Microsoft Azure resources, then I recommend you check out this link.
- A text editor or IDE of your choice (Visual Studio Code with terraform extension is my recommendation)
Declare Azure Provider in Terraform #
The provider.tf file in Terraform is used to specify and configure the providers used in your Terraform configuration. A provider is a service or platform where the resources will be managed. This could be a cloud provider like Microsoft Azure, AWS, Google Cloud, etc.
This file is important because it tells Terraform which provider’s API to use when creating, updating, and deleting resources. Without it, Terraform wouldn’t know where to manage your resources.
provider "azurerm" {
features {}
}
Deploy Azure Resources Using Terraform #
When deploying Azure API Management resources, the main.tf file is essential to orchestrate the entire deployment process. Specifies the settings for the Azure API Management instances that need to be configured, detailing every aspect of these services, from basic settings like names and locations to more complex settings like security settings and network integrations.
- azurerm_api_management: This block is responsible for defining and configuring each API Management instance according to the specifications provided in the
api_managements
variable. Iterates over each API management configuration object and creates a corresponding Azure API Management resource with the specified properties.
resource "azurerm_api_management" "apim" {
for_each = { for api in var.api_managements : api.name => api }
name = each.value.name
location = each.value.location
resource_group_name = each.value.resource_group_name
publisher_name = each.value.publisher_name
publisher_email = each.value.publisher_email
sku_name = each.value.sku_name
client_certificate_enabled = each.value.client_certificate_enabled
virtual_network_type = each.value.virtual_network_type
min_api_version = each.value.min_api_version
public_network_access_enabled = each.value.public_network_access_enabled
tags = var.tags
}
This approach exemplifies the power of infrastructure as code, showcasing how Terraform can be utilized to automate the provisioning of cloud resources like Azure API Management.
Declaration of input variables #
The variables.tf file in Terraform defines the variables I will use in the main.tf file. These variables allow for more flexibility and reusability in the code.
In this example, the variables are defined in the variables.tf include:
- api_managements: This variable defines a list of API Management configuration objects. Each object includes essential information such as the name, resource group name, location, publisher details, SKU name, and various settings related to the API Management instance. This structured approach enables the deployment of multiple API Management instances with distinct configurations, enhancing the modularity and scalability of your infrastructure setup.
- tags: This block declares a variable named tags, which is a map of strings. It is used to assign tags to the Azure resources being created. For example, you can use a key-value pair such as Terraform = true to indicate that the resource was deployed with Terraform.
variable "api_managements" {
description = "List of API Management objects including the resource group name and location for each."
type = list(object({
name = string
resource_group_name = string
location = string
publisher_name = string
publisher_email = string
sku_name = string
client_certificate_enabled = bool
virtual_network_type = string
min_api_version = string
public_network_access_enabled = bool
}))
validation {
condition = alltrue([
for api in var.api_managements : (
length(api.name) > 0 &&
length(api.resource_group_name) > 0 &&
length(api.location) > 0 &&
length(api.publisher_name) > 0 &&
length(api.publisher_email) > 0 &&
contains(["Developer_1", "Basic_1", "Consumption_0", "Premium_1"], api.sku_name) &&
contains(["None", "External", "Internal"], api.virtual_network_type)
)
])
error_message = "Invalid API Management configurations. Please check the values."
}
}
variable "tags" {
description = "Common tags for all resources"
type = object({
Environment = string
Terraform = string
})
default = {
Environment = "www.jorgebernhardt.com"
Terraform = "true"
}
}
By defining these variables, a template is created that we can customize and reuse for different implementations, allowing us to streamline the process of configuring API Management instances in Azure. This approach not only saves us time but also ensures consistency between deployments, making it easier for us to manage Azure resources.
Declaration of output values #
The output.tf file in Terraform extracts and displays information about the resources created or managed by your Terraform configuration. These outputs are defined using the output
keyword and can be used to return information that might be useful for the user, for other Terraform configurations, or for programmatically using the information in scripts or other tools.
In this example, the output.tf file returns information about the API Management instances that were created.
Once Terraform has finished applying your configuration, it will display the defined outputs.
output "api_management_details" {
description = "Details of the created API Management resources"
value = {
ids = [for api in azurerm_api_management.apim : api.id]
gateway_urls = [for api in azurerm_api_management.apim : api.gateway_url]
developer_portal_urls = [for api in azurerm_api_management.apim : api.developer_portal_url]
}
}
This code defines an output value called api_management_details
, which is a map that includes API Management instance IDs, gateway URLs, and developer portal URLs. For each API Management instance created by Terraform, this output value will provide us with an easy way to access IDs, gateway URLs, and developer portal URLs, which is especially useful for integration with other services or for other purposes, such as documentation and simply for quick reference.
Executing the Terraform Deployment #
Now that you’ve declared the resources correctly, it’s time to take the following steps to deploy them in your Azure environment.
-
Initialization: To begin, execute the terraform init command. This will initialize your working directory that holds the .tf files and download the provider specified in the provider.tf file, and configure the Terraform backend. If you want to know how, check this link.
-
Planning: Next, execute the terraform plan. This command creates an execution plan and shows Terraform’s actions to achieve the desired state defined in your .tf files. This gives you a chance to review the changes before applying them.
-
Apply: When you’re satisfied with the plan, execute the terraform apply command. This will implement the required modifications to attain the intended infrastructure state. Before making any changes, you will be asked to confirm your decision.
-
Inspection: After applying the changes, you can use terraform show command to see the current state of your infrastructure.
-
Destroy (optional): when a project is no longer needed or when resources have become outdated. You can use the terraform destroy command. This will remove all the resources that Terraform has created.
References and useful links #
Thank you for taking the time to read my post. I sincerely hope that you find it helpful.