Finally, I use this launch template:
#!/bin/bash
# Install AWS CLI v2
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
rm -rf awscliv2.zip aws
# Define the volume ID
VOLUME_ID=vol-09a5e06d571ee3f79
# Request a token for IMDSv2
TOKEN=$(curl -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" -s "http://169.254.169.254/latest/api/token")
# Retrieve the instance ID using the token
INSTANCE_ID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" "http://169.254.169.254/latest/meta-data/instance-id")
if [ -z "$INSTANCE_ID" ]; then
echo "Failed to retrieve instance ID. Exiting."
exit 1
fi
echo "Instance ID: $INSTANCE_ID"
# Attempt to attach the volume
aws ec2 attach-volume --volume-id $VOLUME_ID --instance-id $INSTANCE_ID --device /dev/sdf
# Create mount directory
sudo mkdir -p /mnt/mydata
# Wait for the device to be recognized (important for auto-mounting)
sleep 10
# Mount the volume
sudo mount /dev/nvme1n1p1 /mnt/mydata
Thank you very much.