如何将变量传递给 terraform 13.5 中的 google_storage_bucket_object
How to pass variables to a google_storage_bucket_object in terraform 13.5
我有一个 google 存储桶对象,它是这样创建的(通过 terraform):
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
source = "${path.module}/my-script.sh"
bucket = my_bucket
}
但是在那个脚本里面是我想做变量的东西。
所以说我的-script.sh 看起来像:
#!/bin/bash
echo "hello ${name}"
有没有办法让我传递一个变量以便进行插值,以便上传的脚本实际上说“你好约翰”
这可能需要一些中间步骤来创建一个带有变量插值的文件,然后我可以将其作为 google_storage_bucket_object 的源传入 - 但不确定如何完成。
按照您到目前为止的编写方式,是 Google 提供程序本身读取文件 my-script.sh
,因此到那时再进行任何 Terraform 模板处理都为时已晚;模板处理是 Terraform 语言本身的一部分,发生在将配置传递给提供者之前。
但是,google_storage_bucket_object
还有一个替代参数 content
,它允许您将所需的内容直接传递给提供者,而不需要提供者本身从文件中读取它。最简单的形式允许您将常量值发送到提供者而无需先将它们写入磁盘:
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
bucket = my_bucket
content = "Hello world!"
}
但是,content
参数可以采用任何有效的 Terraform 语言表达式,其中 returns 一个字符串,其中一个示例是对 templatefile
, which is a function that asks Terraform itself (the core runtime, not the provider) to read a file from disk and interpret its content as a string template.
的调用
把这两件事放在一起,你可以这样做:
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
bucket = my_bucket
content = templatefile("${path.module}/my-script.sh", {
name = var.example_name
})
}
对于您在此处共享的简单示例,无论是 Terraform 本身还是最终从磁盘读取文件的 Google 云平台提供商,都没有太大区别,但有在其他情况下可能很重要的一些实际差异:
- Terraform 字符串始终是 Unicode 字符序列而不是原始字节,因此使用
content
传递值要求内容是 Unicode 字符串,并且提供者会在保存前将其编码为 UTF-8它。另一方面,如果您使用 source
,提供程序将完全按原样获取文件中的原始字节并上传它们,因此您可能会上传其他字符编码的文本或上传不是“根本没有文本。
- 因为它现在是渲染发送给提供商的模板所产生的字符串,而不仅仅是文件名,所以您会看到内容本身显示在 Terraform 计划输出中
content
,而 source
您以前只会看到文件名。在许多情况下,人们认为这是使用 content
的 优势 而不是劣势,但如果内容特别大(从而使计划输出很长),它可能会很麻烦) 或者它包含不适合在控制台输出中显示的内容。
- Terraform 资源配置的大小存在实际限制,这是由提供者协议、SDK 和系统可用内存等各种技术细节造成的,因此使用
content
仅适用于相对较小的对象,例如脚本和其他小型配置文件。
如果您的用例包括处理聚集在一个子目录中的大量模板文件,您可能会发现 the Terraform module hashicorp/dir/template
useful. It wraps both templatefile
and fileset
function calls to prepare a number of files for uploading all at once, returning them in a way that you can pass conveniently to a google_storage_bucket_object
resource using for_each
。
我有一个 google 存储桶对象,它是这样创建的(通过 terraform):
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
source = "${path.module}/my-script.sh"
bucket = my_bucket
}
但是在那个脚本里面是我想做变量的东西。
所以说我的-script.sh 看起来像:
#!/bin/bash
echo "hello ${name}"
有没有办法让我传递一个变量以便进行插值,以便上传的脚本实际上说“你好约翰”
这可能需要一些中间步骤来创建一个带有变量插值的文件,然后我可以将其作为 google_storage_bucket_object 的源传入 - 但不确定如何完成。
按照您到目前为止的编写方式,是 Google 提供程序本身读取文件 my-script.sh
,因此到那时再进行任何 Terraform 模板处理都为时已晚;模板处理是 Terraform 语言本身的一部分,发生在将配置传递给提供者之前。
但是,google_storage_bucket_object
还有一个替代参数 content
,它允许您将所需的内容直接传递给提供者,而不需要提供者本身从文件中读取它。最简单的形式允许您将常量值发送到提供者而无需先将它们写入磁盘:
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
bucket = my_bucket
content = "Hello world!"
}
但是,content
参数可以采用任何有效的 Terraform 语言表达式,其中 returns 一个字符串,其中一个示例是对 templatefile
, which is a function that asks Terraform itself (the core runtime, not the provider) to read a file from disk and interpret its content as a string template.
把这两件事放在一起,你可以这样做:
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
bucket = my_bucket
content = templatefile("${path.module}/my-script.sh", {
name = var.example_name
})
}
对于您在此处共享的简单示例,无论是 Terraform 本身还是最终从磁盘读取文件的 Google 云平台提供商,都没有太大区别,但有在其他情况下可能很重要的一些实际差异:
- Terraform 字符串始终是 Unicode 字符序列而不是原始字节,因此使用
content
传递值要求内容是 Unicode 字符串,并且提供者会在保存前将其编码为 UTF-8它。另一方面,如果您使用source
,提供程序将完全按原样获取文件中的原始字节并上传它们,因此您可能会上传其他字符编码的文本或上传不是“根本没有文本。 - 因为它现在是渲染发送给提供商的模板所产生的字符串,而不仅仅是文件名,所以您会看到内容本身显示在 Terraform 计划输出中
content
,而source
您以前只会看到文件名。在许多情况下,人们认为这是使用content
的 优势 而不是劣势,但如果内容特别大(从而使计划输出很长),它可能会很麻烦) 或者它包含不适合在控制台输出中显示的内容。 - Terraform 资源配置的大小存在实际限制,这是由提供者协议、SDK 和系统可用内存等各种技术细节造成的,因此使用
content
仅适用于相对较小的对象,例如脚本和其他小型配置文件。
如果您的用例包括处理聚集在一个子目录中的大量模板文件,您可能会发现 the Terraform module hashicorp/dir/template
useful. It wraps both templatefile
and fileset
function calls to prepare a number of files for uploading all at once, returning them in a way that you can pass conveniently to a google_storage_bucket_object
resource using for_each
。