【发布时间】:2022-05-12 14:36:51
【问题描述】:
预期当从根目录运行terragrunt run-all apply 时,将在子目录中创建一个provider.tf 文件。我已经验证我的后端能够与我的 azure 存储帐户通信,并且它会在那里创建一个 terraform.tfstate 文件。我希望provider.tf 文件出现在每个“service#”文件夹下。我对terragrunt非常陌生。这是我第一次练习它。我实际上并没有尝试部署任何 terraform 资源。只需在我的子目录中创建 provider.tf 文件。 TF版本是1.1.5。 Terragrunt 版本是 0.36.1。
我的文件夹结构
tfpractice
├───terragrunt.hcl
├───environment_vars.yaml
├───dev
│ ├───service1
│ ├───terragrunt.hcl
│ └───service2
│ ├───terragrunt.hcl
└───prod
├───service1
│ ├───terragrunt.hcl
└───service2
│ ├───terragrunt.hcl
root terragrunt.hcl 配置
# Generate provider configuration for all child directories
generate "provider" {
path = "provider.tf"
if_exists = "overwrite"
contents = <<EOF
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.94.0"
}
}
backend "azurerm" {}
}
provider "azurerm" {
features {}
}
EOF
}
# Remote backend settings for all child directories
remote_state {
backend = "azurerm"
config = {
resource_group_name = local.env_vars.resource_group_name
storage_account_name = local.env_vars.storage_account_name
container_name = local.env_vars.container_name
key = "${path_relative_to_include()}/terraform.tfstate"
}
}
# Collect values from environment_vars.yaml and set as local variables
locals {
env_vars = yamldecode(file("environment_vars.yaml"))
}
environment_vars.yaml配置
resource_group_name: "my-tf-test"
storage_account_name: "mystorage"
container_name: "tfstate"
terragrunt.hcl 服务#文件夹中的配置
# Collect values from parent environment_vars.yaml file and set as local variables
locals {
env_vars = yamldecode(file(find_in_parent_folders("environment_vars.yaml")))
}
# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}
当我运行 terragrunt run-all 时,这是输出
Are you sure you want to run 'terragrunt apply' in each folder of the stack described above? (y/n) y
Initializing the backend...
Initializing the backend...
Initializing the backend...
Initializing the backend...
Initializing provider plugins...
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.
Initializing provider plugins...
Initializing provider plugins...
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.
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.
Initializing provider plugins...
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.
No changes. Your infrastructure matches the configuration.
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
看起来很成功,但是没有 provider.tf 文件出现在任何目录中。甚至没有根。它只是在 service# 目录下创建一个 terraform.tfstate 文件。
但是...如果我从根目录运行terragrunt init,它将按预期在根目录中创建provider.tf 文件。尽管 terragrunt init 成功,但这在 service# 目录中不起作用。
我错过了什么?这是最基本的 terragrunt 用例,但这些示例让我相信这应该可以正常工作。
【问题讨论】:
-
您是否尝试过从
dev或prod目录运行它? -
这行得通,但不是真的。问题出在 terragrunt.hcl 配置中。如果我使用与我当前在 service# 目录中的配置相同的配置,它在 dev 或 prod 级别上可以完美运行。但是,如果我尝试运行 terragrunt run-all apply,则 terragrunt 抱怨只有一个级别的包含。它显然不喜欢嵌套包含,这意味着在 dev 以及 dev 的 service# 目录中。因此,虽然我可以将 .hcl 放在 dev 和 prod 文件夹中并按照我的预期创建 provider.tf,但它不会进入 service# 文件夹。
-
我认为您需要在子
terragrunt.hcl文件中使用此块:include "root" { path = find_in_parent_folders() }。这可能不是您拥有的其他include。你可以试试吗? -
不。它再次抱怨不止一个级别的包含,所以我从 dev 和 prod 目录中删除了包含。没有在任何地方创建 provider.tf 文件,但它确实在每个服务中创建了一个 terraform.tfstate ......由于指定了后端,这也不应该发生。这没有多大意义
-
我让它工作了,但是 terragrunt run-all apply 命令根本不起作用。相反,我必须在根目录运行 terragrunt apply。如果您不在根目录,则所有子文件夹都会组合在一起,而不是放在 dev/prod 子文件夹中。然后我必须去每个子文件夹并再次运行它。这是我让它工作的唯一方法。
标签: terraform terraform-provider-azure terragrunt