Terraform 中如何使用数据源? [英] How are data sources used in Terraform?
问题描述
Terraform 数据源文档告诉我什么是数据源,但我不太明白.有人可以给我一个数据源的用例吗?它和使用变量配置有什么区别?
The Terraform Data Sources documentation tells me what a data source is, but I do not quite understand it. Can somebody give me a use case of data source? What is the difference between it and configuring something using variables?
推荐答案
可以使用数据源的原因有很多;但他们的目标是做某事,然后给你数据.
Data sources can be used for a number of reasons; but their goal is to do something and then give you data.
让我们以他们的文档为例:
# Find the latest available AMI that is tagged with Component = web
data "aws_ami" "web" {
filter {
name = "state"
values = ["available"]
}
filter {
name = "tag:Component"
values = ["web"]
}
most_recent = true
}
这使用 aws_ami 数据源 - 这是不同的比资源!相反,它只会为您提供信息,而不是创建任何东西.此示例将特别调用 describe-images
AWS API 调用,传入一些指定的 --filter
选项,并返回一个您可以获取信息的对象来自 - 看看这些 属性!
This uses the aws_ami data source - this is different than a resource! It will instead just give you information, and not create anything. This example in particular will call out to the describe-images
AWS API call, pass in a few --filter
options as specified, and return an object that you can get information from - take a look at these attributes!
- 姓名
- owner_id
- 说明
- image_id
... 名单还在继续.如果我是这样的话,这真的很有用,比方说 - 总是想提取与某些标签匹配的最新 AMI,并保持启动配置是最新的.我可以使用此数据提供程序,而不必总是更新变量或对 ID 进行硬编码.
... The list goes on. This is really useful if I were, let's say - always wanting to pull the latest AMI matching some tags, and keep a launch configuration up to date with it. I could use this data provider rather than always have to update a variable or hard-code the ID.
数据源也可以用于其他原因;我最喜欢的一个是 模板提供程序.
Data source can be used for other reasons as well; one of my favorites is the template provider.
祝你好运!
这篇关于Terraform 中如何使用数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!