Can you write a python script that re-executes failed step-functions workflows? This script will use argparse to read parameters, will then fetch all the failed workflows, retrieve their inputs and names and then re-execute them by using the same input as the original workflow and the same name but adding a "retry" suffix at the end.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Here's a Python script that uses the boto3 library to interact with AWS Step Functions and argparse to handle command-line arguments. This script will re-execute failed workflows by using the same input and adding a "retry" suffix to the workflow name.
First, make sure you have the boto3 library installed:
bash
pip install boto3
Now, create a file called retry_stepfunctions.py and paste the following code into it:
python
import argparse
import boto3
import json
def get_failed_executions(client, state_machine_arn):
max_items = 100
next_token = None
failed_executions = []
while True:
if next_token:
kwargs = {'nextToken': next_token, 'maxItems': max_items}
else:
kwargs = {'stateMachineArn': state_machine_arn, 'maxItems': max_items}
response = client.list_executions(**kwargs)
executions = response['executions']
for execution in executions:
if execution['status'] == 'FAILED':
failed_executions.append(execution)
if not response.get('nextToken'):
break
next_token = response['nextToken']
return failed_executions
def get_execution_input(client, execution_arn):
response = client.describe_execution(executionArn=execution_arn)
return response['input']
def start_execution(client, state_machine_arn, name, input):
client.start_execution(
stateMachineArn=state_machine_arn,
name=name,
input=input
)
def main():
parser = argparse.ArgumentParser(description='Retry failed AWS Step Functions workflows.')
parser.add_argument('--state_machine_arn', type=str, required=True,
help='The ARN of the state machine to retry failed executions for.')
args = parser.parse_args()
client = boto3.client('stepfunctions')
failed_executions = get_failed_executions(client, args.state_machine_arn)
for execution in failed_executions:
execution_name = execution['name']
input = get_execution_input(client, execution['executionArn'])
retry_execution_name = execution_name + '_retry'
start_execution(client, args.state_machine_arn, retry_execution_name, input)
print(f'Retrying execution {execution_name} with name {retry_execution_name}')
if __name__ == '__main__':
main()
Replace <your_state_machine_arn> with the ARN of the state machine you want to retry failed executions for.
Keep in mind that this script assumes you have the necessary AWS credentials configured in your environment. If not, you can configure them using the AWS CLI:
bash
aws configure
This script is a starting point and might need adjustments depending on your specific use case.