<p>A better approach I came to discover was using the <code>user_data</code> of a launch configuration. While it still suffers some of the same issues that provisioners introduce, there's a lot less room for error in terms of permissions and timing as seen in <a href="https://stackoverflow.com/questions/69488032/terraform-how-to-mount-efs-access-point-to-ec2">other questions</a>.</p>
<p>For Example:</p>
<pre class="lang-hcl prettyprint-override"><code>resource &quot;aws_launch_configuration&quot; &quot;sample&quot; {
  image_id          = &quot;ami-04afe279c8bff9ed8&quot;
  instance_type = &quot;t2.micro&quot;
  security_groups = [
    aws_security_group.efs.id,
  ]

  user_data = &lt;&lt;-EOF
              #!/bin/bash
              mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport ${module.efs_mount.file_system_dns_name}:/ /your/mount/point/
              EOF
  lifecycle {
    create_before_destroy = true
  }
}
</code></pre>
<p>I should note, that this also implies my additional use of an Auto Scaling Group - which may or may not fit into everyone's requirements.</p>

CC BY-SA 4.0
2021-11-04T14:28:19.163Z
2021-11-04T14:38:15.130Z
2021-11-04T14:38:15.130Z
AndrewGentry
<p>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:</p>
<pre><code>$  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
</code></pre>
<p>As you can see from my string interpolation, I have already attempted to set things up in a Terraform <code>provisioner &quot;remote-exec&quot;</code> block when the EC2 instances get created.</p>
<p>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.</p>
<p><strong>Is this the right approach?</strong>  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 <a href="https://www.terraform.io/docs/language/resources/provisioners/syntax.html" rel="nofollow noreferrer">the docs</a>)</p>
<p><em>Networking Error for context:</em></p>
<pre><code>│ Error: remote-exec provisioner error
│ 
│   with aws_instance.example-instance-with-efs[0],
│   on main.tf line 72, in resource &quot;aws_instance&quot; &quot;example-instance-with-efs&quot;:
│   72:   provisioner &quot;remote-exec&quot; {
│ 
│ error executing &quot;/tmp/terraform_1760279708.sh&quot;: Process exited with status 32
</code></pre>

CC BY-SA 4.0
2021-11-03T16:06:52.373Z
I think this is worth checking out => [Link](https://stackoverflow.com/questions/69488032/terraform-how-to-mount-efs-access-point-to-ec2)
Kristian Kanchev
CC BY-SA 4.0
2021-11-04T07:45:27.987Z
I’m voting to close this question because duplicate of https://stackoverflow.com/questions/69488032/terraform-how-to-mount-efs-access-point-to-ec2
Bruce Becker
CC BY-SA 4.0
2021-11-04T14:34:38.710Z
@BruceBecker - I've added an answer, which I feel adds value as it highlights a solution to the "approach" part of the original question. The SO duplicate you mention seems to only address the permissions problem that user was facing.  Happy to move wherever, but I do feel the answer I provided may be valuable to keep around?
AndrewGentry
CC BY-SA 4.0
2021-11-04T16:40:42.140Z
Thanks @AndrewGentry! this is useful.
Bruce Becker
<amazon-web-services><terraform><amazon-ec2><amazon-efs>
How to mount EC2 intances to AWS EFS via Terraform
AndrewGentry
# How to mount EC2 intances to AWS EFS via Terraform

(Asked by: username_0 on 2021-11-03)

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

**Tags:** amazon-web-services, terraform, amazon-ec2, amazon-efs

> **Comments:**
> **username_1**: I think this is worth checking out => [Link](https://stackoverflow.com/questions/69488032/terraform-how-to-mount-efs-access-point-to-ec2)  
> **username_2**: I’m voting to close this question because duplicate of https://stackoverflow.com/questions/69488032/terraform-how-to-mount-efs-access-point-to-ec2  
> **username_0**: @BruceBecker - I've added an answer, which I feel adds value as it highlights a solution to the "approach" part of the original question. The SO duplicate you mention seems to only address the permissions problem that user was facing.  Happy to move wherever, but I do feel the answer I provided may be valuable to keep around?  
> **username_2**: Thanks @username_0! this is useful.  

---

## 1 answers

### Answer by username_0 on 2021-11-04. Score: 0

A better approach I came to discover was using the user_data of a launch configuration. While it still suffers some of the same issues that provisioners introduce, there's a lot less room for error in terms of permissions and timing as seen in other questions.
For Example:
resource "aws_launch_configuration" "sample" {
  image_id          = "ami-04afe279c8bff9ed8"
  instance_type = "t2.micro"
  security_groups = [
    aws_security_group.efs.id,
  ]

  user_data = <<-EOF
              #!/bin/bash
              mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport ${module.efs_mount.file_system_dns_name}:/ /your/mount/point/
              EOF
  lifecycle {
    create_before_destroy = true
  }
}

I should note, that this also implies my additional use of an Auto Scaling Group - which may or may not fit into everyone's requirements.


---

