amazon-web-services - 如何实例化渲染模板文件的内容?
问题描述
我有一个用于列表的模板文件:
variable "users" {
type = "list"
default = [
"blackwidow",
"hulk",
"marvel",
]
}
// This will loop through the users list above and render out code for
// each item in the list.
data "template_file" "init" {
template = file("user_template.tpl")
count = length(var.users)
vars = {
username = var.users[count.index]
bucketid = aws_s3_bucket.myFTP_Bucket.id
}
}
模板文件有多个 aws 资源,例如
- “aws_transfer_user”
- “aws_s3_bucket_object”
- “aws_transfer_ssh_key”
等......事实上,它可以有更多的东西。它也有一些地形变量。
这个数据模板非常适合渲染模板文件的内容,替换为我的用户名。
但这就是 terraform 所做的一切。
Terraform 不会实例化模板文件的渲染内容。它只是将其保存为字符串并将其保存在内存中。有点像 C 预处理器进行替换,但不“包含”文件。有点令人沮丧。我希望 Terraform 实例化我渲染的模板文件的内容。我该怎么做呢?
解决方案
template_file
数据源(以及templatefile
为 Terraform 0.12 替换它的函数)用于字符串模板,而不是用于模块化 Terraform 配置。
要为集合中的每个项目生成一组不同的资源实例,我们使用resourcefor_each
:
variable "users" {
type = set(string)
default = [
"blackwidow",
"hulk",
"marvel",
]
}
resource "aws_transfer_user" "example" {
for_each = var.users
# ...
}
resource "aws_transfer_user" "example" {
for_each = var.users
# ...
}
resource "aws_s3_bucket_object" "example" {
for_each = var.users
# ...
}
resource "aws_transfer_ssh_key" "example" {
for_each = aws_transfer_user.example
# ...
}
Inside each of those resource blocks you can use each.key
to refer to each one of the usernames. Inside the resource "aws_transfer_ssh_key" "example"
block, because I used aws_transfer_user.example
as the repetition expression, you can also use each.value
to access the attributes of the corresponding aws_transfer_user
object. That for_each
expression also serves to tell Terraform that aws_transfer_ssh_key.example
depends on aws_transfer_user.example
.
推荐阅读
- python - 部署到 AWS 时,gunicorn 上的 CRITICAL Worker TIMEOUT
- objective-c - 如何在 NSNotification 中心嵌入一个 Objective-C 枚举?
- spring - org.jboss.as.ejb3.component.EJBComponentUnavailableException: WFLYEJB0467: 请求被拒绝,因为容器被挂起
- c - 互斥体应该存储在 main 中还是对象中?
- css - 在 yaml 标头中指定的自定义背景图像
- c# - 限制 Observable GroupBy / Merge 组合的并发性
- c# - 模拟多个接口实例:最后一个模拟的设置会影响其他模拟
- c# - Response.AddHeader 附件文件和 AntiForgeryToken()
- linux - devshell环境和bitbake任务环境的区别?
- tfs - TFS 2018 - 无法在版本中使用 Environment.Name