azure 在Terraform中使用DTAP TFvars模块

fafcakar  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(117)

我的目标是通过模块和TFvars在Terraform中实现日志分析工作区,以便将其用于其他环境(DTAP)。我正在尝试这个测试,但我得到一个错误:

文件夹结构

env-tfvars\mg-sandbox-d.tfvars

tfvars_resource_group_name  = "rg-ter-mg-sandbox"
tfvars_storage_account_name = "censored"
tfvars_container_name       = "tfstate"
tfvars_key                  = "censored.terraform.tfstate"

location = "westeurope"

log-workspace-application-name = "test-law"

modules\log-analytic-workspace\main.tf

resource "azurerm_log_analytics_workspace" "log-workspace-application" {
  name                = var.log-workspace-application-name
  location            = var.location
  resource_group_name = var.tfvars_resource_group_name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

modules\log-analytic-workspace\variables.tf

variable "log-workspace-application-name" {
  type = string
}

variable "location" {
  type = string
}

variable "tfvars_resource_group_name" {
  type = string
}

$\main.tf

resource "azurerm_resource_group" "rg-test" {
  name     = var.tfvars_resource_group_name
  location = var.location
}

module "log-analytic-workspace" {
  source              = "./modules/log-analytic-workspace"
  name                = var.log-workspace-application-name
  location            = var.location
  resource_group_name = var.tfvars_resource_group_name
  depends_on          = [azurerm_resource_group.rg-test]
}

$\variables.tf

variable "location" {
  type = string
}

variable "tfvars_resource_group_name" {
  type = string
}
variable "tfvars_storage_account_name" {
  type = string
}
variable "tfvars_container_name" {
  type = string
}
variable "tfvars_key" {
  type = string
}

variable "log-workspace-application-name" {
  type = string
}

错误

q9rjltbz

q9rjltbz1#

我尝试在Terraform中使用DTAP TFvars模块来创建log_analytics工作空间,我能够成功地提供需求。
错误消息表明log-workspace-application模块缺少所需的参数tfvars_resource_group_namelog-workspace-application-name
要修复此错误,需要在main.tf文件中示例化模块时将这些参数传递给模块。

文件夹结构:

.
├── env-tfvars
│   └── mg-sandbox-d.tfvars
├── modules
│   └── log-analytic-workspace
│       ├── main.tf
│       └── variables.tf
├── main.tf
└── variables.tf

我的地形配置:

env-tfvars/mg-sandbox-d.tfvars:

tfvars_resource_group_name  = "demovksb"
location                    = "east us 2"
log_workspace_name          = "demovk"

modules/log-analytic-workspace/main.tf:

resource "azurerm_log_analytics_workspace" "log_workspace" {
  name                = var.log_workspace_name
  location            = var.location
  resource_group_name = var.tfvars_resource_group_name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

modules/log-analytic-workspace/variables.tf:

variable "log_workspace_name" {
  type = string
}

variable "location" {
  type = string
}

variable "tfvars_resource_group_name" {
  type = string
}

main.tf:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg_test" {
  name     = var.tfvars_resource_group_name
  location = var.location
}

module "log-analytic-workspace" {
  source                    = "./modules/log-analytic-workspace"
  log_workspace_name        = var.log_workspace_name
  location                  = var.location
  tfvars_resource_group_name = var.tfvars_resource_group_name
  depends_on                = [azurerm_resource_group.rg_test]
}

variables.tf:

variable "location" {
  type = string
}

variable "tfvars_resource_group_name" {
  type = string
}

variable "log_workspace_name" {
  type = string
}

输出:

相关问题