CLI的wait子命令阻塞等待狀態更新

通常創建一個spot請求或創建一個instance比較費時,

創建命令完成後不能馬上執行ssh等命令。

AWS的CLI提供了一個wait子命令來阻塞住命令,幫助腳本的順序正常執行。

下面以創建一個Spot EC2實例爲例子,演示wait的功能。


查看spot價格記錄

aws ec2 describe-spot-price-history --start-time $(date +"%Y%m%dT%H0000") --product "Linux/UNIX (Amazon VPC)" --instance-type "m3.medium" --region us-east-1 --output table


創建Spot請求

aws ec2 request-spot-instances --region us-east-1 --spot-price "0.010" --instance-count 1 --type "one-time" --launch-specification "{\"ImageId\":\"ami-1ecae776\",\"InstanceType\":\"m3.medium\",\"SubnetId\":\"subnet-ID\",  \"SecurityGroupIds\": [\"sg-ID\"],\"KeyName\": \"PemFile\"}"


等待請求被接受。由於使用了wait命令,直到接受,命令纔會返回,否則一直阻塞

 aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids sir-xxxxxxx --region us-east-1


根據請求獲得EC2實例的id

 aws ec2 describe-spot-instance-requests --spot-instance-request-ids sir-xxxxxxx  --region us-east-1


等待實例被創建完成。這一步很快會返回,但是還不能ssh連接,因爲需要做初始化配置

 aws ec2 wait instance-running --instance-ids  i-xxxxxxx --region us-east-1 


等待實例成爲可訪問狀態。這一步大概需要等待2,3分鐘

 
aws ec2 wait instance-status-ok --instance-ids  i-xxxxxxx --region us-east-1

等上一步完成,實例纔算真正創建完成,能通過ssh或RDP連接上。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章