How are data sources used in Terraform?

后端 未结 3 998
说谎
说谎 2020-12-24 05:37

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 differ

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 06:01

    Data sources are used to fetch the data from the provider end, so that It can be used as configuration in .tf files, Instead of hardcoding it. Example: Below code fetches the AWS AMI ID and uses it to launch AWS instance.

    data "aws_ami" "std_ami" {
      most_recent = true
      owners      = ["amazon"]
    
    filter {
        name   = "root-device-type"
        values = ["ebs"]
      }
    
    filter {
        name   = "virtualization-type"
        values = ["hvm"]
      }
    }
    
    resource "aws_instance" "myec2" {
      ami           = data.aws_ami.std_ami.id
      instance_type = "t2.micro"
    }
    

提交回复
热议问题