📝

CloudFormation で EC2 起動時に FSx for NetApp ONTAP をマウントしてみた

に公開

ユーザーデータを使用して NFS でマウントしてみました。

CloudFormation テンプレート

AWSTemplateFormatVersion: "2010-09-09"
Description: "EC2 instance with FSx for NetApp ONTAP NFS mount using existing configurations"

Parameters:
  FSxDnsName:
    Description: DNS name of the FSx for NetApp ONTAP file system
    Type: String
    Default: <SVM の NFS DNS 名>

  FSxVolumeName:
    Description: Name of the volume to mount
    Type: String
    Default: vol1

  MountPoint:
    Description: Directory to mount the FSx file system on
    Type: String
    Default: /mnt/fsx

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-xxx
      InstanceType: t2.micro
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          DeviceIndex: 0
          GroupSet:
            - sg-xxx
          SubnetId: subnet-xxx
      IamInstanceProfile: xxx
      Tags:
        - Key: Name
          Value: FSx-NFS-Client
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash -xe

          # Update system packages
          dnf update -y

          # Install NFS utilities
          dnf install -y nfs-utils

          # Create mount point directory
          mkdir -p ${MountPoint}

          # Mount FSx for NetApp ONTAP using NFS
          mount -t nfs ${FSxDnsName}:/${FSxVolumeName} ${MountPoint}

          # Add entry to /etc/fstab for persistent mount after reboot
          echo "${FSxDnsName}:/${FSxVolumeName} ${MountPoint} nfs defaults,_netdev 0 0" >> /etc/fstab

          # Set appropriate permissions
          chown ec2-user:ec2-user ${MountPoint}

マウント確認

$ df -h
Filesystem                                                                         Size  Used Avail Use% Mounted on
devtmpfs                                                                           4.0M     0  4.0M   0% /dev
tmpfs                                                                              475M     0  475M   0% /dev/shm
tmpfs                                                                              190M  444K  190M   1% /run
/dev/xvda1                                                                         8.0G  1.6G  6.4G  20% /
tmpfs                                                                              475M     0  475M   0% /tmp
/dev/xvda128                                                                        10M  1.3M  8.7M  13% /boot/efi
svm-0e32cc82ff06095d4.fs-05402a0e263fc7e20.fsx.ap-northeast-1.amazonaws.com:/vol1  973G  113G  861G  12% /mnt/fsx
tmpfs

$ mount | grep fsx
svm-0e32cc82ff06095d4.fs-05402a0e263fc7e20.fsx.ap-northeast-1.amazonaws.com:/vol1 on /mnt/fsx type nfs4 (rw,relatime,vers=4.2,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.31.41.121,local_lock=none,addr=172.31.19.0)

$ ls -la /mnt/fsx
total 4
drwxr-xr-x. 2 ec2-user ec2-user 4096 May 30 05:39 .
drwxr-xr-x. 3 root     root       17 May 30 05:47 ..

まとめ

今回は CloudFormation で EC2 起動時に FSx for NetApp ONTAP をマウントしてみました。
どなたかの参考になれば幸いです。

Discussion