|
| 1 | +#!/bin/bash |
| 2 | +# Tutorial: Create an Amazon EFS file system |
| 3 | +# Source: https://docs.aws.amazon.com/efs/latest/ug/getting-started.html |
| 4 | + |
| 5 | +WORK_DIR=$(mktemp -d) |
| 6 | +LOG_FILE="$WORK_DIR/efs-$(date +%Y%m%d-%H%M%S).log" |
| 7 | +exec > >(tee -a "$LOG_FILE") 2>&1 |
| 8 | + |
| 9 | +REGION=${AWS_DEFAULT_REGION:-${AWS_REGION:-$(aws configure get region 2>/dev/null)}} |
| 10 | +if [ -z "$REGION" ]; then |
| 11 | + echo "ERROR: No AWS region configured. Set one with: export AWS_DEFAULT_REGION=us-east-1" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | +export AWS_DEFAULT_REGION="$REGION" |
| 15 | +echo "Region: $REGION" |
| 16 | + |
| 17 | +RANDOM_ID=$(openssl rand -hex 4) |
| 18 | +FS_TOKEN="tut-efs-${RANDOM_ID}" |
| 19 | + |
| 20 | +handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; } |
| 21 | +trap 'handle_error $LINENO' ERR |
| 22 | + |
| 23 | +cleanup() { |
| 24 | + echo "" |
| 25 | + echo "Cleaning up resources..." |
| 26 | + if [ -n "$FS_ID" ]; then |
| 27 | + # Delete mount targets first |
| 28 | + for MT_ID in $(aws efs describe-mount-targets --file-system-id "$FS_ID" \ |
| 29 | + --query 'MountTargets[].MountTargetId' --output text 2>/dev/null); do |
| 30 | + aws efs delete-mount-target --mount-target-id "$MT_ID" 2>/dev/null |
| 31 | + echo " Deleted mount target $MT_ID" |
| 32 | + done |
| 33 | + # Wait for mount targets to be deleted |
| 34 | + for i in $(seq 1 12); do |
| 35 | + MT_COUNT=$(aws efs describe-mount-targets --file-system-id "$FS_ID" \ |
| 36 | + --query 'MountTargets | length(@)' --output text 2>/dev/null || echo "0") |
| 37 | + [ "$MT_COUNT" = "0" ] && break |
| 38 | + sleep 10 |
| 39 | + done |
| 40 | + aws efs delete-file-system --file-system-id "$FS_ID" 2>/dev/null && \ |
| 41 | + echo " Deleted file system $FS_ID" |
| 42 | + fi |
| 43 | + rm -rf "$WORK_DIR" |
| 44 | + echo "Cleanup complete." |
| 45 | +} |
| 46 | + |
| 47 | +# Step 1: Create a file system |
| 48 | +echo "Step 1: Creating EFS file system" |
| 49 | +FS_ID=$(aws efs create-file-system --creation-token "$FS_TOKEN" \ |
| 50 | + --performance-mode generalPurpose \ |
| 51 | + --throughput-mode bursting \ |
| 52 | + --encrypted \ |
| 53 | + --tags Key=Name,Value="tutorial-efs-${RANDOM_ID}" \ |
| 54 | + --query 'FileSystemId' --output text) |
| 55 | +echo " File system ID: $FS_ID" |
| 56 | + |
| 57 | +# Step 2: Wait for file system to be available |
| 58 | +echo "Step 2: Waiting for file system to be available..." |
| 59 | +for i in $(seq 1 15); do |
| 60 | + STATE=$(aws efs describe-file-systems --file-system-id "$FS_ID" \ |
| 61 | + --query 'FileSystems[0].LifeCycleState' --output text) |
| 62 | + echo " State: $STATE" |
| 63 | + [ "$STATE" = "available" ] && break |
| 64 | + sleep 5 |
| 65 | +done |
| 66 | + |
| 67 | +# Step 3: Describe the file system |
| 68 | +echo "Step 3: File system details" |
| 69 | +aws efs describe-file-systems --file-system-id "$FS_ID" \ |
| 70 | + --query 'FileSystems[0].{Id:FileSystemId,State:LifeCycleState,Encrypted:Encrypted,Performance:PerformanceMode,Size:SizeInBytes.Value}' --output table |
| 71 | + |
| 72 | +# Step 4: Create a mount target |
| 73 | +echo "Step 4: Creating mount target" |
| 74 | +VPC_ID=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query 'Vpcs[0].VpcId' --output text) |
| 75 | +SUBNET_ID=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPC_ID" --query 'Subnets[0].SubnetId' --output text) |
| 76 | +echo " VPC: $VPC_ID, Subnet: $SUBNET_ID" |
| 77 | + |
| 78 | +MT_ID=$(aws efs create-mount-target --file-system-id "$FS_ID" --subnet-id "$SUBNET_ID" \ |
| 79 | + --query 'MountTargetId' --output text) |
| 80 | +echo " Mount target: $MT_ID" |
| 81 | + |
| 82 | +# Step 5: Wait for mount target |
| 83 | +echo "Step 5: Waiting for mount target to be available..." |
| 84 | +for i in $(seq 1 15); do |
| 85 | + MT_STATE=$(aws efs describe-mount-targets --mount-target-id "$MT_ID" \ |
| 86 | + --query 'MountTargets[0].LifeCycleState' --output text) |
| 87 | + echo " State: $MT_STATE" |
| 88 | + [ "$MT_STATE" = "available" ] && break |
| 89 | + sleep 10 |
| 90 | +done |
| 91 | + |
| 92 | +# Step 6: Describe mount targets |
| 93 | +echo "Step 6: Mount target details" |
| 94 | +aws efs describe-mount-targets --file-system-id "$FS_ID" \ |
| 95 | + --query 'MountTargets[].{Id:MountTargetId,Subnet:SubnetId,State:LifeCycleState,IP:IpAddress}' --output table |
| 96 | + |
| 97 | +# Step 7: Set lifecycle policy |
| 98 | +echo "Step 7: Setting lifecycle policy (move to IA after 30 days)" |
| 99 | +aws efs put-lifecycle-configuration --file-system-id "$FS_ID" \ |
| 100 | + --lifecycle-policies "[{\"TransitionToIA\":\"AFTER_30_DAYS\"}]" > /dev/null |
| 101 | +aws efs describe-lifecycle-configuration --file-system-id "$FS_ID" \ |
| 102 | + --query 'LifecyclePolicies' --output table |
| 103 | + |
| 104 | +echo "" |
| 105 | +echo "Tutorial complete." |
| 106 | +echo "To mount: sudo mount -t nfs4 $FS_ID.efs.$REGION.amazonaws.com:/ /mnt/efs" |
| 107 | +echo "" |
| 108 | +echo "Do you want to clean up all resources? (y/n): " |
| 109 | +read -r CHOICE |
| 110 | +if [[ "$CHOICE" =~ ^[Yy]$ ]]; then |
| 111 | + cleanup |
| 112 | +else |
| 113 | + echo "Resources left running. EFS charges per GB stored." |
| 114 | + echo "Manual cleanup:" |
| 115 | + echo " aws efs delete-mount-target --mount-target-id $MT_ID" |
| 116 | + echo " sleep 60" |
| 117 | + echo " aws efs delete-file-system --file-system-id $FS_ID" |
| 118 | +fi |
0 commit comments