nodes: 0, module; 1, function_definition; 2, function_name:is_first_instance_aws; 3, parameters; 4, block; 5, expression_statement; 6, try_statement; 7, try_statement; 8, try_statement; 9, return_statement; 10, comment:"""
      Returns True if the current instance is the first instance in the ASG group,
      sorted by instance_id.
      """; 11, comment:# get instance id and aws region; 12, block; 13, except_clause; 14, comment:# get instance's autoscaling group; 15, block; 16, except_clause; 17, except_clause; 18, comment:# list in-service instances in autoscaling group; 19, comment:# instances being launched or terminated should not be considered; 20, block; 21, except_clause; 22, call; 23, expression_statement; 24, expression_statement; 25, expression_statement; 26, as_pattern; 27, block; 28, expression_statement; 29, expression_statement; 30, assert_statement; 31, expression_statement; 32, as_pattern; 33, block; 34, identifier:AssertionError; 35, block; 36, expression_statement; 37, assert_statement; 38, expression_statement; 39, as_pattern; 40, block; 41, identifier:bool; 42, argument_list; 43, assignment; 44, assignment; 45, assignment; 46, tuple; 47, as_pattern_target; 48, raise_statement; 49, assignment; 50, assignment; 51, comparison_operator:len(response['AutoScalingInstances']) == 1; 52, assignment; 53, identifier:ClientError; 54, as_pattern_target; 55, raise_statement; 56, raise_statement; 57, assignment; 58, comparison_operator:len(response['AutoScalingGroups']) == 1; 59, assignment; 60, tuple; 61, as_pattern_target; 62, raise_statement; 63, boolean_operator; 64, identifier:instance_details; 65, call; 66, identifier:instance_id; 67, subscript; 68, identifier:instance_region; 69, subscript; 70, attribute; 71, identifier:ValueError; 72, identifier:KeyError; 73, identifier:e; 74, call; 75, identifier:autoscaling_client; 76, call; 77, identifier:response; 78, call; 79, call; 80, integer:1; 81, identifier:autoscaling_group; 82, subscript; 83, identifier:e; 84, call; 85, call; 86, identifier:response; 87, call; 88, call; 89, integer:1; 90, identifier:autoscaling_group_instance_ids; 91, call; 92, identifier:ClientError; 93, identifier:AssertionError; 94, identifier:e; 95, call; 96, identifier:autoscaling_group_instance_ids; 97, comparison_operator:autoscaling_group_instance_ids[0] == instance_id; 98, attribute; 99, argument_list; 100, identifier:instance_details; 101, string; 102, identifier:instance_details; 103, string; 104, identifier:requests; 105, identifier:RequestException; 106, identifier:StackInterrogationException; 107, argument_list; 108, attribute; 109, argument_list; 110, attribute; 111, argument_list; 112, identifier:len; 113, argument_list; 114, subscript; 115, string; 116, identifier:StackInterrogationException; 117, argument_list; 118, identifier:InstanceNotInAsgException; 119, argument_list; 120, attribute; 121, argument_list; 122, identifier:len; 123, argument_list; 124, identifier:sorted; 125, generator_expression; 126, identifier:StackInterrogationException; 127, argument_list; 128, subscript; 129, identifier:instance_id; 130, call; 131, identifier:json; 132, string_content:instanceId; 133, string_content:region; 134, identifier:e; 135, identifier:boto3; 136, identifier:client; 137, string; 138, keyword_argument; 139, identifier:autoscaling_client; 140, identifier:describe_auto_scaling_instances; 141, keyword_argument; 142, subscript; 143, subscript; 144, integer:0; 145, string_content:AutoScalingGroupName; 146, identifier:e; 147, identifier:autoscaling_client; 148, identifier:describe_auto_scaling_groups; 149, keyword_argument; 150, subscript; 151, subscript; 152, for_in_clause; 153, if_clause; 154, identifier:e; 155, identifier:autoscaling_group_instance_ids; 156, integer:0; 157, attribute; 158, argument_list; 159, string_content:autoscaling; 160, identifier:region_name; 161, identifier:instance_region; 162, identifier:InstanceIds; 163, list; 164, identifier:response; 165, string; 166, identifier:response; 167, string; 168, identifier:AutoScalingGroupNames; 169, list; 170, identifier:response; 171, string; 172, identifier:instance; 173, string; 174, identifier:instance; 175, subscript; 176, comparison_operator:instance['LifecycleState'] == 'InService'; 177, identifier:requests; 178, identifier:get; 179, string; 180, keyword_argument; 181, identifier:instance_id; 182, string_content:AutoScalingInstances; 183, string_content:AutoScalingInstances; 184, identifier:autoscaling_group; 185, string_content:AutoScalingGroups; 186, string_content:InstanceId; 187, subscript; 188, string; 189, subscript; 190, string; 191, string_content:http://169.254.169.254/latest/dynamic/instance-identity/document; 192, identifier:timeout; 193, integer:5; 194, subscript; 195, integer:0; 196, string_content:Instances; 197, identifier:instance; 198, string; 199, string_content:InService; 200, identifier:response; 201, string; 202, string_content:LifecycleState; 203, string_content:AutoScalingGroups
code: def is_first_instance_aws():
      """
      Returns True if the current instance is the first instance in the ASG group,
      sorted by instance_id.
      """
      try:
          # get instance id and aws region
          instance_details = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document',
                                          timeout=5).json()
          instance_id = instance_details['instanceId']
          instance_region = instance_details['region']
      except (requests.RequestException, ValueError, KeyError) as e:
          raise StackInterrogationException(e)
  
      try:
          # get instance's autoscaling group
          autoscaling_client = boto3.client('autoscaling', region_name=instance_region)
          response = autoscaling_client.describe_auto_scaling_instances(InstanceIds=[instance_id])
          assert len(response['AutoScalingInstances']) == 1
          autoscaling_group = response['AutoScalingInstances'][0]['AutoScalingGroupName']
      except ClientError as e:
          raise StackInterrogationException(e)
      except AssertionError:
          raise InstanceNotInAsgException()
  
      try:
          # list in-service instances in autoscaling group
          # instances being launched or terminated should not be considered
          response = autoscaling_client.describe_auto_scaling_groups(AutoScalingGroupNames=[autoscaling_group])
          assert len(response['AutoScalingGroups']) == 1
          autoscaling_group_instance_ids = sorted(
              instance['InstanceId']
              for instance in response['AutoScalingGroups'][0]['Instances']
              if instance['LifecycleState'] == 'InService'
          )
      except (ClientError, AssertionError) as e:
          raise StackInterrogationException(e)
  
      return bool(autoscaling_group_instance_ids and autoscaling_group_instance_ids[0] == instance_id)