0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

EC2のステータスがrunningになるまで待つshell関数

Posted at

AWS ClientでEC2インスタンスを立ち上げてからEIPを振りたいけど、
そのまま順番にコマンドを並べてもEC2はまずpendingステータスで起動するので連携でこける。

なのでrunningになるまで待つ関数を用意。
リトライの最大カウント数なども追加して利用予定。

#!/bin/bash


wait_ec2pending() {

    echo "instance-id: "$instance_id
    while :
    do
        instance_status=$(aws ec2 describe-instance-status \
            --instance-ids $instance_id \
            --query 'InstanceStatuses[].InstanceState.Name' | jq -r .[0])

        if [ "$instance_status" = "running" ]; then
            echo "OK: instance status running"
            break;
        fi
        echo "waiting for instance status change..."
        sleep 3
    done
}


# set one instance-id
instance_id="<your-instance-id>"
wait_ec2pending

echo "next process"
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?