terraform - Generate file with dynamic content with Terragrunt
问题描述
I'm really new to Terragrunt.
I was wondering if there is a way to dynamically generate the content of a file?
For example, consider the following piece of code:
generate "provider" {
path = "provider.tf"
if_exists = "overwrite"
contents = <<EOF
terraform {
required_providers {
azurerm = {
source = "azurerm"
version = "=2.49.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "xxxxxxxxxxxxxxxxx"
}
EOF
}
Is there a way to set values such as subscription_id
dynamically? I've tried using something like ${local.providers.subscription_id}
but it doesn't work:
provider "azurerm" {
features {}
subscription_id = "${local.providers.subscription_id}"
}
解决方案
只要您在同一范围内定义本地,您所拥有的就应该完全按原样工作。刚刚使用 Terragrunt v0.28.24 测试了以下内容。
在common.hcl
,位于某个父目录中的文件(但仍在同一个 Git 存储库中):
locals {
providers = {
subscription_id = "foo"
}
}
在你的terragrunt.hcl
:
locals {
common_vars = read_terragrunt_config(find_in_parent_folders("common.hcl"))
}
generate "provider" {
path = "provider.tf"
if_exists = "overwrite"
contents = <<EOF
terraform {
required_providers {
azurerm = {
source = "azurerm"
version = "=2.49.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "${local.common_vars.locals.providers.subscription_id}"
}
EOF
}
运行后terragrunt init
,将provider.tf
生成预期内容:
provider "azurerm" {
features {}
subscription_id = "foo"
}
推荐阅读
- reactjs - 如何自动更新 React 组件?
- php - 有时无法从 codeigniter 中的数据库获取响应
- python - 如何将目录中的图像转换为np数组
- javascript - 如何从内部函数访问 Firebase 托管项目目标 ID
- ios - 更改 NSManagedObject 属性的值不会反映在 DiffableDataSource
- freertos - FreeRTOS - 为最后一次函数调用恢复堆栈指针
- java - 用于组合单独子阵列中的单个元素的算法
- graphql - GraphQL 中的动态类型
- android - 带有动态图像 URL 的 Android 谷歌地图集群项目标记 - 用所有标记覆盖 URL
- multithreading - Rust 的通道如何实现?