如何将资源块(例如:资源“ aws卷附件”“ ebs att”)作为输入发送到tf文件
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.example.id}"
instance_id = "${aws_instance.example.id}"
force_detach = "true"
}
假设,下面是我的terraform .tf文件
provider "aws" {
region = "${var.region}"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_key_pair" "deployer" {
key_name = "testkey-${var.hostname}"
public_key = "${file(var.public_key_path)}"
}
对于此文件,我必须发送资源块->
resource "aws_volume_attachment" "ebs_att"
最终的.tf文件看起来像
provider "aws" {
region = "${var.region}"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_key_pair" "deployer" {
key_name = "testkey-${var.hostname}"
public_key = "${file(var.public_key_path)}"
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.example.id}"
instance_id = "${aws_instance.example.id}"
force_detach = "true"
}
答案 0 :(得分:0)
您将无法单独使用terraform脚本动态添加新资源(为什么我告诉“ 单独”是因为在我的情况下,我已经使用python类作为配置并生成terraform动态地从文件中提取文件并从python脚本运行apply命令)
您可以通过向资源添加计数并将默认变量值设置为0来实现此目的,除非您将计数值专门设置为1,否则通常不会创建资源。
variable "create_ebs_resource" {
type = number
default = 0
}
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.example.id}"
instance_id = "${aws_instance.example.id}"
force_detach = "true"
count = var.create_ebs_resource
}
然后运行apply命令为
terraform apply -var="create_ebs_resource=1"
(如果您想创建资源),否则只需运行terraform apply