79164790

Date: 2024-11-07 01:54:58
Score: 3.5
Natty:
Report link

I use this in user data, not work:

#!/bin/bash
# Variables
VOLUME_TAG_KEY="Purpose"
VOLUME_TAG_VALUE="MyAppData"
DEVICE_NAME="/dev/sdf"  # Update as needed

# Get instance details
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)

# Locate the correct EBS volume by tag
VOLUME_ID=$(aws ec2 describe-volumes --region $REGION --filters "Name=tag:$VOLUME_TAG_KEY,Values=$VOLUME_TAG_VALUE" "Name=status,Values=available" --query "Volumes[0].VolumeId" --output text)

# Attach volume if found
if [ "$VOLUME_ID" != "None" ]; then
    echo "Attaching volume $VOLUME_ID to $INSTANCE_ID at $DEVICE_NAME"
    aws ec2 attach-volume --region $REGION --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device $DEVICE_NAME

    # Wait until the volume is attached
    while [ "$(aws ec2 describe-volumes --volume-ids $VOLUME_ID --query "Volumes[0].Attachments[0].State" --output text)" != "attached" ]; do
        echo "Waiting for volume to attach..."
        sleep 5
    done

    # Mount the volume
    mkdir -p /data
    mount $DEVICE_NAME /data
    echo "$DEVICE_NAME /data ext4 defaults,nofail 0 2" >> /etc/fstab
    echo "Volume $VOLUME_ID attached and mounted at /data"
else
    echo "No available volume found with tag $VOLUME_TAG_KEY=$VOLUME_TAG_VALUE. Exiting."
    exit 1
fi

Actually, I would like to mount to the /dev/sda1, could anyone help?

Thank you very much.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • RegEx Blacklisted phrase (3): could anyone help
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: ปลาวาฬทราย