Terraform: "Error: Reference to undeclared resource when calling modules from terragrunt" in Azure
Terraform: "Error: Reference to undeclared resource when calling modules from terragrunt" in Azure
我是第一次尝试使用 terrag运行t。我遵循了 https://terratest.gruntwork.io/docs/getting-started/quick-start/ 中提到的目录结构。我想重置我在环境文件夹中使用的重复 main.tf、outputs.tf 和 vars.tf 的 gid。以下是我面临的版本和错误。任何帮助将不胜感激。提前致谢。
Terrag运行t 版本
terragrunt version v0.23.10
Terraform 版本
Terraform v0.12.24
目录结构
terraform-live/
├── prod
│ └── resource_group
│ ├── main.tf
│ └── terragrunt.hcl
└── terragrunt.hcl
terraform-live/terragrunt.hcl 的内容
backend = "azurerm"
config = {
key = "${path_relative_to_include()}/terraform.tfstate"
resource_group_name = "common-rg"
storage_account_name = "testsa01"
container_name = "tfstate"
}
}
terraform-live/prod/resource_group/main.tf
的内容
backend "azurerm" {}
}
terraform-live/prod/resource_group/terragrunt.hcl 的内容
terraform {
source = "git::git@github.com:adi4dpeople/terraform_modules.git//resource_group?ref=v0.0.1"
}
# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}
# These are the variables we have to pass in to use the module specified in the terragrunt configuration above
inputs = {
location = "westus"
rg_name = "testrg01"
}
当我 运行 terrag运行t 计划时,我收到以下错误:
[terragrunt] 2020/04/24 22:24:39 Reading Terragrunt config file at /home/aditya/terraform-live/prod/resource_group/terragrunt.hcl
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:39 Running command: terraform --version
[terragrunt] 2020/04/24 22:24:44 Terraform files in /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group are up to date. Will not download again.
[terragrunt] 2020/04/24 22:24:44 Copying files from /home/aditya/terraform-live/prod/resource_group into /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group
[terragrunt] 2020/04/24 22:24:44 Setting working directory to /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:44 Backend azurerm has not changed.
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:44 Running command: terraform init -backend-config=access_key=xxxxxxxxxxxx -backend-config=container_name=tfstate -backend-config=key=prod/resource_group/terraform.tfstate -backend-config=resource_group_name=testrg01 -backend-config=storage_account_name=testsa01
Initializing the backend...
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
[terragrunt] 2020/04/24 22:24:52 Running command: terraform plan
Acquiring state lock. This may take a few moments...
Error: Reference to undeclared resource
on outputs.tf line 2, in output "id":
2: value = azurerm_resource_group.rg.id
A managed resource "azurerm_resource_group" "rg" has not been declared in the
root module.
Error: Reference to undeclared resource
on outputs.tf line 6, in output "name":
6: value = azurerm_resource_group.rg.name
A managed resource "azurerm_resource_group" "rg" has not been declared in the
root module.
Releasing state lock. This may take a few moments...
[terragrunt] 2020/04/24 22:25:01 Hit multiple errors:
exit status 1
aditya@LAPTOP-6C2MPJDV:~/terraform-live/prod/resource_group$
我已经在 terragrunt 上解决了这个 GitHub 问题
https://github.com/gruntwork-io/terragrunt/issues/1151
我在尝试创建将拉取角色分配给 Azure Kubernetes 集群 以从 Azure 容器注册表[=52= 拉取图像时遇到了类似的问题] 使用 托管系统身份
Azure 角色分配(outputs.tf
文件)
output "acr_id" {
value = azure_container_registry.acr.id
}
这被放在一个名为 azure-role-assignment
的模块目录中
但是,当我在测试环境中调用模块输出文件时(main.tf
文件):
# Create azure container registry
module "azure_container_registry" {
source = "../modules/azure-container-registry"
container_registry_name = var.container_registry_name
resource_group_name = var.resource_group_name
location = var.location
sku = var.sku
admin_enabled = var.admin_enabled
}
# Create azure role assignment
module "azure_role_assignment" {
source = "../modules/azure-role-assignment"
scope = module.azure_container_registry.acr_id
role_definition_name = var.role_definition_name
principal_id = module.azure_kubernetes_cluster.principal_id
}
然而,当我 运行 terraform 应用时,我收到错误:
Error: Reference to undeclared resource
on ../modules/azure-container-registry/outputs.tf line 2, in output "acr_id":
2: value = azure_container_registry.acr.id
A managed resource "azure_container_registry" "acr" has not been declared in
module.azure_container_registry.
我是这样解决的:
问题出在我如何在 outputs.tf
文件中定义 arc_id
的值。而不是这个:
Azure 角色分配(outputs.tf
文件)
output "acr_id" {
value = azure_container_registry.acr.id
}
应该是这样的:
Azure role assignment (`outputs.tf` file)
output "acr_id" {
value = azurerm_container_registry.acr.id
}
那是 azurerm_container_registry.acr.id
而不是 azure_container_registry.acr.id
就这些了。
希望对您有所帮助
我是第一次尝试使用 terrag运行t。我遵循了 https://terratest.gruntwork.io/docs/getting-started/quick-start/ 中提到的目录结构。我想重置我在环境文件夹中使用的重复 main.tf、outputs.tf 和 vars.tf 的 gid。以下是我面临的版本和错误。任何帮助将不胜感激。提前致谢。
Terrag运行t 版本
terragrunt version v0.23.10
Terraform 版本
Terraform v0.12.24
目录结构
terraform-live/
├── prod
│ └── resource_group
│ ├── main.tf
│ └── terragrunt.hcl
└── terragrunt.hcl
terraform-live/terragrunt.hcl 的内容
backend = "azurerm"
config = {
key = "${path_relative_to_include()}/terraform.tfstate"
resource_group_name = "common-rg"
storage_account_name = "testsa01"
container_name = "tfstate"
}
}
terraform-live/prod/resource_group/main.tf
的内容backend "azurerm" {}
}
terraform-live/prod/resource_group/terragrunt.hcl 的内容
terraform {
source = "git::git@github.com:adi4dpeople/terraform_modules.git//resource_group?ref=v0.0.1"
}
# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}
# These are the variables we have to pass in to use the module specified in the terragrunt configuration above
inputs = {
location = "westus"
rg_name = "testrg01"
}
当我 运行 terrag运行t 计划时,我收到以下错误:
[terragrunt] 2020/04/24 22:24:39 Reading Terragrunt config file at /home/aditya/terraform-live/prod/resource_group/terragrunt.hcl
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:39 Running command: terraform --version
[terragrunt] 2020/04/24 22:24:44 Terraform files in /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group are up to date. Will not download again.
[terragrunt] 2020/04/24 22:24:44 Copying files from /home/aditya/terraform-live/prod/resource_group into /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group
[terragrunt] 2020/04/24 22:24:44 Setting working directory to /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:44 Backend azurerm has not changed.
[terragrunt] [/home/aditya/terraform-live/prod/resource_group] 2020/04/24 22:24:44 Running command: terraform init -backend-config=access_key=xxxxxxxxxxxx -backend-config=container_name=tfstate -backend-config=key=prod/resource_group/terraform.tfstate -backend-config=resource_group_name=testrg01 -backend-config=storage_account_name=testsa01
Initializing the backend...
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
[terragrunt] 2020/04/24 22:24:52 Running command: terraform plan
Acquiring state lock. This may take a few moments...
Error: Reference to undeclared resource
on outputs.tf line 2, in output "id":
2: value = azurerm_resource_group.rg.id
A managed resource "azurerm_resource_group" "rg" has not been declared in the
root module.
Error: Reference to undeclared resource
on outputs.tf line 6, in output "name":
6: value = azurerm_resource_group.rg.name
A managed resource "azurerm_resource_group" "rg" has not been declared in the
root module.
Releasing state lock. This may take a few moments...
[terragrunt] 2020/04/24 22:25:01 Hit multiple errors:
exit status 1
aditya@LAPTOP-6C2MPJDV:~/terraform-live/prod/resource_group$
我已经在 terragrunt 上解决了这个 GitHub 问题 https://github.com/gruntwork-io/terragrunt/issues/1151
我在尝试创建将拉取角色分配给 Azure Kubernetes 集群 以从 Azure 容器注册表[=52= 拉取图像时遇到了类似的问题] 使用 托管系统身份
Azure 角色分配(outputs.tf
文件)
output "acr_id" {
value = azure_container_registry.acr.id
}
这被放在一个名为 azure-role-assignment
的模块目录中但是,当我在测试环境中调用模块输出文件时(main.tf
文件):
# Create azure container registry
module "azure_container_registry" {
source = "../modules/azure-container-registry"
container_registry_name = var.container_registry_name
resource_group_name = var.resource_group_name
location = var.location
sku = var.sku
admin_enabled = var.admin_enabled
}
# Create azure role assignment
module "azure_role_assignment" {
source = "../modules/azure-role-assignment"
scope = module.azure_container_registry.acr_id
role_definition_name = var.role_definition_name
principal_id = module.azure_kubernetes_cluster.principal_id
}
然而,当我 运行 terraform 应用时,我收到错误:
Error: Reference to undeclared resource
on ../modules/azure-container-registry/outputs.tf line 2, in output "acr_id":
2: value = azure_container_registry.acr.id
A managed resource "azure_container_registry" "acr" has not been declared in
module.azure_container_registry.
我是这样解决的:
问题出在我如何在 outputs.tf
文件中定义 arc_id
的值。而不是这个:
Azure 角色分配(outputs.tf
文件)
output "acr_id" {
value = azure_container_registry.acr.id
}
应该是这样的:
Azure role assignment (`outputs.tf` file)
output "acr_id" {
value = azurerm_container_registry.acr.id
}
那是 azurerm_container_registry.acr.id
而不是 azure_container_registry.acr.id
就这些了。
希望对您有所帮助