Bootstrap FreeKB - Amazon Web Services (AWS) - Execute Step Function State Machine using the AWS CLI
Amazon Web Services (AWS) - Execute Step Function State Machine using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

Step Functions can be used to submit a request to your one of your API Gateway Routes. For example, let's say you have an API Gateway Route https://abcdefg123.execute-api.us-east-1.amazonaws.com/foo that returns JSON "greeting": "Hello World", like this.

~]$ curl --request GET --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/foo
{"greeting": "Hello World"}

 

A Step Function State Machine can be used to forwards requests onto the API Gateway Route

The aws stepfunctions list-state-machines command can be used to list your Step Function State Machines.

~]$ aws stepfunctions list-state-machines
{
    "stateMachines": [
        {
            "type": "STANDARD",
            "creationDate": 1703644772.497,
            "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
            "name": "myStateMachine"
        }
    ]
}

 

Then the aws stepfunctions start-execution command can be used to execute the Step Function State Machine.

~]$ aws stepfunctions start-execution --state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine
{
    "startDate": 1703730246.195,
    "executionArn": "arn:aws:states:us-east-1:123456789012:execution:myStateMachine:4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf"
}

 

And here is how you can pass in input JSON.

aws stepfunctions start-execution --state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine --input "{\"email\" : \"john.doe@example.com\"}"

 

And then the aws stepfunctions list-executions command can be used to list each executions, as a way to know if the execution successed or failed.

~]$ aws stepfunctions list-executions --state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine
{
    "executions": [
        {
            "status": "SUCCEEDED",
            "startDate": 1703730246.195,
            "name": "4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf",
            "executionArn": "arn:aws:states:us-east-1:123456789012:execution:myStateMachine:4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf",
            "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
            "stopDate": 1703730247.342
        },
        {
            "status": "SUCCEEDED",
            "startDate": 1703729660.813,
            "name": "bfe0c413-27ce-4a3b-9ec1-40e8a4205cfc",
            "executionArn": "arn:aws:states:us-east-1:123456789012:execution:myStateMachine:bfe0c413-27ce-4a3b-9ec1-40e8a4205cfc",
            "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
            "stopDate": 1703729661.849
        },
        {
            "status": "FAILED",
            "startDate": 1703644837.076,
            "name": "d03b2c6b-4030-4afc-8166-0168005c24dd",
            "executionArn": "arn:aws:states:us-east-1:123456789012:execution:myStateMachine:d03b2c6b-4030-4afc-8166-0168005c24dd",
            "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
            "stopDate": 1703644838.419
        }
    ]
}

 

And then the aws stepfunctions describe-execution command can be used to get the output of the request. Notice in this example that the output includes ResponseBody "greeting": "Hello World".

~]$ aws stepfunctions describe-execution --execution-arn arn:aws:states:us-east-1:123456789012:execution:myStateMachine:4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf
{
    "status": "SUCCEEDED",
    "startDate": 1703730246.195,
    "name": "4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf",
    "executionArn": "arn:aws:states:us-east-1:123456789012:execution:myStateMachine:4c7a1b58-03cf-4cbc-9e82-354d3a0cb3cf",
    "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
    "stopDate": 1703730247.342,
    "outputDetails": {
        "included": true
    },
    "output": "{\"Headers\":{\"Apigw-Requestid\":[\"QodbChRwoAMEPKw=\"],\"Connection\":[\"keep-alive\"],\"Content-Length\":[\"99\"],\"Content-Type\":[\"text/plain; charset=utf-8\"],\"Date\":[\"Thu, 28 Dec 2023 02:24:07 GMT\"]},\"ResponseBody\":{\"greeting\":\"Hello World\"},\"StatusCode\":200,\"StatusText\":\"OK\"}",
    "input": "{}",
    "inputDetails": {
        "included": true
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 076a95 in the box below so that we can be sure you are a human.