How do I know the instance is ready when using AWS cloudformatoin to start EC2 instance
See original GitHub issueI am now using my script(Python) to send requests checking the instance status after creating the stack using AWS cloudformation. It’s a polling way to describe_instance_status every 10 seconds until a successful state is reached.
def wait_ec2_complate(client, instance_id):
"""
this method is to make client keep sending request until ec2 instance building complate or fail
:param client:
:param instance_name:
:return:
"""
while True:
time.sleep(10)
rsp = client.describe_instance_status(
InstanceIds=[str(instance_id)],
IncludeAllInstances=True
)
# double check 2/2 status
instance_status = rsp['InstanceStatuses'][0]['InstanceStatus']['Status']
system_status = rsp['InstanceStatuses'][0]['SystemStatus']['Status']
if str(instance_status) == 'ok' and str(system_status) == 'ok':
status = True
break
if str(instance_status) == 'impaired' or str(instance_status) == 'insufficient-data' or \
str(instance_status) == 'not-applicable' or str(system_status) == 'failed' or \
str(system_status) == 'insufficient-data':
status = False
print 'Instance status is ' + str(instance_status)
print 'System status is ' + str(system_status)
break
if time >= POLL_TIMES:
break
return status
But it seems not a good solution. I try using cfn-signal
to check the instances’ status, only to find out that when received signal the cloudformation stack is successfully created, but the EC2 instance is still being checked(System check and Instance check).
Then, I added WaitCondition
in my template to get callback signal from cfn-signal
.
Sadly, when the cloudformation stack successfully created, I switched to the EC2 instances manager to check instance’s status. But it was still in the initializing progress. That’s exactly what I worry about. The WaitCondition
told me that the instance was ready to use, but the EC2 instances manager showed that the instance was still being checked.
How do I know the instance is ready when using AWS cloudformatoin to start EC2 instance.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
I would recommend using the waiter interfaces instead of using your own solution. So you have a couple of waiter options available to you. If you want to wait for the CloudFormation stack to be created or updated, I would recommend using the StackCreateComplete or StackUpdateComplete waiters.
If you are concerned with actually being able to start using the instance (i.e. it is in the ok state), you can use the InstanceStatusOk waiter to wait for the instances to reach an ok state.
This is hopefully better than having to maintain your own custom solution. Let us know if that helps.
Thank you for responding. I am closing this issue. Please reopen if you have any concerns.