EC2: Waiting until a new instance is in running state

前端 未结 6 2181
死守一世寂寞
死守一世寂寞 2021-02-19 23:33

I would like to create a new instance based on my stored AMI.

I achieve this by the following code:

RunInstancesRequest rir = new RunInstancesRequest(ima         


        
相关标签:
6条回答
  • 2021-02-19 23:36

    You can use boto3 waiters, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#waiters

    for this ex: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Waiter.InstanceRunning

    Or in Java https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/ I am sure there are waiters implemented in all the AWS sdks.

    0 讨论(0)
  • 2021-02-19 23:46

    boto3 has:

    instance.wait_until_running()
    

    From the boto3 docs:

    Waits until this Instance is running. This method calls EC2.Waiter.instance_running.wait() which polls EC2.Client.describe_instances() every 15 seconds until a successful state is reached. An error is returned after 40 failed checks.

    0 讨论(0)
  • 2021-02-19 23:47

    From the AWS CLI changelog for v1.6.0:

    Add a wait subcommand that allows for a command to block until an AWS resource reaches a given state (issue 992, issue 985)

    I don't see this mentioned in the documentation, but the following worked for me:

    aws ec2 start-instances --instance-ids "i-XXXXXXXX"
    aws ec2 wait instance-running --instance-ids "i-XXXXXXXX"
    

    The wait instance-running line did not finish until the EC2 instance was running.

    I don't use Python/boto/botocore but assume it has something similar. Check out waiter.py on Github.

    0 讨论(0)
  • 2021-02-19 23:48

    Waiting for the EC2 instance to get ready is a common pattern. In the Python library boto you also solve this with sleep calls:

       reservation = conn.run_instances([Instance configuration here])
       instance = reservation.instances[0]
    
       while instance.state != 'running':
           print '...instance is %s' % instance.state
           time.sleep(10)
           instance.update()
    

    With this mechanism you will be able to poll when your new instance will come up.

    0 讨论(0)
  • 2021-02-19 23:51

    Go use Boto3's wait_until_running method:

    http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Instance.wait_until_running

    0 讨论(0)
  • 2021-02-19 23:59

    Depending on what you are trying to do (and how many servers you plan on starting), instead of polling for the instance start events, you could install on the AMI a simple program/script that runs once when the instance starts and sends out a notification to that effect, i.e. to an AWS SNS Topic.

    The process that needs to know about new servers starting could then subscribe to this SNS topic, and would receive a push notifications each time a server starts.

    Solves the same problem from a different angle; your mileage may vary.

    0 讨论(0)
提交回复
热议问题