{ "cells": [ { "cell_type": "markdown", "source": [ "# State Machine Deployment\n", "\n", "In this section, we demonstrate the best practice using [aws_stepfunciton](https://github.com/MacHu-GWU/aws_stepfunction-project) library to manage the deployment / update / execution and deletion of an AWS State Machine" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "markdown", "source": [ "## Define Workflow, State and StateMachine\n", "\n", "First, let's import required libraries" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 49, "outputs": [], "source": [ "# import the aws_stepfunction top level namespace\n", "import aws_stepfunction as sfn\n", "\n", "# import the boto_session_manager library\n", "from boto_session_manager import BotoSesManager\n", "\n", "# import the pretty printer library\n", "from rich import print" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "then, let's define a simple workflow, and we hard code the input argument in the ``result`` field in the first \"Input Argument Handling\" task. we expect to see this value in execution result." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 50, "outputs": [ { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'StartAt'\u001B[0m: \u001B[32m'Input Argument Handling'\u001B[0m,\n \u001B[32m'States'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'Input Argument Handling'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'Type'\u001B[0m: \u001B[32m'Pass'\u001B[0m, \u001B[32m'Result'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'message'\u001B[0m: \u001B[32m'Hello Alice'\u001B[0m\u001B[1m}\u001B[0m, \u001B[32m'Next'\u001B[0m: \u001B[32m'Task 1'\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'Task 1'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'Type'\u001B[0m: \u001B[32m'Pass'\u001B[0m, \u001B[32m'Next'\u001B[0m: \u001B[32m'Task 2'\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'Task 2'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'Type'\u001B[0m: \u001B[32m'Pass'\u001B[0m, \u001B[32m'End'\u001B[0m: \u001B[3;92mTrue\u001B[0m\u001B[1m}\u001B[0m\n \u001B[1m}\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'StartAt': 'Input Argument Handling',\n    'States': {\n        'Input Argument Handling': {'Type': 'Pass', 'Result': {'message': 'Hello Alice'}, 'Next': 'Task 1'},\n        'Task 1': {'Type': 'Pass', 'Next': 'Task 2'},\n        'Task 2': {'Type': 'Pass', 'End': True}\n    }\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "workflow = sfn.Workflow()\n", "\n", "input_argument_handling = sfn.Pass(\n", " id=\"Input Argument Handling\",\n", " result={\"message\": \"Hello Alice\"}, # <=== input argument\n", ")\n", "\n", "(\n", " workflow.start_from(input_argument_handling)\n", " .next_then(sfn.Pass(id=\"Task 1\"))\n", " .next_then(sfn.Pass(id=\"Task 2\"))\n", " .end()\n", ")\n", "\n", "print(workflow.serialize())" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "A minimal State Machine should include the following required arguments:\n", "\n", "- ``name``: the name of the state machine\n", "- ``workflow``: the ``aws_stepfunction.workflow.Workflow`` object\n", "- ``role_arn``: the IAM role to assume to grant the state machine necessary permission\n", "\n", "By default, a State Machine only support ``async`` execution mode. This is because that a State Machine could take very long to finish. If you want to execute a State Machine in ``sync`` mode, you have to set the State Machine type as ``EXPRESS``. However, it introduces the limitation that the workflow has to be done in 5 minutes." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 51, "outputs": [], "source": [ "state_machine = sfn.StateMachine(\n", " name=\"stepfunction_deployment_example\",\n", " workflow=workflow,\n", " role_arn=\"arn:aws:iam::669508176277:role/sanhe-for-everything-admin\",\n", " tags=dict(Creator=\"alice@example.com\")\n", ")\n", "state_machine.set_type_as_express() # this also work :state_machine = sfn.StateMachine(..., type=\"EXPRESS\", ...)\n", "pass" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "[boto_session_manager](https://pypi.org/project/boto-session-manager/) is a thin wrapper around the official [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html) library. It caches the created AWS Service Client in memory and avoid re-authentication in short period of time." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 52, "outputs": [], "source": [ "bsm = BotoSesManager(\n", " profile_name=\"aws_data_lab_sanhe_us_east_1\",\n", " region_name=\"us-east-1\",\n", ")" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Deploy\n", "\n", "Deploy is just a combination of [create_state_machine](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.create_state_machine) and [update_state_machine](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.update_state_machine). If it doesn't exist, we do ``create``, otherwise we do ``update``." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 53, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "deploy state machine to 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example' ...\n", " already exists, update state machine ...\n", " done, preview at: https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/visual-editor?stateMachineArn=arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example\n" ] }, { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'updateDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m43\u001B[0m, \u001B[1;36m181000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'ResponseMetadata'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'RequestId'\u001B[0m: \u001B[32m'433346d0-90a3-4628-8fe4-4273b6add896'\u001B[0m,\n \u001B[32m'HTTPStatusCode'\u001B[0m: \u001B[1;36m200\u001B[0m,\n \u001B[32m'HTTPHeaders'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'x-amzn-requestid'\u001B[0m: \u001B[32m'433346d0-90a3-4628-8fe4-4273b6add896'\u001B[0m,\n \u001B[32m'date'\u001B[0m: \u001B[32m'Sun, 07 Aug 2022 15:11:43 GMT'\u001B[0m,\n \u001B[32m'content-type'\u001B[0m: \u001B[32m'application/x-amz-json-1.0'\u001B[0m,\n \u001B[32m'content-length'\u001B[0m: \u001B[32m'31'\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'RetryAttempts'\u001B[0m: \u001B[1;36m0\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'_deploy_action'\u001B[0m: \u001B[32m'update'\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'updateDate': datetime.datetime(2022, 8, 7, 11, 11, 43, 181000, tzinfo=tzlocal()),\n    'ResponseMetadata': {\n        'RequestId': '433346d0-90a3-4628-8fe4-4273b6add896',\n        'HTTPStatusCode': 200,\n        'HTTPHeaders': {\n            'x-amzn-requestid': '433346d0-90a3-4628-8fe4-4273b6add896',\n            'date': 'Sun, 07 Aug 2022 15:11:43 GMT',\n            'content-type': 'application/x-amz-json-1.0',\n            'content-length': '31'\n        },\n        'RetryAttempts': 0\n    },\n    '_deploy_action': 'update'\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "result = state_machine.deploy(bsm)\n", "print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Execution\n", "\n", "You can execute a State Machine from python code. The ``output`` field in the API response is the final State Machine output." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 54, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "execute state machine 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example'\n", " preview at: https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/executions/details/arn:aws:states:us-east-1:669508176277:execution:stepfunction_deployment_example:ddea193b-2c78-4fc9-9c1b-acade9d5a699\n" ] }, { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'executionArn'\u001B[0m: \n\u001B[32m'arn:aws:states:us-east-1:669508176277:express:stepfunction_deployment_example:c2794158-4597-47e7-b6d3-d86ba6c21da6\u001B[0m\n\u001B[32m:ddea193b-2c78-4fc9-9c1b-acade9d5a699'\u001B[0m,\n \u001B[32m'stateMachineArn'\u001B[0m: \u001B[32m'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example'\u001B[0m,\n \u001B[32m'name'\u001B[0m: \u001B[32m'c2794158-4597-47e7-b6d3-d86ba6c21da6'\u001B[0m,\n \u001B[32m'startDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m44\u001B[0m, \u001B[1;36m958000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'stopDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m44\u001B[0m, \u001B[1;36m962000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'status'\u001B[0m: \u001B[32m'SUCCEEDED'\u001B[0m,\n \u001B[32m'input'\u001B[0m: \u001B[32m'\u001B[0m\u001B[32m{\u001B[0m\u001B[32m}\u001B[0m\u001B[32m'\u001B[0m,\n \u001B[32m'inputDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'included'\u001B[0m: \u001B[3;92mTrue\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'output'\u001B[0m: \u001B[32m'\u001B[0m\u001B[32m{\u001B[0m\u001B[32m\"message\":\"Hello Alice\"\u001B[0m\u001B[32m}\u001B[0m\u001B[32m'\u001B[0m,\n \u001B[32m'outputDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'included'\u001B[0m: \u001B[3;92mTrue\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'billingDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'billedMemoryUsedInMB'\u001B[0m: \u001B[1;36m64\u001B[0m, \u001B[32m'billedDurationInMilliseconds'\u001B[0m: \u001B[1;36m100\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'ResponseMetadata'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'RequestId'\u001B[0m: \u001B[32m'df85be9b-8ada-4f55-98b2-60d50f2b4d68'\u001B[0m,\n \u001B[32m'HTTPStatusCode'\u001B[0m: \u001B[1;36m200\u001B[0m,\n \u001B[32m'HTTPHeaders'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'x-amzn-requestid'\u001B[0m: \u001B[32m'df85be9b-8ada-4f55-98b2-60d50f2b4d68'\u001B[0m,\n \u001B[32m'date'\u001B[0m: \u001B[32m'Sun, 07 Aug 2022 15:11:44 GMT'\u001B[0m,\n \u001B[32m'content-type'\u001B[0m: \u001B[32m'application/x-amz-json-1.0'\u001B[0m,\n \u001B[32m'content-length'\u001B[0m: \u001B[32m'752'\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'RetryAttempts'\u001B[0m: \u001B[1;36m0\u001B[0m\n \u001B[1m}\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'executionArn': \n'arn:aws:states:us-east-1:669508176277:express:stepfunction_deployment_example:c2794158-4597-47e7-b6d3-d86ba6c21da6\n:ddea193b-2c78-4fc9-9c1b-acade9d5a699',\n    'stateMachineArn': 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example',\n    'name': 'c2794158-4597-47e7-b6d3-d86ba6c21da6',\n    'startDate': datetime.datetime(2022, 8, 7, 11, 11, 44, 958000, tzinfo=tzlocal()),\n    'stopDate': datetime.datetime(2022, 8, 7, 11, 11, 44, 962000, tzinfo=tzlocal()),\n    'status': 'SUCCEEDED',\n    'input': '{}',\n    'inputDetails': {'included': True},\n    'output': '{\"message\":\"Hello Alice\"}',\n    'outputDetails': {'included': True},\n    'billingDetails': {'billedMemoryUsedInMB': 64, 'billedDurationInMilliseconds': 100},\n    'ResponseMetadata': {\n        'RequestId': 'df85be9b-8ada-4f55-98b2-60d50f2b4d68',\n        'HTTPStatusCode': 200,\n        'HTTPHeaders': {\n            'x-amzn-requestid': 'df85be9b-8ada-4f55-98b2-60d50f2b4d68',\n            'date': 'Sun, 07 Aug 2022 15:11:44 GMT',\n            'content-type': 'application/x-amz-json-1.0',\n            'content-length': '752'\n        },\n        'RetryAttempts': 0\n    }\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "result = state_machine.execute(bsm, sync=True) # explicitly use sync mode\n", "print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Deploy a New Version and Execute Again\n", "\n", "We changed the initial input from ``{\"message\": \"Hello Alice\"}`` to ``{\"message\": \"Hello Bob\"}``, and we applied the updates." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 55, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "deploy state machine to 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example' ...\n", " already exists, update state machine ...\n", " done, preview at: https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/visual-editor?stateMachineArn=arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example\n" ] }, { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'updateDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m46\u001B[0m, \u001B[1;36m64000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'ResponseMetadata'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'RequestId'\u001B[0m: \u001B[32m'0cff91cc-1688-4b3e-a573-22bdaed2b38f'\u001B[0m,\n \u001B[32m'HTTPStatusCode'\u001B[0m: \u001B[1;36m200\u001B[0m,\n \u001B[32m'HTTPHeaders'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'x-amzn-requestid'\u001B[0m: \u001B[32m'0cff91cc-1688-4b3e-a573-22bdaed2b38f'\u001B[0m,\n \u001B[32m'date'\u001B[0m: \u001B[32m'Sun, 07 Aug 2022 15:11:46 GMT'\u001B[0m,\n \u001B[32m'content-type'\u001B[0m: \u001B[32m'application/x-amz-json-1.0'\u001B[0m,\n \u001B[32m'content-length'\u001B[0m: \u001B[32m'31'\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'RetryAttempts'\u001B[0m: \u001B[1;36m0\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'_deploy_action'\u001B[0m: \u001B[32m'update'\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'updateDate': datetime.datetime(2022, 8, 7, 11, 11, 46, 64000, tzinfo=tzlocal()),\n    'ResponseMetadata': {\n        'RequestId': '0cff91cc-1688-4b3e-a573-22bdaed2b38f',\n        'HTTPStatusCode': 200,\n        'HTTPHeaders': {\n            'x-amzn-requestid': '0cff91cc-1688-4b3e-a573-22bdaed2b38f',\n            'date': 'Sun, 07 Aug 2022 15:11:46 GMT',\n            'content-type': 'application/x-amz-json-1.0',\n            'content-length': '31'\n        },\n        'RetryAttempts': 0\n    },\n    '_deploy_action': 'update'\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "input_argument_handling.result = {\"message\": \"Hello Bob\"}\n", "result = state_machine.deploy(bsm)\n", "print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "You can see that the ``output`` field in execution result is changed." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 56, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "execute state machine 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example'\n", " preview at: https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/executions/details/arn:aws:states:us-east-1:669508176277:execution:stepfunction_deployment_example:02a8a9a0-40b8-485f-8720-da1d86c063bb\n" ] }, { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'executionArn'\u001B[0m: \n\u001B[32m'arn:aws:states:us-east-1:669508176277:express:stepfunction_deployment_example:bb47dd82-6e97-4dfa-a1fc-1eb5843f5d7b\u001B[0m\n\u001B[32m:02a8a9a0-40b8-485f-8720-da1d86c063bb'\u001B[0m,\n \u001B[32m'stateMachineArn'\u001B[0m: \u001B[32m'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example'\u001B[0m,\n \u001B[32m'name'\u001B[0m: \u001B[32m'bb47dd82-6e97-4dfa-a1fc-1eb5843f5d7b'\u001B[0m,\n \u001B[32m'startDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m47\u001B[0m, \u001B[1;36m462000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'stopDate'\u001B[0m: \u001B[1;35mdatetime.datetime\u001B[0m\u001B[1m(\u001B[0m\u001B[1;36m2022\u001B[0m, \u001B[1;36m8\u001B[0m, \u001B[1;36m7\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m11\u001B[0m, \u001B[1;36m47\u001B[0m, \u001B[1;36m466000\u001B[0m, \u001B[33mtzinfo\u001B[0m=\u001B[1;35mtzlocal\u001B[0m\u001B[1m(\u001B[0m\u001B[1m)\u001B[0m\u001B[1m)\u001B[0m,\n \u001B[32m'status'\u001B[0m: \u001B[32m'SUCCEEDED'\u001B[0m,\n \u001B[32m'input'\u001B[0m: \u001B[32m'\u001B[0m\u001B[32m{\u001B[0m\u001B[32m}\u001B[0m\u001B[32m'\u001B[0m,\n \u001B[32m'inputDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'included'\u001B[0m: \u001B[3;92mTrue\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'output'\u001B[0m: \u001B[32m'\u001B[0m\u001B[32m{\u001B[0m\u001B[32m\"message\":\"Hello Bob\"\u001B[0m\u001B[32m}\u001B[0m\u001B[32m'\u001B[0m,\n \u001B[32m'outputDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'included'\u001B[0m: \u001B[3;92mTrue\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'billingDetails'\u001B[0m: \u001B[1m{\u001B[0m\u001B[32m'billedMemoryUsedInMB'\u001B[0m: \u001B[1;36m64\u001B[0m, \u001B[32m'billedDurationInMilliseconds'\u001B[0m: \u001B[1;36m100\u001B[0m\u001B[1m}\u001B[0m,\n \u001B[32m'ResponseMetadata'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'RequestId'\u001B[0m: \u001B[32m'46d8ec41-9dcf-41cc-93ff-f440bb0dc4f9'\u001B[0m,\n \u001B[32m'HTTPStatusCode'\u001B[0m: \u001B[1;36m200\u001B[0m,\n \u001B[32m'HTTPHeaders'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'x-amzn-requestid'\u001B[0m: \u001B[32m'46d8ec41-9dcf-41cc-93ff-f440bb0dc4f9'\u001B[0m,\n \u001B[32m'date'\u001B[0m: \u001B[32m'Sun, 07 Aug 2022 15:11:47 GMT'\u001B[0m,\n \u001B[32m'content-type'\u001B[0m: \u001B[32m'application/x-amz-json-1.0'\u001B[0m,\n \u001B[32m'content-length'\u001B[0m: \u001B[32m'750'\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'RetryAttempts'\u001B[0m: \u001B[1;36m0\u001B[0m\n \u001B[1m}\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'executionArn': \n'arn:aws:states:us-east-1:669508176277:express:stepfunction_deployment_example:bb47dd82-6e97-4dfa-a1fc-1eb5843f5d7b\n:02a8a9a0-40b8-485f-8720-da1d86c063bb',\n    'stateMachineArn': 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example',\n    'name': 'bb47dd82-6e97-4dfa-a1fc-1eb5843f5d7b',\n    'startDate': datetime.datetime(2022, 8, 7, 11, 11, 47, 462000, tzinfo=tzlocal()),\n    'stopDate': datetime.datetime(2022, 8, 7, 11, 11, 47, 466000, tzinfo=tzlocal()),\n    'status': 'SUCCEEDED',\n    'input': '{}',\n    'inputDetails': {'included': True},\n    'output': '{\"message\":\"Hello Bob\"}',\n    'outputDetails': {'included': True},\n    'billingDetails': {'billedMemoryUsedInMB': 64, 'billedDurationInMilliseconds': 100},\n    'ResponseMetadata': {\n        'RequestId': '46d8ec41-9dcf-41cc-93ff-f440bb0dc4f9',\n        'HTTPStatusCode': 200,\n        'HTTPHeaders': {\n            'x-amzn-requestid': '46d8ec41-9dcf-41cc-93ff-f440bb0dc4f9',\n            'date': 'Sun, 07 Aug 2022 15:11:47 GMT',\n            'content-type': 'application/x-amz-json-1.0',\n            'content-length': '750'\n        },\n        'RetryAttempts': 0\n    }\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "result = state_machine.execute(bsm, sync=True) # explicitly use sync mode\n", "print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Delete the State Machine" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": 57, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "delete state machine 'arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example'\n", " done, exam at: https://us-east-1.console.aws.amazon.com/states/home?region=us-east-1#/statemachines/view/arn:aws:states:us-east-1:669508176277:stateMachine:stepfunction_deployment_example\n" ] }, { "data": { "text/plain": "\u001B[1m{\u001B[0m\n \u001B[32m'ResponseMetadata'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'RequestId'\u001B[0m: \u001B[32m'c1db49bc-de2d-4404-9485-58a93b023f5a'\u001B[0m,\n \u001B[32m'HTTPStatusCode'\u001B[0m: \u001B[1;36m200\u001B[0m,\n \u001B[32m'HTTPHeaders'\u001B[0m: \u001B[1m{\u001B[0m\n \u001B[32m'x-amzn-requestid'\u001B[0m: \u001B[32m'c1db49bc-de2d-4404-9485-58a93b023f5a'\u001B[0m,\n \u001B[32m'date'\u001B[0m: \u001B[32m'Sun, 07 Aug 2022 15:11:51 GMT'\u001B[0m,\n \u001B[32m'content-type'\u001B[0m: \u001B[32m'application/x-amz-json-1.0'\u001B[0m,\n \u001B[32m'content-length'\u001B[0m: \u001B[32m'2'\u001B[0m\n \u001B[1m}\u001B[0m,\n \u001B[32m'RetryAttempts'\u001B[0m: \u001B[1;36m0\u001B[0m\n \u001B[1m}\u001B[0m\n\u001B[1m}\u001B[0m\n", "text/html": "
{\n    'ResponseMetadata': {\n        'RequestId': 'c1db49bc-de2d-4404-9485-58a93b023f5a',\n        'HTTPStatusCode': 200,\n        'HTTPHeaders': {\n            'x-amzn-requestid': 'c1db49bc-de2d-4404-9485-58a93b023f5a',\n            'date': 'Sun, 07 Aug 2022 15:11:51 GMT',\n            'content-type': 'application/x-amz-json-1.0',\n            'content-length': '2'\n        },\n        'RetryAttempts': 0\n    }\n}\n
\n" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "result = state_machine.delete(bsm)\n", "print(result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Final Thought\n", "\n", "**Immutable Deployment and Semantic Versioning**\n", "\n", "Since all State Machine / Workflow / State are just Python object, you could use semantic versioning in your python code to track deployment history. And you can use Git to roll back your deployment to any historical version." ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }