5

我在两个可用区启动了两个 EC2 实例,我需要使用 Terraform 在两个实例中安装 EFS。

resource "aws_efs_file_system" "magento-efs" {
   creation_token = "efs-demo"
   performance_mode = "generalPurpose"
   throughput_mode = "bursting"
   encrypted = "true"
 tags = {
     Name = "Magento-EFS"
   }
 }

resource "aws_efs_mount_target" "efs-mount" {
   file_system_id  = "${aws_efs_file_system.magento-efs.id}"
   subnet_id = "${aws_subnet.public_subnet.0.id}"
   security_groups = ["${aws_security_group.efs-sg.id}"]
}

使用上面的代码,我可以在 us-east-1a 中创建 EFS。我需要在 us-east-1a 和 us-east-1b 中都提供它。

4

2 回答 2

9

您只需在 AZ us-east-1b 的子网中添加另一个挂载目标:

resource "aws_efs_mount_target" "efs-mount-b" {
   file_system_id  = "${aws_efs_file_system.magento-efs.id}"
   subnet_id = "${aws_subnet.public_subnet.1.id}"
   security_groups = ["${aws_security_group.efs-sg.id}"]
}

更优雅(使用count取决于子网的数量):

resource "aws_efs_mount_target" "efs-mount" {
   count = "length(aws_subnet.public_subnet.*.id)"
   file_system_id  = "${aws_efs_file_system.magento-efs.id}"
   subnet_id = "${element(aws_subnet.public_subnet.*.id, count.index)}"
   security_groups = ["${aws_security_group.efs-sg.id}"]
}
于 2019-08-18T18:03:16.683 回答
0

我使用 terraform 版本0.14.10。这将起作用。

resource "aws_efs_mount_target" "efs-mount-a" {
   file_system_id  = aws_efs_file_system.magento-efs.id
   subnet_id = aws_subnet.public_subnet.0.id
   security_groups = [aws_security_group.efs-sg.id]
}

resource "aws_efs_mount_target" "efs-mount-b" {
   file_system_id  = aws_efs_file_system.magento-efs.id
   subnet_id = aws_subnet.public_subnet.1.id
   security_groups = [aws_security_group.efs-sg.id]
}
于 2021-04-17T15:11:05.820 回答