question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I know the instance is ready when using AWS cloudformatoin to start EC2 instance

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kyleknapcommented, Jul 26, 2017

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.

0reactions
swetashrecommented, Aug 13, 2020

Thank you for responding. I am closing this issue. Please reopen if you have any concerns.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I know the instance is ready when using AWS ...
I am now using my script(Python) to send request checking the instance status after create the stack using AWS cloudformation.
Read more >
AWS::EC2::Instance - AWS CloudFormation
Indicates whether the instance is associated with a dedicated host. If you want the instance to always restart on the same host on...
Read more >
Steps to launch an EC2 instance using AWS CloudFormation
Follow this step-by-step tutorial to create an EC2 instance with AWS CloudFormation. Learn how to define a template and create a resource ...
Read more >
Provisioning an EC2 Instance with CloudFormation (part 1)
1. Create Basic Amazon EC2 Instance ... First, we'll create a basic EC2 instance with CloudFormation. ... In the template above, we have...
Read more >
A Simple Introduction to AWS CloudFormation Part 1: EC2 ...
To check on the status of the newly launch stack, you can use the AWS CloudFormation console and click on the Events Tab...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found