I have launched two EC2 instances in two availability zones and I need to mount the EFS in both the instances using Terraform.
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}"]
}
Using the above code I am able to create EFS in us-east-1a. I need to make it available in both us-east-1a and us-east-1b.
By default, every EFS file system object (such as directory, file, and link) is redundantly stored across multiple AZs for file systems using Standard storage classes. If you select Amazon EFS One Zone storage classes, your data is redundantly stored within a single AZ.
With Elastic File System (EFS), you can share data between multiple EC2 instances and your data is replicated between multiple Availability Zones (AZ). EFS is based on the NFSv4. 1 protocol,which allows you to mount it like any other file system.
Building the Terraform Configuration for an AWS EFS Log in to the Ubuntu machine with your favorite SSH client. 3. Open your preferred code editor, and create a file called main.tf inside the ~/terraform-amazon-efs-demo directory. Copy/paste the following configuration to the main.tf file, and save the changes.
There are four steps that you need to perform to create and use your first Amazon EFS file system: Create your Amazon EFS file system. Create your Amazon EC2 resources, launch your instance, and mount the file system. Transfer files to your EFS file system using AWS DataSync.
You just need to add another mount target in a subnet in 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}"]
}
More elegant (using count
dependent on the number of subnets):
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}"]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With