gpt4 book ai didi

azure - terraform 报告未为资源组定义名称参数

转载 作者:行者123 更新时间:2023-12-03 06:16:29 27 4
gpt4 key购买 nike

目标:使用变量创建 Azure 资源组并强制执行命名约束。

Terraform Azure 资源组模块定义如下。

locals.tf

locals {
tags = {
"Environment" = var.environment_name
"Department" = var.department
"Managed By" = "Terraform Open Source"
}
}

ma​​in.tf

resource "azurerm_resource_group" "resource_group" {
name = regex("^[-\\w\\._\\(\\)]+$",substr("rg-${local.environment_name}-${local.application_name}-${var.location}", 90))
location = var.location
tags = local. Tags
}output "resource_group_id" {
value = azurerm_resource_group.resource_group.id
description = "Resource Group id"
}

猫输出.tf

output "resource_group_name" {
value = azurerm_resource_group.resource_group.name
description = "The name of the resource group"
}

output "resource_group_location" {
value = azurerm_resource_group.resource_group.location
description = "The location of the resource group"
}

providers.tf

terraform {
required_version = ">= 1.4.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.55.0"
}
}
}
provider "azurerm" {
features {}
}

变量.tf

variable "name" {
type = string
description = "Name of the resource group"
}

variable "location" {
type = string
description = "Location of the resource group"
}

variable "environment_name" {
type = string
description = <<EOT
(Optional) The name of the environment where the resources will be deployed.

Options:
- dev
- uat
- test
- prod

Default: dev
EOT

default = "dev"

validation {
condition = can(regex("dev|uat|test|prod"), var.environment)
error_message = "Err: environment name is not valid."
}
}
variable "department" {
type = string
description = "Name of the department"
}
variable "cost_centre" {
type = string
description = "Cost Centre for the Resource or Resource Group"

}
variable "application_name" {
type = string
description = "Name of the application"

}

现在,我创建了一个如下所示的示例。

ma​​in.tf

module "rg" {
source = "./.."
location = "australiaeast"
environment_name = "Dev"
department = "Data Services"
cost_centre = "ABC101"
application_name = "demo"
}

provider.tf

terraform {
required_version = ">= 1.4.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.55.0"
}
}
}
provider "azurerm" {
features {}
}

不幸的是,当我运行terraform plan时,我收到如下错误。

错误:

│ Error: Missing required argument

│ on main.tf line 1, in module "rg":
│ 1: module "rg" {

│ The argument "name" is required, but no definition was found.

最佳答案

如果您查看代码,它会清楚地定义 name 变量而没有默认值:

variable "name" {
type = string
description = "Name of the resource group"
}

由于没有给出默认值,因此在实例化 rg 模块时,您必须显式提供 name。如果您不想这样做,则必须定义一个默认的名称,例如:

variable "name" {
type = string
default = "some-default-rg-name"
description = "Name of the resource group"
}

关于azure - terraform 报告未为资源组定义名称参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76214339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com