用Terraform部署Azure Event Hubs报以下错:
Error: creating Namespace: (Name "mcpredimacn2cehubns" / Resource Group "rg_test20210707"): performing CreateOrUpdate: namespaces.NamespacesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="NoRegisteredProviderFound" Message="No registered resource provider found for location 'chinanorth2' and API version '2021-01-01-preview' for type 'namespaces'. The supported api-versions are '2014-09-01, 2015-08-01, 2017-04-01, 2018-01-01-preview'. The supported locations are 'chinanorth, chinaeast, chinanorth2, chinaeast2'."
主要原因是未指定hashicorp/azurerm的Version,默认用了最新版(07/08/2021时,是2.66.0)。
用以下方式指定老版本,就可以了:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.0.0"
}
}
}
provider "azurerm" {
features {}
}
Azure上运行Terraform的方式,可以参考以下文档:
https://docs.azure.cn/zh-cn/governance/policy/assign-policy-terraform
https://docs.microsoft.com/en-us/azure/developer/terraform/get-started-powershell
有关此文的完整Terraform脚本是:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.0.0"
}
}
}
provider "azurerm" {
features {}
}
variable "DEPLOYMENT" {
type = map(string)
default = {
env = "mc"
MC_region = "cn2"
}
}
# Create a resource group
resource "azurerm_resource_group" "rg-test" {
name = "rg_test20210707"
location = "chinanorth2"
}
resource "azurerm_eventhub_namespace" "testeventhubns" {
name = "${var.DEPLOYMENT.env}test${var.DEPLOYMENT.MC_region}cehubns"
location = azurerm_resource_group.rg-test.location
resource_group_name = azurerm_resource_group.rg-test.name
sku = "Standard"
capacity = 1
tags = "${merge(tomap({
"comp" = "enenthubsns",
"resource" = "testeventhubns",
"desc" = "azurerm_eventhub_namespace for ${var.DEPLOYMENT.MC_region} ${var.DEPLOYMENT.env} instance"}),
var.DEPLOYMENT)}"
}
resource "azurerm_eventhub" "test" {
name = "${var.DEPLOYMENT.env}test${var.DEPLOYMENT.MC_region}cehub"
namespace_name = azurerm_eventhub_namespace.testeventhubns.name
resource_group_name = azurerm_resource_group.rg-test.name
partition_count = 2
message_retention = 1
}