Terraform中如何使用数据源? [英] How are data sources used in Terraform?

查看:169
本文介绍了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.

让我们以其文档中的示例:

Let's take the example from their documentation:

# 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆