如何根据 Terraform-AWS 中的变量 enable/disable 磁盘?
How to enable/disable disk according to variable in Terraform-AWS?
我想创建一个 AWS 实例,并根据变量,创建或不创建额外的磁盘。这将允许我保留相同的 .tf 文件,并在我需要磁盘时通过命令行变量指定。
...
variable "create-extra-disk" {
default=false
}
...
resource "aws_instance" "my_instance" {
count = "${var.instance_count}"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
ebs_block_device {
# enable = "${var.create-extra-disk}" # I'd like something like this
device_name = "/dev/sdb"
volume_size = 100
volume_type = "gp2"
delete_on_termination = true
}
...
您需要创建两个资源并使用带变量的计数来包含 运行 额外磁盘部分的条件,所有这些都将在单个文件中。
...
variable "create-extra-disk" {
default=false
}
...
resource "aws_instance" "my_instance" {
count = "${var.instance_count && var.create-extra-disk == true ? 1 : 0}"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
}
resource "aws_instance" "my_instance_with_ebs" {
count = "${var.instance_count && var.create-extra-disk == true ? 1 : 0 }"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 100
volume_type = "gp2"
delete_on_termination = true
}
您可以为 ec2、ebs 和 ebs 附件使用单独的资源以实现可配置性,
https://www.terraform.io/docs/providers/aws/r/ebs_volume.html
https://www.terraform.io/docs/providers/aws/r/volume_attachment.html
以上代码似乎是 terraform 0.11 或以下,
variable "create-extra-disk" {
default = true
}
resource "aws_instance" "my_instance" {
count = "${var.instance_count}"
...
}
resource "aws_ebs_volume" "additional" {
count = "${var.create-extra-disk == true ? var.instance_count : 0}"
availability_zone = "${var.region}"
size = 100
type = "gp2"
}
resource "aws_volume_attachment" "ebs_att" {
count = "${var.create-extra-disk == true ? var.instance_count : 0}"
device_name = "/dev/sdb"
volume_id = "${element(aws_ebs_volume.additional.*.id, count.index)}"
instance_id = "${element(aws_instance.my_instance.*.id, count.index)}"
}
我想创建一个 AWS 实例,并根据变量,创建或不创建额外的磁盘。这将允许我保留相同的 .tf 文件,并在我需要磁盘时通过命令行变量指定。
...
variable "create-extra-disk" {
default=false
}
...
resource "aws_instance" "my_instance" {
count = "${var.instance_count}"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
ebs_block_device {
# enable = "${var.create-extra-disk}" # I'd like something like this
device_name = "/dev/sdb"
volume_size = 100
volume_type = "gp2"
delete_on_termination = true
}
...
您需要创建两个资源并使用带变量的计数来包含 运行 额外磁盘部分的条件,所有这些都将在单个文件中。
...
variable "create-extra-disk" {
default=false
}
...
resource "aws_instance" "my_instance" {
count = "${var.instance_count && var.create-extra-disk == true ? 1 : 0}"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
}
resource "aws_instance" "my_instance_with_ebs" {
count = "${var.instance_count && var.create-extra-disk == true ? 1 : 0 }"
ami = "${var.image_id}"
instance_type = "${var.type}"
key_name = "${aws_key_pair.my-keypair.key_name}"
security_groups = ["${aws_security_group.basic_sg.name}"]
ebs_block_device {
device_name = "/dev/sdb"
volume_size = 100
volume_type = "gp2"
delete_on_termination = true
}
您可以为 ec2、ebs 和 ebs 附件使用单独的资源以实现可配置性, https://www.terraform.io/docs/providers/aws/r/ebs_volume.html https://www.terraform.io/docs/providers/aws/r/volume_attachment.html
以上代码似乎是 terraform 0.11 或以下,
variable "create-extra-disk" {
default = true
}
resource "aws_instance" "my_instance" {
count = "${var.instance_count}"
...
}
resource "aws_ebs_volume" "additional" {
count = "${var.create-extra-disk == true ? var.instance_count : 0}"
availability_zone = "${var.region}"
size = 100
type = "gp2"
}
resource "aws_volume_attachment" "ebs_att" {
count = "${var.create-extra-disk == true ? var.instance_count : 0}"
device_name = "/dev/sdb"
volume_id = "${element(aws_ebs_volume.additional.*.id, count.index)}"
instance_id = "${element(aws_instance.my_instance.*.id, count.index)}"
}