use-cases/lambda-chaining-with-redshift-data-api/cloud_formation_template/lambda-chaining-with-redshift-data-api.yaml [496:530]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                  elif action == "status_check":
                      # check status of a previously run query
                      res = status_check(client, query_id)
                  elif action == "notify":
                      # notify to end-users by publishing message in Amazon SNS service
                      res = notify(sns_topic_arn, subject, body)
                  else:
                      raise ValueError("Invalid Action: " + action)
              except Exception as e:
                  subject = "Error:" + action + ":" + str(e)
                  body = traceback.format_exc()
                  notify(sns_topic_arn, subject, body)
                  raise
              return {'statusCode': 200, 'body': json.dumps(res)}
          def run_sql(client, sql_text, redshift_database, redshift_user, redshift_cluster_id, with_event=True):
              MAX_WAIT_CYCLES = 20
              attempts = 0
              print("Executing: {}".format(sql_text))
              res = client.execute_statement(Database=redshift_database, DbUser=redshift_user, Sql=sql_text,
                                             ClusterIdentifier=redshift_cluster_id, WithEvent=with_event)
              print(res)
              query_id = res["Id"]
              
              while attempts < MAX_WAIT_CYCLES:
                  attempts += 1
                  time.sleep(1)
                  status = status_check(client, query_id)
                  if status in ("STARTED", "FAILED", "FINISHED"):
                      print("status is: {}".format(status))
                      break
              return query_id
          def status_check(client, query_id):
              desc = client.describe_statement(Id=query_id)
              status = desc["Status"]
              if status == "FAILED":
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



use-cases/lambda-chaining-with-redshift-data-api/scripts/LambdaRedshiftDataApiETL.py [50:86]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            elif action == "status_check":
                # check status of a previously run query
                res = status_check(client, query_id)
            elif action == "notify":
                # notify to end-users by publishing message in Amazon SNS service
                res = notify(sns_topic_arn, subject, body)
            else:
                raise ValueError("Invalid Action: " + action)
        except Exception as e:
            subject = "Error:" + action + ":" + str(e)
            body = traceback.format_exc()
            notify(sns_topic_arn, subject, body)
            raise
        return {'statusCode': 200, 'body': json.dumps(res)}

def run_sql(client, sql_text, redshift_database, redshift_user, redshift_cluster_id, with_event=True):
        MAX_WAIT_CYCLES = 20
        attempts = 0
        print("Executing: {}".format(sql_text))
        res = client.execute_statement(Database=redshift_database, DbUser=redshift_user, Sql=sql_text,
                                       ClusterIdentifier=redshift_cluster_id, WithEvent=with_event)
        print(res)
        query_id = res["Id"]

        while attempts < MAX_WAIT_CYCLES:
            attempts += 1
            time.sleep(1)
            status = status_check(client, query_id)
            if status in ("STARTED", "FAILED", "FINISHED"):
                print("status is: {}".format(status))
                break
        return query_id

def status_check(client, query_id):
        desc = client.describe_statement(Id=query_id)
        status = desc["Status"]
        if status == "FAILED":
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



