def lambda_handler()

in global-clusters-automation/failover_and_delete_lambda_function.py [0:0]


def lambda_handler(event, context):
    try:
        start_time = time.time()
        print('Started process to failover secondary cluster to standalone primary')
        validate_input(event)

        print('User Input validation completed. Validating if the provided input was processed earlier to ensure '
              'idempotency')

        # Idempotency check to avoid duplicate processing
        if not is_request_processed(event):
            print('Idempotency check passed. The provided input will be recorded in DynamoDB table')

            put_item(global_cluster_id=event['global_cluster_id'],
                     secondary_cluster_arn=event['secondary_cluster_arn'],
                     primary_cluster_cname=event['primary_cluster_cname'],
                     hosted_zone_id=event['hosted_zone_id'],
                     current_state="FAILOVER_PROCESS_STARTED"
                     )
            print('User Input validation complete. Secondary cluster ', event['secondary_cluster_arn'],
                  ' will be removed from global cluster ', event['global_cluster_id'], ' and the cname ',
                  event['primary_cluster_cname'], ' will be updated with promoted primary cluster endpoint')

            failover_and_delete_global_cluster.failover(global_cluster_id=event['global_cluster_id'],
                                                        secondary_cluster_arn=event['secondary_cluster_arn'],
                                                        primary_cluster_cname=event['primary_cluster_cname'],
                                                        hosted_zone_id=event['hosted_zone_id'],
                                                        is_delete_global_cluster=event['is_delete_global_cluster'])
            end_time = time.time()

            print('Completed process to failover secondary cluster to standalone primary in ',
                  end_time - start_time,
                  ' seconds. Recording state FAILOVER_PROCESS_COMPLETED in DynamoDB table')

            update_item(global_cluster_id=event['global_cluster_id'],
                        secondary_cluster_arn=event['secondary_cluster_arn'],
                        current_state="FAILOVER_PROCESS_COMPLETED")

        else:
            print('Duplicate request received for the provided input. The process will not perform failover. '
                  'Returning status code 200...')
            return {
                'statusCode': 200,
                'body': json.dumps('Duplicate request received for the provided input. Skipping processing...')
            }

    except RuntimeError as e:
        update_item(global_cluster_id=event['global_cluster_id'],
                    secondary_cluster_arn=event['secondary_cluster_arn'],
                    current_state="FAILOVER_PROCESS_ERRORED")
        print('ERROR OCCURRED WHILE PROCESSING: ', e)
        print('PROCESSING WILL STOP')
        raise RuntimeError

    return {
        'statusCode': 200,
        'body': json.dumps('Successfully promoted cluster')
    }