|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create an Azure API Management service using Terraform' |
| 3 | +description: 'In this article, you create an Azure API Management service using Terraform.' |
| 4 | +ms.topic: quickstart |
| 5 | +ms.service: api-management |
| 6 | +ms.date: 3/13/2023 |
| 7 | +ms.custom: devx-track-terraform |
| 8 | +author: TomArcherMsft |
| 9 | +ms.author: tarcher |
| 10 | +--- |
| 11 | + |
| 12 | +# Quickstart: Create an Azure API Management service using Terraform |
| 13 | + |
| 14 | +This article shows how to use [Terraform](/azure/terraform) to create an [API Management service instance](/azure/api-management/api-management-key-concepts) on Azure. API Management helps organizations publish APIs to external, partner, and internal developers to unlock the potential of their data and services. API Management provides the core competencies to ensure a successful API program through developer engagement, business insights, analytics, security, and protection. API Management enables you to create and manage modern API gateways for existing backend services hosted anywhere. For more information, see the [Azure API Management - Overview and key concepts](api-management-key-concepts.md). |
| 15 | + |
| 16 | +[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 17 | + |
| 18 | +In this article, you learn how to: |
| 19 | + |
| 20 | +> [!div class="checklist"] |
| 21 | +> * Create a random pet name for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
| 22 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 23 | +> * Create a random string for the Azure API Management service name using [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) |
| 24 | +> * Create an Azure API Management service using [azurerm_api_management](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management) |
| 25 | +
|
| 26 | +[!INCLUDE [AI attribution](../../includes/ai-generated-attribution.md)] |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 31 | + |
| 32 | +## Implement the Terraform code |
| 33 | + |
| 34 | +> [!NOTE] |
| 35 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-api-management-create). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-api-management-create/TestRecord.md). |
| 36 | +> |
| 37 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 38 | +
|
| 39 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 40 | + |
| 41 | +1. Create a file named `main.tf` and insert the following code: |
| 42 | + |
| 43 | + [!code-terraform[master](~/terraform_samples/quickstart/101-azure-api-management-create/main.tf)] |
| 44 | + |
| 45 | +1. Create a file named `outputs.tf` and insert the following code: |
| 46 | + |
| 47 | + [!code-terraform[master](~/terraform_samples/quickstart/101-azure-api-management-create/outputs.tf)] |
| 48 | + |
| 49 | +1. Create a file named `providers.tf` and insert the following code: |
| 50 | + |
| 51 | + [!code-terraform[master](~/terraform_samples/quickstart/101-azure-api-management-create/providers.tf)] |
| 52 | + |
| 53 | +1. Create a file named `variables.tf` and insert the following code: |
| 54 | + |
| 55 | + [!code-terraform[master](~/terraform_samples/quickstart/101-azure-api-management-create/variables.tf)] |
| 56 | + |
| 57 | +## Initialize Terraform |
| 58 | + |
| 59 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 60 | + |
| 61 | +## Create a Terraform execution plan |
| 62 | + |
| 63 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 64 | + |
| 65 | +## Apply a Terraform execution plan |
| 66 | + |
| 67 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 68 | + |
| 69 | +## Verify the results |
| 70 | + |
| 71 | +#### [Azure CLI](#tab/azure-cli) |
| 72 | + |
| 73 | +1. Get the Azure resource group name. |
| 74 | + |
| 75 | + ```console |
| 76 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Get the service name. |
| 80 | + |
| 81 | + ```console |
| 82 | + api_management_service_name=$(terraform output -raw api_management_service_name) |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Run [az apim show](/cli/azure/apim#az-apim-show) to display information about the new service. |
| 86 | + |
| 87 | + ```azurecli |
| 88 | + az apim show --resource-group $resource_group_name \ |
| 89 | + --name $api_management_service_name |
| 90 | + ``` |
| 91 | + |
| 92 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 93 | + |
| 94 | +1. Get the Azure resource group name. |
| 95 | + |
| 96 | + ```console |
| 97 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 98 | + ``` |
| 99 | + |
| 100 | +1. Get the service name. |
| 101 | + |
| 102 | + ```console |
| 103 | + $api_management_service_name=$(terraform output -raw api_management_service_name) |
| 104 | + ``` |
| 105 | + |
| 106 | +1. Run [Get-AzApiManagement](/powershell/module/az.apimanagement/get-azapimanagement) to display information about the new service. |
| 107 | + |
| 108 | + ```azurepowershell |
| 109 | + Get-AzApiManagement -ResourceGroupName $resource_group_name ` |
| 110 | + -Name $api_management_service_name |
| 111 | + ``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Clean up resources |
| 116 | + |
| 117 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 118 | + |
| 119 | +## Troubleshoot Terraform on Azure |
| 120 | + |
| 121 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 122 | + |
| 123 | +## Next steps |
| 124 | + |
| 125 | +> [!div class="nextstepaction"] |
| 126 | +> [Tutorial: Import and publish your first API](import-and-publish.md) |
0 commit comments