In this article, we will see how a user can stop a workflow present in an AWS account.
Example
Problem Statement: Use boto3 library in Python to stop a workflow.
Approach/Algorithm to solve this problem
Step 1: Import boto3 and botocore exceptions to handle exceptions.
Step 2: workflow_name and run_id are the required parameters in this function.
Step 3: Create an AWS session using boto3 lib. Make sure region_name is mentioned in the default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.
Step 4: Create an AWS client for glue.
Step 5: Now use the stop_workflow_run function and pass the parameter workflow_name as Name and run_id as RunId.
Step 6: It returns the response metadata and stops the workflow.
Step 7: Handle the generic exception if something went wrong while stopping a workflow.
Example Code
The following code stops a running workflow −
import boto3 from botocore.exceptions import ClientError def stop_a_workflow(workflow_name, run_id) session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.stop_workflow_run(Name=workflow_name, RunId= run_id) return response except ClientError as e: raise Exception("boto3 client error in stop_a_workflow: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in stop_a_workflow: " + e.__str__()) print(stop_a_workflow("test-daily"))
Output
{'RunId': 'wr_64e880240692fddd5e1b19aed587f856bc20a96f54bc', 'ResponseMetadata': {'RequestId': '782e953b-8ee3-4876-9b2c-cd35e147b513', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Mar 2021 08:11:02 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '79', 'connection': 'keep-alive', 'x-amzn-requestid': '782e953b-********************************13'}, 'RetryAttempts': 0}}