0

Creating an instance in lambda and assigning an Elastic IP. Create instance part works, code section below is meant to wait before assigning the elastic IP. (1) What state does the instance have to be in to assign? (assuming "running") (2) Below logic never proceeds to after the loop, although I can verify the instance goes into "running" state. I verified the instance ID is good and there's only 1 instance in this case.

    print ('waiting')
    newresp = ec2_client.describe_instance_status(InstanceIds=newins_list,IncludeAllInstances=True)
    while (newresp['InstanceStatuses'][0]['InstanceState']['Name'] != 'running'):
        newresp = ec2_client.describe_instance_status(InstanceIds=newins_list,IncludeAllInstances=True)
    
    print ('New Instance Running')
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
can.do
  • 475
  • 4
  • 14
  • Why do you think you need to wait before assigning it? – Paolo Aug 04 '22 at 20:47
  • Otherwise assignment errors on "state" of instance -- I placed the wait in front of the assignment. It never reaches past 'New Instance Running'. – can.do Aug 04 '22 at 21:06
  • I found out why and resolved it, will post an answer soon... – can.do Aug 04 '22 at 23:09
  • If you had read the CloudWatch Logs for your Lambda function invocation, you would have seen that it timed out after 3 seconds. – jarmod Aug 04 '22 at 23:38
  • No, the logs didn't show it timed out. I did read the logs. – can.do Aug 05 '22 at 03:30
  • Not sure why you make a comment like "if you read the logs"... there's nothing specific about the fact that it timed out, logs just show end of function. – can.do Aug 05 '22 at 03:37
  • Following the REPORT line in CloudWatch Logs, you should see a log that indicates something like "Task timed out after 3.00 seconds". More [here](https://lumigo.io/blog/debugging-aws-lambda-timeouts/). – jarmod Aug 05 '22 at 16:11
  • I figured out it out in the end, if you had made your point before, that would have been nice, and there would be a point to continue on commenting... Seems you're trying hard to point out that I missed the timeout message; the timeout message is not apparent: here it is, it gets chopped off at the end: "2022-08-05T23:29:17.166Z da9d2d22-9cdf-48a1-b83e-2d9f5529fd5d Task..." That's from the log, it's very easy to miss it. None of any of this means I wasn't working with the logs, for you to start with "if you had read the logs" . Try to keep your comments neutral and informative. – can.do Aug 06 '22 at 23:48

1 Answers1

0

The "timeout" setting is important in lambda. This affects loops and timers. If you have for/while loops, sleep, or other functions that take time, lambda should be configured.

When I created the function the default setting was 3s execution time. The statement after the loop was never reached because the function timed out.

You can set the timeout from the function "Configuration" tab under General Configuration.

You have to remember though, pricing is based on the amount of time your code runs. I set it to 30s, and checked status at 5s intervals in the loop. Here's the final code, starting from the loop section, including the associate command:

        ....
        while (newresp['InstanceStatuses'][0]['InstanceState']['Name'] != 'running'):
            time.sleep(5)
            newresp = ec2_client.describe_instance_status(InstanceIds=newins_list,IncludeAllInstances=True)
        print ('Associating Elastic IP')
        try:
            ec2_client.associate_address(AllocationId='eipalloc-0805514a57680aaf8',InstanceId=newins,AllowReassociation=True)
            print ('New Instance Running')
        except ClientError as e:
            print(e)
can.do
  • 475
  • 4
  • 14
  • Rather than loop, describing the instance until it's running, you can use an [EC2 InstanceRunning waiter](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Waiter.InstanceRunning). That said, unless you need to wait, don't wait. – jarmod Aug 04 '22 at 23:38