- Newest
- Most votes
- Most comments
On Amazon Linux 2023 and specifically in your QuestDB-based AMI, utilities like parted
, mkfs.ext4
, etc., are located in /usr/sbin
and /sbin
, but those directories may not be in PATH
during execution unless explicitly set.
However, simply setting export PATH=...
in the script often has no effect when using InitCommand
, because each command runs in a fresh shell.
Solution: Use full paths for commands
To avoid issues, use the full absolute paths for the tools. Here’s a cleaned-up version of your script that works reliably:
#!/bin/bash TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \ -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" \ http://169.254.169.254/latest/meta-data/instance-id) aws ec2 attach-volume --device /dev/xvdc --instance-id "$INSTANCE_ID" --volume-id "$VOLUME_ID" echo "volume attached, waiting for device to become available" while ! lsblk | grep xvdc; do sleep 0.1 done /usr/sbin/parted -s -a opt /dev/xvdc mklabel gpt /usr/sbin/parted -s -a opt /dev/xvdc mkpart primary ext4 0% 100% /usr/sbin/mkfs.ext4 /dev/xvdc1 mkdir -p /mnt mount /dev/xvdc1 /mnt
Alternative: Use a wrapper script
If you want to avoid using full paths everywhere and ensure PATH is available, wrap your logic in a script and invoke it using bash -l
(login shell), which sources /etc/profile
and sets up the environment.
Modify InitCommand
like this:
ec2.InitCommand.shellCommand("bash -l -c '/opt/init/init.sh'", { env: { VOLUME_ID: volume.volumeId, }, }),
This ensures the system paths from login shells are respected (/sbin
, /usr/sbin
, etc.).
Relevant content
- AWS OFFICIALUpdated 2 years ago