AWS/EC2

EC2 생성과 동시에 EFS Mount(Linux)

나참새 2020. 5. 26. 18:13

[목표]

EC2 생성과 동시에 특정 EFS를 볼륨에 마운트 하기 위해 사용자 데이터를 적용합니다.

 

[전제조건]

 

1) Target이 되는 EFS를 먼저 생성하고, 해당 EFS의 DNS를 미리 확인합니다.

(efs의 dns : file-system-id.efs.aws-region.amazonaws.com)

 

2) 파일 시스템 정책을 추가합니다. (2020-07-21 확인결과 필수사항으로 추가됨)

 

{
    "Version": "2012-10-17",
    "Id": "efs-policy",
    "Statement": [
        {
            "Sid": "efs-statement-example",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": [
                "elasticfilesystem:ClientMount",
                "elasticfilesystem:ClientWrite",
                "elasticfilesystem:ClientRootAccess"
            ],
            "Resource": "arn:aws:elasticfilesystem:your-region:your-id:file-system/your-fs-id"
        }
    ]
}

 

[userdata]

 

#!/bin/bash
# Install AWS EFS Utilities
yum install -y amazon-efs-utils
# Mount EFS
mkdir /efs
efs_dns="your_efs_dns"
mount -t efs $efs_dns:/ /efs
# Edit fstab so EFS automatically loads on reboot
echo $efs_dns:/ /efs efs defaults,_netdev 0 0 >> /etc/fstab

 

!efs_dns가 아닌 efs_id로 작성하면 재부팅시 마운트가 적용되지 않습니다!

이제 efs_dns가 아닌 efs_id로 작성해도 재부팅이 가능합니다. (2020-07-21)

 

[결과물]

 

생성과 동시에 마운트 된 efs를 확인할 수 있습니다.

해당 작업결과, EC2를 재부팅 한 후에도 EFS 마운트를 유지합니다.

 

[참고]

 

https://medium.com/@wblakecannon/add-efs-to-an-amazon-linux-2-aws-ec2-instance-with-terraform-bb073b6de7

 

Add EFS to an Amazon Linux 2 AWS EC2 Instance with Terraform

Terraform is a great tool to create cloud infrastructure as code. I have been spending time with it the past few months and have really…

medium.com

https://docs.aws.amazon.com/ko_kr/efs/latest/ug/installing-amazon-efs-utils.html

 

Amazon EFS 클라이언트 수동 설치 - Amazon Elastic File System

이 페이지에 작업이 필요하다는 점을 알려 주셔서 감사합니다. 실망시켜 드려 죄송합니다. 잠깐 시간을 내어 설명서를 향상시킬 수 있는 방법에 대해 말씀해 주십시오.

docs.aws.amazon.com