I'm trying to Terraform a few EC2 instances, and would like to automate the provisioning steps that involve mounting their filesystems to EFS. The commands to run this manually are as follows:
$ sudo mkdir /var/www/html/efs-mount-point
$ sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport ${module.efs_mount.file_system_dns_name}:/ /var/www/html/efs-mount-point
As you can see from my string interpolation, I have already attempted to set things up in a Terraform provisioner "remote-exec"
block when the EC2 instances get created.
This works for the most part, as the directory gets created - but I'm running into errors with the actual mounting step. I believe that Terraform may be trying to mount before everything has established proper network connections. I'll include the full error below.
Is this the right approach? I can't help but feel like I could set up the EC2 AMI to be already mounted or have some startup mount script instead of relying on Terraform provisioners (as they are noted as a last-resort approach in the docs)
Networking Error for context:
│ Error: remote-exec provisioner error
│
│ with aws_instance.example-instance-with-efs[0],
│ on main.tf line 72, in resource "aws_instance" "example-instance-with-efs":
│ 72: provisioner "remote-exec" {
│
│ error executing "/tmp/terraform_1760279708.sh": Process exited with status 32