def lambda_handler()

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


def lambda_handler(event, context):
    try:
        start_time = time.time()
        print('Started process to convert standalone cluster to global cluster')
        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')
            # When triggered as a standalone lambda function, there will be no item in the DDB store. When triggered
            # via convert to local, there will an item inserted in the failover lambda function. To this record add
            # secondary clusters
            if "Item" in get_item(event['global_cluster_id'], event['primary_cluster_arn']):
                update_item(global_cluster_id=event['global_cluster_id'],
                            primary_cluster_arn=event['primary_cluster_arn'],
                            secondary_clusters=event['secondary_clusters'],
                            current_state='CONVERT_TO_GLOBAL_PROCESS_STARTED')
            else:
                put_item(global_cluster_id=event['global_cluster_id'], primary_cluster_arn=event['primary_cluster_arn'],
                         secondary_clusters=event['secondary_clusters'],
                         current_state='CONVERT_TO_GLOBAL_PROCESS_STARTED')

            print('User Input validation complete. Primary cluster ', event['primary_cluster_arn'],
                  ' will be converted to global cluster ', event['global_cluster_id'],
                  ' and the provided secondary clusters will be added to this global cluster')

            add_secondarycluster.convert_regional_to_global(primary_cluster_arn=event['primary_cluster_arn'],
                                                            global_cluster_id=event['global_cluster_id'],
                                                            secondary_clusters=event['secondary_clusters'])

            end_time = time.time()

            print('Completed process to convert standalone primary to global cluster in ',
                  end_time - start_time,
                  ' seconds. Recording state CONVERT_TO_GLOBAL_PROCESS_COMPLETED in DynamoDB table')

            update_item(global_cluster_id=event['global_cluster_id'], primary_cluster_arn=event['primary_cluster_arn'],
                        secondary_clusters=event['secondary_clusters'],
                        current_state='CONVERT_TO_GLOBAL_PROCESS_COMPLETED')
        else:
            print('Duplicate request received for the provided input. '
                  'The process will not perform conversion to global cluster. 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'], primary_cluster_arn=event['primary_cluster_arn'],
                    secondary_clusters=event['secondary_clusters'], current_state='CONVERT_TO_GLOBAL_PROCESS_ERRORED')

        print('ERROR OCCURRED WHILE PROCESSING: ', e)
        print('PROCESSING WILL STOP')
        raise RuntimeError

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