当在配置的顶级模块(运行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
第一次配置,以便这个值将被实际记录在状态中。任何时候在第一个输出发生变化时,第二个配置都必须重新应用。
如果我有本地'state'文件,该怎么办? –