"""Connects to AWS SFN.""" import json import uuid import boto3 from botocore.config import Config config = Config( retries={ 'max_attempts': 6, 'mode': 'standard' } ) def get_full_execution_count_by_arn(sfn_arn, status='RUNNING'): """Get the number of executions with a certain status by sfn name. Args: sfn_arn (str): The AWS ARN of the target SFN to count executions status (str): A status by which to filter results Returns: int: The count executions for the given SFN with given status """ sfn_client = boto3.client('stepfunctions', config=config) current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50 ) next_token = current_executions.get('nextToken') current_sfn_count = len(current_executions['executions']) while next_token: current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50, nextToken=next_token ) current_sfn_count = current_sfn_count + len( current_executions['executions']) next_token = current_executions.get('nextToken') return current_sfn_count def start_execution_by_arn(sfn_arn, upc, grps_ingestion_id, prod_no): """Start grps ingestion state machine execution.""" sfn_client = boto3.client('stepfunctions', config=config) input_data = { 'detail': { 'requestParameters': { 'upc': upc, 'grps_ingestion_id': grps_ingestion_id, 'prod_no': prod_no } } } name = f'{upc}_{str(uuid.uuid4())}' response = sfn_client.start_execution( stateMachineArn=sfn_arn, name=name, input=json.dumps(input_data) ) return response, name