我已经编写了脚本来从 VPC 中查找实例 ID,并为每个实例 ID 创建 AMI:
#!/bin/bash
#Script to Automate AMI backup
echo "----------------------------------\n `date` \n----------------------------------"
aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-1c927479 | awk '{ print $8 }' | sort -n | grep "i-" > /tmp/instanceid.txt
echo "Starting the Daily AMI creation: "
#To create AMI from instance-id
for i in $(cat /tmp/instanceid.txt); do
echo "Creating AMI for Instance id $i ......."
echo "instance-`date +%d%b%y`-$i" > /tmp/aminame.txt
aws ec2 create-image --instance-id $i --name "`cat /tmp/aminame.txt`" --description "This is created by ami-backup.sh" --no-reboot | grep -ir ami | awk '{print $4}' > /tmp/amiID.txt
echo "AMI Name is: `cat /tmp/aminame.txt`\n"
echo done
done
但是我想创建不包括每个卷附加卷的 AMI!
ec2-create-image --block-device-mapping /dev/sdh=none
这将创建不带附加卷的 ami,但每个实例的 /dev/sdh 都会有所不同,那么如何自动化此部分?有什么提示吗?
答案1
命令:$ aws ec2 describe-images --image-ids ami-xxxx |grep "/dev" | awk '{print $2}'
输出 :
/dev/sda1
这就是我们如何找到附加卷的列表并将其传递给“ec2-create-image --block-device-mapping /dev/sdh=none”