TL;DR
- EC2の起動後にEFSをマウントを行う
- amazon-efs-utilsパッケージを使用する
How to
EFSの準備
EFSの作成
$ aws efs create-file-system \ --performance-mode generalPurpose \ --throughput-mode bursting \ --tags Key=Name,Value=tmp-my-efs
EFS用のセキュリティグループの作成
$ export SG_NAME=tmp-my-efs
$ aws ec2 create-security-group \
--group-name ${SG_NAME} \
--description "tmp efs sg" \
--vpc-id <VPC ID>
$ aws ec2 authorize-security-group-ingress
--group-name ${SG_NAME} \
--protocol tcp \
--port 2049 \
--cidr <YOUR VPC CIDR>
EC2を起動
$ aws ec2 run-instances \
--image-id ami-0bb2c57f7cfafb1cb \ # AL2023 ap-northeast-1a
--count 1 \
--instance-type t3.micro \
--subnet-id <YOUR SUBNET> \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=TmpEFSEC2}]'
EC2にEFSのマウント
EFSのパッケージをインストール
$ sudo dnf -y install amazon-efs-utils
マウント先ディレクトリを作成
$ sudo mkdir /mnt/efs
マウントしていないことを確認。
sh-5.2$ sudo df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 4096 0 4096 0% /dev tmpfs 463284 0 463284 0% /dev/shm tmpfs 185316 424 184892 1% /run /dev/nvme0n1p1 8310764 2211620 6099144 27% / tmpfs 463288 0 463288 0% /tmp /dev/nvme0n1p128 10202 1314 8888 13% /boot/efi tmpfs 92656 0 92656 0% /run/user/0
EFSのマウント
$ sudo mount -t efs -o tls fs-xxxxxxxxxx.efs.ap-northeast-1.amazonaws.com:/ /mnt/efs
マウントされていることを確認
sh-5.2$ sudo df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 4096 0 4096 0% /dev tmpfs 463284 0 463284 0% /dev/shm tmpfs 185316 488 184828 1% /run /dev/nvme0n1p1 8310764 2209312 6101452 27% / tmpfs 463288 0 463288 0% /tmp /dev/nvme0n1p128 10202 1314 8888 13% /boot/efi tmpfs 92656 0 92656 0% /run/user/0 127.0.0.1:/ 9007199254739968 0 9007199254739968 0% /mnt/efs # <= EFSがマウントされていることを確認
MEMO: IAM認証する場合
mount時に iam オプションを追加する
$ sudo mount -t efs -o tls,iam fs-xxxxxxxxxx.efs.ap-northeast-1.amazonaws.com:/ /mnt/efs
EFSのファイルシステムポリシー
注意点として、PrincipalにはインスタンスプロファイルではなくインスタンスプロファイルのロールのARNを設定
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::0000000000000:role/ec2-role" }, "Action": [ "elasticfilesystem:ClientWrite", "elasticfilesystem:ClientMount", "elasticfilesystem:ClientRootAccess" ], "Resource": "arn:aws:elasticfilesystem:ap-northeast-1:000000000000:file-system/fs-xxxxxxxxxxx", "Condition": { "Bool": { "elasticfilesystem:AccessedViaMountTarget": "true" } } } ] }