我有一个应该做的事情。
提前致谢。
答案 0 :(得分:3)
这是解决此问题的伪代码: -
recipients="user1@example.com,user2@example.com"
#Create AMI Image
aws ec2 create-image --instance-id i-1e0beaaf --name "My server1" --description "An AMI for my server" &> /tmp/ami.log
# Check Exit Status
if [ $? == 0]
then
# Send mail if success
/usr/sbin/sendmail "$recipients" <<EOF
subject: E-mail
from:admin@example.com
"AMI Success"
EOF
else
# Send mail if failure
/usr/sbin/sendmail "$recipients" <<EOF
subject: E-mail
from:admin@example.com
"AMI failure"
EOF
fi
编辑: - 检查AMI状态
aws ec2 create-image --instance-id i-1e0beaaf --name "My server1" --description "An AMI for my server" > /tmp/ami.txt
AMIID=`cat /tmp/ami.txt | grep ami | awk '{print $2}'`
aws ec2 describe-images --image-ids $AMIID | grep "State"
编辑: - 当“AMI状态=可用”时,您可以使用以下脚本发送邮件。
recipients="user1@example.com,user2@example.com"
aws ec2 create-image --instance-id i-1e0beaaf --name "My server" --description "An AMI for my server" > /tmp/ami.txt
AMIID=`cat /tmp/ami.txt | grep ami | awk '{print $2}'|sed 's/.//;s/.$//'`
status=`aws ec2 describe-images --image-ids $AMIID | grep "State" | awk '{print $2}' | sed 's/.$//' | sed 's/.//;s/.$//'`
while [ "$status" != "available" ]
do
status=`aws ec2 describe-images --image-ids $AMIID | grep "State" | awk '{print $2}' | sed 's/.$//'| sed 's/.//;s/.$//'`
##echo "$status"
sleep 5
done
echo "Sending mail"
/usr/sbin/sendmail "$recipients" <<EOF
subject: E-mail
from:admin@example.com
"AMI Success"
EOF
我测试了这个脚本,它运行得很好。当AMI状态可用时,它会发送电子邮件。脚本是自我解释的。
答案 1 :(得分:1)
您还可以使用Amazon SES并将其添加到bash脚本中。
#Check Exit Status
if [ $? == 0]
then
aws sns publish --topic-arn "arn:aws:sns:us-west-2:0123456789012:my-topic" --message $SuccessMessage
else
aws sns publish --topic-arn "arn:aws:sns:us-west-2:0123456789012:my-topic" --message $FailureMessage
fi