【发布时间】:2017-05-26 13:41:49
【问题描述】:
有什么方法可以将 Terraform 模板输出用于另一个 Terraform 模板的输入?
例如:我有一个 Terraform 模板,它创建一个 ELB,我还有另一个 Terraform 模板,它将创建一个需要 ELB 信息作为输入变量的自动缩放组。
我知道我可以使用 shell 脚本到 grep 并输入 ELB 信息,但我正在寻找一些 Terraform 方法来执行此操作。
【问题讨论】:
标签: terraform
有什么方法可以将 Terraform 模板输出用于另一个 Terraform 模板的输入?
例如:我有一个 Terraform 模板,它创建一个 ELB,我还有另一个 Terraform 模板,它将创建一个需要 ELB 信息作为输入变量的自动缩放组。
我知道我可以使用 shell 脚本到 grep 并输入 ELB 信息,但我正在寻找一些 Terraform 方法来执行此操作。
【问题讨论】:
标签: terraform
您是否尝试过使用远程状态来填充您的第二个模板?
这样声明:
resource "terraform_remote_state" "your_state" {
backend = "s3"
config {
bucket = "${var.your_bucket}"
region = "${var.your_region}"
key = "${var.your_state_file}"
}
}
然后你应该可以像这样直接提取你的资源:
your_elb = "${terraform_remote_state.your_state.output.your_output_resource}"
如果这对您不起作用,您是否尝试过在模块中实现 ELB,然后只使用输出?
https://github.com/terraform-community-modules/tf_aws_elb 是如何构建模块的一个很好的例子。
【讨论】:
your_elb = terraform_remote_state.your_state.output.your_output_resource
看起来在较新版本的 Terraform 中,您可以像这样访问输出变量
your_elb = "${data.terraform_remote_state.your_state.your_output_resource}"
其余的都是一样的,只是你引用它的方式。
【讨论】:
问题是关于 ELB,但我将举一个 S3 的例子。写的东西比较少。 如果您不知道如何在 AWS 上存储 terraform 状态,请阅读article。
假设您有两个独立的项目:project-1、project-2。它们位于两个不同的目录(两个不同的存储库)中!
地形文件/tmp/project-1/main.tf:
// Create an S3 bucket
resource "aws_s3_bucket" "main_bucket" {
bucket = "my-epic-test-b1"
acl = "private"
}
// Output. It will available on s3://multi-terraform-project-state-bucket/p1.tfstate
output "bucket_name_p1" {
value = aws_s3_bucket.main_bucket.bucket
}
// Store terraform state on AWS. The S3 bucket and dynamo db table should be created before running terraform
terraform {
backend "s3" {
bucket = "multi-terraform-project-state-bucket"
key = "p1.tfstate"
dynamodb_table = "multi-terraform-project-state-table"
region = "eu-central-1" // AWS region of state resources
}
}
provider "aws" {
profile = "my-cli-profile" // User profile defined in ~/.aws/credentials
region = "eu-central-1" // AWS region
}
您运行 terraform init,然后 terraform apply。
之后,您将移至 terraform 文件 /tmp/project-2/main.tf:
// Create an S3 bucket
resource "aws_s3_bucket" "main_bucket" {
bucket = "my-epic-test-b2"
acl = "private"
tags = {
// Get the S3 bucket name from another terraform state file. In this case it is s3://multi-terraform-project-state-bucket/p1.tfstate
p1-bucket = data.terraform_remote_state.state1.outputs.bucket_name_p1
}
}
// Get date from another state file
data "terraform_remote_state" "state1" {
backend = "s3"
config = {
bucket = "multi-terraform-project-state-bucket"
key = "p1.tfstate"
region = "eu-central-1"
}
}
// Store terraform state on AWS. The S3 bucket and dynamo db table should be created before running terraform
terraform {
backend "s3" {
bucket = "multi-terraform-project-state-bucket"
key = "p2.tfstate"
dynamodb_table = "multi-terraform-project-state-table"
region = "eu-central-1" // AWS region of state resources
}
}
provider "aws" {
profile = "my-cli-profile" // User profile defined in ~/.aws/credentials
region = "eu-central-1" // AWS region
}
您运行 terraform init,然后 terraform apply。
现在检查 my-epic-test-b2 中的标签。您将在那里找到 project-1 中的存储桶的名称。
【讨论】: