2017-09-25 58 views
0

我有两个计划,在这两个计划中,我创建了两个不同的服务器(例如,否则它非常复杂)。在一个计划,我outputing安全组这样的值:从一个计划到另一个计划的资源输出值

output "security_group_id" { 
    value = "${aws_security_group.security_group.id}" 
} 

我有第二个计划,其中我想使用这个值,我怎么能实现它,我已经试过几件事情,但没有为我工作。

我知道如何使用moduleoutput价值回报,但不知道我怎么可以用一个计划output到另一个。

回答

1

当在配置的顶级模块(运行terraform plan的目录)中使用输出时,其值将记录在Terraform状态中。

为了从另一个配置中使用此值,必须将状态发布到可由其他配置读取的位置。通常的做法是使用Remote State

随着对第一配置启用远程状态,因此能够从使用the terraform_remote_state data source所述配置读取所得到的值。

例如,它可以通过使用类似如下的后端配置,以保持在亚马逊S3的第一配置状态:

terraform { 
    backend "s3" { 
    bucket = "example-s3-bucket" 
    key = "example-bucket-key" 
    region = "us-east-1" 
    } 
} 

加入这第一次配置后,Terraform会提示你运行terraform init初始化新的后端,其中包括迁移存储在S3上的现有状态。

然后在配置这可以通过提供相同的配置到terraform_remote_state数据源检索到:

data "terraform_remote_state" "example" { 
    backend = "s3" 
    config { 
    bucket = "example-s3-bucket" 
    key = "example-bucket-key" 
    region = "us-east-1" 
    } 
} 

resource "aws_instance" "foo" { 
    # ... 
    vpc_security_group_ids = "${data.terraform_remote_state.example.security_group_id}" 
} 

注意,由于第二配置从所述第一读出的状态,有必要terraform apply第一次配置,以便这个值将被实际记录在状态中。任何时候在第一个输出发生变化时,第二个配置都必须重新应用。

+0

如果我有本地'state'文件,该怎么办? –