在terraform中,如何获取数据资源的结果?

时间:2020-06-04 00:16:21

标签: terraform terraform-provider-aws

terraform v0.12.17。我已经读过Data Source: aws_ebs_snapshot,但没有告诉我如何使用结果。

说我想根据最新的AWS快照创建AWS卷。如何使用data {}资源获取最新的快照,然后使用它创建卷?

provider "aws" {
  region = "us-east-1"
}

data "aws_ebs_snapshot_ids" "jenkins_master" {    
  filter {
    name   = "tag:Name"
    values = ["jenkins-master"]
  }
}

resource "aws_ebs_volume" "jenkins_master_ebs" {
  availability_zone = "us-east-1a"
  size              = ??? // taken from result of above data{}
  snapshot_id       = ??? // taken from result of above data{}

  tags = {
    Name = "jenkins-master-ebs"
  }
}

1 个答案:

答案 0 :(得分:1)

根据Terraform文档,您将获得ids的列表,因此可以使用以下内容:

resource "aws_ebs_volume" "jenkins_master_ebs" {
  availability_zone = "us-east-1a"
  size              = 50 # Your custom size
  snapshot_id       = data.aws_ebs_snapshot_ids.jenkins_master.ids.0 # First element

  tags = {
    Name = "jenkins-master-ebs"
  }
}