Skip to content

Instantly share code, notes, and snippets.

@wcheek
Created April 22, 2022 08:04
Show Gist options
  • Save wcheek/59064888c3552cf822c05cb5dd941e27 to your computer and use it in GitHub Desktop.
Save wcheek/59064888c3552cf822c05cb5dd941e27 to your computer and use it in GitHub Desktop.
AWS CDK EC2 instance with EFS attached
self.file_system.connections.allow_default_port_from(self.ec2_instance)
self.ec2_instance.user_data.add_commands(
"yum check-update -y",
"yum upgrade -y",
"yum install -y amazon-efs-utils",
"yum install -y nfs-utils",
"file_system_id_1=" + self.file_system.file_system_id,
"efs_mount_point_1=/mnt/efs/fs1",
'mkdir -p "${efs_mount_point_1}"',
'test -f "/sbin/mount.efs" && echo "${file_system_id_1}:/ ${efs_mount_point_1} efs defaults,_netdev" >> /etc/fstab || " + "echo "${file_system_id_1}.efs." + Stack.of(self).region + ".amazonaws.com:/ ${efs_mount_point_1} nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport,_netdev 0 0" >> /etc/fstab',
"mount -a -t efs,nfs4 defaults",
)
@wcheek
Copy link
Author

wcheek commented Apr 22, 2022

Actually this one is probably better:

  self.file_system.connections.allow_default_port_from(self.ec2_instance)

  self.ec2_instance.user_data.add_commands(
      "yum check-update -y",
      "yum upgrade -y",
      "yum install -y amazon-efs-utils",
      "yum install -y nfs-utils",
      "file_system_id_1=" + self.file_system.file_system_id,
      "efs_mount_point_1=/mnt/efs/fs1",
      'mkdir -p "${efs_mount_point_1}"',
      'sudo mount -t efs -o tls "${file_system_id_1}":/ "${efs_mount_point_1}"'
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment