1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「AWS Hands-on for Beginners AWS Step Functions 入門 - ビジュアルツールを使ってローコードにワークフローを作成する」をAWS CLIでやってみる

Posted at

上記、「AWS Hands-on for Beginners AWS Step Functions 入門 - ビジュアルツールを使ってローコードにワークフローを作成する」 をAWS CLIでやってみる

image.png
ハンズオンから引用

02 ステートマシーンの作成 + 「アクション」を使ってみる

変数

コマンド
# ステートマシン名
STATE_MACHINE_NAME="H4BStateMachine" \
&& echo ${STATE_MACHINE_NAME}

# IAMポリシー名
IAM_POLICY_NAME="XRayAccessPolicy-${STATE_MACHINE_NAME}" \
&& echo ${IAM_POLICY_NAME}

# IAMロール名
IAM_ROLE_NAME="StepFunctions-${STATE_MACHINE_NAME}-role" \
&& echo ${IAM_ROLE_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシン名
[cloudshell-user@ip-10-130-38-2 ~]$ STATE_MACHINE_NAME="H4BStateMachine" \
> && echo ${STATE_MACHINE_NAME}
H4BStateMachine
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMポリシー名
[cloudshell-user@ip-10-130-38-2 ~]$ IAM_POLICY_NAME="XRayAccessPolicy-${STATE_MACHINE_NAME}" \
> && echo ${IAM_POLICY_NAME}
XRayAccessPolicy-H4BStateMachine
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロール名
[cloudshell-user@ip-10-130-38-2 ~]$ IAM_ROLE_NAME="StepFunctions-${STATE_MACHINE_NAME}-role" \
> && echo ${IAM_ROLE_NAME}
StepFunctions-H4BStateMachine-role

作成

IAMポリシーの作成

コマンド
# ポリシードキュメントの作成
POLICY_DOCUMENT_JSON=$(cat << EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "xray:PutTraceSegments",
                "xray:PutTelemetryRecords",
                "xray:GetSamplingRules",
                "xray:GetSamplingTargets"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
EOF
) \
&& echo ${POLICY_DOCUMENT_JSON}

# JSONフォーマットの確認
echo ${POLICY_DOCUMENT_JSON} | python -m json.tool

# ポリシーの作成
aws iam create-policy \
    --policy-name ${IAM_POLICY_NAME} \
    --policy-document "${POLICY_DOCUMENT_JSON}" \
    --path /service-role/

# ARN取得
IAM_POLICY_ARN=$(
    aws iam list-policies \
        --query "Policies[?PolicyName=='${IAM_POLICY_NAME}'].Arn" \
        --output text
) \
&& echo ${IAM_POLICY_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ポリシードキュメントの作成
[cloudshell-user@ip-10-130-38-2 ~]$ POLICY_DOCUMENT_JSON=$(cat << EOF
> {
>     "Version": "2012-10-17",
>     "Statement": [
>         {
>             "Effect": "Allow",
>             "Action": [
>                 "xray:PutTraceSegments",
>                 "xray:PutTelemetryRecords",
>                 "xray:GetSamplingRules",
>                 "xray:GetSamplingTargets"
>             ],
>             "Resource": [
>                 "*"
>             ]
>         }
>     ]
> }
> EOF
> ) \
> && echo ${POLICY_DOCUMENT_JSON}
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "xray:PutTraceSegments", "xray:PutTelemetryRecords", "xray:GetSamplingRules", "xray:GetSamplingTargets" ], "Resource": [ "*" ] } ] }
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # JSONフォーマットの確認
[cloudshell-user@ip-10-130-38-2 ~]$ echo ${POLICY_DOCUMENT_JSON} | python -m json.tool
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "xray:PutTraceSegments",
                "xray:PutTelemetryRecords",
                "xray:GetSamplingRules",
                "xray:GetSamplingTargets"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # ポリシーの作成
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam create-policy \
>     --policy-name ${IAM_POLICY_NAME} \
>     --policy-document "${POLICY_DOCUMENT_JSON}" \
>     --path /service-role/
{
    "Policy": {
        "PolicyName": "XRayAccessPolicy-H4BStateMachine",
        "PolicyId": "ANPAWFKRCMKOVS4YY2QSC",
        "Arn": "arn:aws:iam::999999999999:policy/service-role/XRayAccessPolicy-H4BStateMachine",
        "Path": "/service-role/",
        "DefaultVersionId": "v1",
        "AttachmentCount": 0,
        "PermissionsBoundaryUsageCount": 0,
        "IsAttachable": true,
        "CreateDate": "2024-10-05T02:54:36+00:00",
        "UpdateDate": "2024-10-05T02:54:36+00:00"
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # ARN取得
[cloudshell-user@ip-10-130-38-2 ~]$ IAM_POLICY_ARN=$(
>     aws iam list-policies \
>         --query "Policies[?PolicyName=='${IAM_POLICY_NAME}'].Arn" \
>         --output text
> ) \
> && echo ${IAM_POLICY_ARN}
arn:aws:iam::999999999999:policy/service-role/XRayAccessPolicy-H4BStateMachine

IAMロールの作成

コマンド
# 信頼関係ポリシードキュメントの作成
ASSUME_ROLE_POLICY_DOCUMENT=$(cat << EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "states.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
EOF
) \
&& echo ${ASSUME_ROLE_POLICY_DOCUMENT}

# JSONフォーマットの確認
echo ${ASSUME_ROLE_POLICY_DOCUMENT} | python -m json.tool

# IAMロールの作成
aws iam create-role \
    --role-name ${IAM_ROLE_NAME} \
    --assume-role-policy-document "${ASSUME_ROLE_POLICY_DOCUMENT}" \
    --path /service-role/

# ARN取得
IAM_ROLE_ARN=$(
    aws iam get-role \
        --role-name ${IAM_ROLE_NAME} \
        --query 'Role.Arn' --output text
) \
&& echo ${IAM_ROLE_ARN}

# IAMロールにポリシーをアタッチ
aws iam attach-role-policy \
    --role-name ${IAM_ROLE_NAME} \
    --policy-arn ${IAM_POLICY_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 信頼関係ポリシードキュメントの作成
[cloudshell-user@ip-10-130-38-2 ~]$ ASSUME_ROLE_POLICY_DOCUMENT=$(cat << EOF
> {
>     "Version": "2012-10-17",
>     "Statement": [
>         {
>             "Effect": "Allow",
>             "Principal": {
>                 "Service": "states.amazonaws.com"
>             },
>             "Action": "sts:AssumeRole"
>         }
>     ]
> }
> EOF
> ) \
> && echo ${ASSUME_ROLE_POLICY_DOCUMENT}
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "states.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # JSONフォーマットの確認
[cloudshell-user@ip-10-130-38-2 ~]$ echo ${ASSUME_ROLE_POLICY_DOCUMENT} | python -m json.tool
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "states.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールの作成
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam create-role \
>     --role-name ${IAM_ROLE_NAME} \
>     --assume-role-policy-document "${ASSUME_ROLE_POLICY_DOCUMENT}" \
>     --path /service-role/
{
    "Role": {
        "Path": "/service-role/",
        "RoleName": "StepFunctions-H4BStateMachine-role",
        "RoleId": "AROAWFKRCMKO4GNE2VUAW",
        "Arn": "arn:aws:iam::999999999999:role/service-role/StepFunctions-H4BStateMachine-role",
        "CreateDate": "2024-10-05T02:55:24+00:00",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "states.amazonaws.com"
                    },
                    "Action": "sts:AssumeRole"
                }
            ]
        }
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # ARN取得
[cloudshell-user@ip-10-130-38-2 ~]$ IAM_ROLE_ARN=$(
>     aws iam get-role \
>         --role-name ${IAM_ROLE_NAME} \
>         --query 'Role.Arn' --output text
> ) \
> && echo ${IAM_ROLE_ARN}
arn:aws:iam::999999999999:role/service-role/StepFunctions-H4BStateMachine-role
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールにポリシーをアタッチ
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam attach-role-policy \
>     --role-name ${IAM_ROLE_NAME} \
>     --policy-arn ${IAM_POLICY_ARN}

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
    "Comment": "A description of my state machine",
    "StartAt": "Pass",
    "States": {
        "Pass": {
            "Type": "Pass",
            "End": true
        }
    },
    "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>     "Comment": "A description of my state machine",
>     "StartAt": "Pass",
>     "States": {
>         "Pass": {
>             "Type": "Pass",
>             "End": true
>         }
>     },
>     "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "Pass",
    "States": {
        "Pass": {
            "Type": "Pass",
            "End": true
        }
    },
    "TimeoutSeconds": 30
}

Step Functions ステートマシンの作成

コマンド

# ステートマシンの作成
aws stepfunctions create-state-machine \
    --name ${STATE_MACHINE_NAME} \
    --definition "${DEFINITION_JSON}" \
    --role-arn ${IAM_ROLE_ARN}

# ARN取得
STATE_MACHINE_ARN=$(
    aws stepfunctions list-state-machines \
        --query "stateMachines[?name=='${STATE_MACHINE_NAME}'].stateMachineArn" \
        --output text
) \
&& echo ${STATE_MACHINE_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの作成
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions create-state-machine \
>     --name ${STATE_MACHINE_NAME} \
>     --definition "${DEFINITION_JSON}" \
>     --role-arn ${IAM_ROLE_ARN}
{
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "creationDate": "2024-10-05T11:57:18.571000+09:00"
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # ARN取得
[cloudshell-user@ip-10-130-38-2 ~]$ STATE_MACHINE_ARN=$(
>     aws stepfunctions list-state-machines \
>         --query "stateMachines[?name=='${STATE_MACHINE_NAME}'].stateMachineArn" \
>         --output text
> ) \
> && echo ${STATE_MACHINE_ARN}
arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine

実行

インプットJSON

コマンド
INPUT_JSON=$(cat << EOF
{
  "Comment": "Import your JSON here"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "Comment": "Import your JSON here"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "Comment": "Import your JSON here"
}

ステートマシンの実行

コマンド
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:80c4932d-2cb5-4cae-95b0-623718e86ffb",
    "startDate": "2024-10-05T12:00:48.625000+09:00"
}

最新の実行ARNを取得

コマンド
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:80c4932d-2cb5-4cae-95b0-623718e86ffb

ステートマシンの実行結果の取得

コマンド
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:80c4932d-2cb5-4cae-95b0-623718e86ffb",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "80c4932d-2cb5-4cae-95b0-623718e86ffb",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:00:48.625000+09:00",
    "stopDate": "2024-10-05T12:00:48.709000+09:00",
    "input": "{\n  \"Comment\": \"Import your JSON here\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{\n  \"Comment\": \"Import your JSON here\"\n}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

DynamoDBテーブルの作成

変数

コマンド
# テーブル名
DYNAMODB_TABLE_NAME="Article" \
&& echo ${DYNAMODB_TABLE_NAME}

# パーティションキー
PARTITION_KEY="ArticleID" \
&& echo ${PARTITION_KEY}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # テーブル名
[cloudshell-user@ip-10-130-38-2 ~]$ DYNAMODB_TABLE_NAME="Article" \
> && echo ${DYNAMODB_TABLE_NAME}
Article
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # パーティションキー
[cloudshell-user@ip-10-130-38-2 ~]$ PARTITION_KEY="ArticleID" \
> && echo ${PARTITION_KEY}
ArticleID

作成

コマンド
aws dynamodb create-table \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --attribute-definitions AttributeName=${PARTITION_KEY},AttributeType=S \
    --key-schema AttributeName=${PARTITION_KEY},KeyType=HASH \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

出力
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb create-table \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --attribute-definitions AttributeName=${PARTITION_KEY},AttributeType=S \
>     --key-schema AttributeName=${PARTITION_KEY},KeyType=HASH \
>     --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "ArticleID",
                "AttributeType": "S"
            }
        ],
        "TableName": "Article",
        "KeySchema": [
            {
                "AttributeName": "ArticleID",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "CREATING",
        "CreationDateTime": "2024-10-05T12:02:30.056000+09:00",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:999999999999:table/Article",
        "TableId": "1c4d1b92-1d33-4166-8ab9-9950e54fa0d6",
        "DeletionProtectionEnabled": false
    }
}

データの投入

コマンド
aws dynamodb put-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --item '{
        "ArticleID": {"S": "0001"},
        "Detail": {"S": "AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。"}
    }'

aws dynamodb put-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --item '{
        "ArticleID": {"S": "0002"},
        "Detail": {"S": "AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。"}
    }'

出力
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb put-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --item '{
>         "ArticleID": {"S": "0001"},
>         "Detail": {"S": "AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーシ ョンにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。"}
>     }'
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb put-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --item '{
>         "ArticleID": {"S": "0002"},
>         "Detail": {"S": "AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方 向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。"}
>     }'

ステートマシーン更新

ステートマシンの定義

コマンド
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S": "0001"
          }
        }
      },
      "End": true
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S": "0001"
>           }
>         }
>       },
>       "End": true
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S": "0001"
                    }
                }
            },
            "End": true
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T12:16:33.272000+09:00",
    "revisionId": "30b90cdd-acf7-4f84-8dd4-212351a7b2a1"
}

実行

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:a9476467-5782-42b1-836a-b37fc7885156",
    "startDate": "2024-10-05T12:17:43.465000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:a9476467-5782-42b1-836a-b37fc7885156

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:a9476467-5782-42b1-836a-b37fc7885156",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "a9476467-5782-42b1-836a-b37fc7885156",
    "status": "FAILED",
    "startDate": "2024-10-05T12:17:43.465000+09:00",
    "stopDate": "2024-10-05T12:17:43.694000+09:00",
    "input": "{\n  \"Comment\": \"Import your JSON here\"\n}",
    "inputDetails": {
        "included": true
    },
    "error": "DynamoDb.DynamoDbException",
    "cause": "User: arn:aws:sts::999999999999:assumed-role/StepFunctions-H4BStateMachine-role/NhuSIPyXmaLKkVtMIZERFnOoAajCBFuT is not authorized to perform: dynamodb:GetItem on resource: arn:aws:dynamodb:ap-northeast-1:999999999999:table/Article because no identity-based policy allows the dynamodb:GetItem action (Service: DynamoDb, Status Code: 400, Request ID: 5R5HQN82T9AJV1HAFU61GQU253VV4KQNSO5AEMVJF66Q9ASUAAJG)",
    "redriveCount": 0,
    "redriveStatus": "REDRIVABLE"
}

DynamoDBの権限がないため、エラーになる。
"error": "DynamoDb.DynamoDbException"

IAMポリシーのアタッチ

コマンド
# AWS管理ポリシー
AWS_MGMT_POLICY_NAME="AmazonDynamoDBFullAccess" \
&& echo ${AWS_MGMT_POLICY_NAME}

# IAMロールにポリシーをアタッチ
aws iam attach-role-policy \
    --role-name ${IAM_ROLE_NAME} \
    --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # AWS管理ポリシー
[cloudshell-user@ip-10-130-38-2 ~]$ AWS_MGMT_POLICY_NAME="AmazonDynamoDBFullAccess" \
> && echo ${AWS_MGMT_POLICY_NAME}
AmazonDynamoDBFullAccess
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールにポリシーをアタッチ
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam attach-role-policy \
>     --role-name ${IAM_ROLE_NAME} \
>     --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

実行

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:31b6e779-a4af-4ca8-9275-ea740a5d7f4b",
    "startDate": "2024-10-05T12:22:18.503000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:31b6e779-a4af-4ca8-9275-ea740a5d7f4b

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:31b6e779-a4af-4ca8-9275-ea740a5d7f4b",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "31b6e779-a4af-4ca8-9275-ea740a5d7f4b",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:22:18.503000+09:00",
    "stopDate": "2024-10-05T12:22:18.783000+09:00",
    "input": "{\n  \"Comment\": \"Import your JSON here\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{\"Item\":{\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレス
オーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ
間の入力と出力を追跡しながら集中管理できます。\"}}}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

03 Input の受け取り + Choice ステートを使ってみる

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "End": true
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "End": true
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "End": true
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T12:31:53.034000+09:00",
    "revisionId": "5f8cf4d6-8766-4b94-a6c0-26249b398473"
}

実行1

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:40cb3910-c1e5-4e10-b145-c5efd23fc82f",
    "startDate": "2024-10-05T12:33:14.713000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:40cb3910-c1e5-4e10-b145-c5efd23fc82f

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:40cb3910-c1e5-4e10-b145-c5efd23fc82f",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "40cb3910-c1e5-4e10-b145-c5efd23fc82f",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:33:14.713000+09:00",
    "stopDate": "2024-10-05T12:33:14.997000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{\"Item\":{\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレス
オーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ
間の入力と出力を追跡しながら集中管理できます。\"}}}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

実行2

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0002"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0002"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0002"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:be77bc06-c15b-4289-b93e-d55d240fd5c0",
    "startDate": "2024-10-05T12:38:34.150000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:be77bc06-c15b-4289-b93e-d55d240fd5c0

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:be77bc06-c15b-4289-b93e-d55d240fd5c0",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "be77bc06-c15b-4289-b93e-d55d240fd5c0",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:38:34.150000+09:00",
    "stopDate": "2024-10-05T12:38:34.418000+09:00",
    "input": "{\n  \"ArticleID\": \"0002\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{\"Item\":{\"ArticleID\":{\"S\":\"0002\"},\"Detail\":{\"S\":\"AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。\"}}}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

実行3

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "9999"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "9999"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "9999"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:da2d2ab8-d7af-4f54-a3fa-1ea69231c806",
    "startDate": "2024-10-05T12:42:32.475000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:da2d2ab8-d7af-4f54-a3fa-1ea69231c806

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:da2d2ab8-d7af-4f54-a3fa-1ea69231c806",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "da2d2ab8-d7af-4f54-a3fa-1ea69231c806",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:42:32.475000+09:00",
    "stopDate": "2024-10-05T12:42:32.746000+09:00",
    "input": "{\n  \"ArticleID\": \"9999\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Success"
        }
      ]
    },
    "Success": {
      "Type": "Succeed"
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Success"
>         }
>       ]
>     },
>     "Success": {
>       "Type": "Succeed"
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Success"
                }
            ]
        },
        "Success": {
            "Type": "Succeed"
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T12:44:27.373000+09:00",
    "revisionId": "a8df2e9c-ee6e-4c40-975c-b3f3d8c54a4b"
}

実行4

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0002"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0002"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0002"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7783432f-3b56-43bb-b6df-153e4727bd61",
    "startDate": "2024-10-05T12:45:41.238000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7783432f-3b56-43bb-b6df-153e4727bd61

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7783432f-3b56-43bb-b6df-153e4727bd61",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "7783432f-3b56-43bb-b6df-153e4727bd61",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T12:45:41.238000+09:00",
    "stopDate": "2024-10-05T12:45:41.502000+09:00",
    "input": "{\n  \"ArticleID\": \"0002\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "{\"Item\":{\"ArticleID\":{\"S\":\"0002\"},\"Detail\":{\"S\":\"AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。\"}}}",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

実行5

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "9999"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "9999"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "9999"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ec53ec69-be2b-4aea-a306-444079aeb225",
    "startDate": "2024-10-05T12:49:30.381000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ec53ec69-be2b-4aea-a306-444079aeb225

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ec53ec69-be2b-4aea-a306-444079aeb225",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "ec53ec69-be2b-4aea-a306-444079aeb225",
    "status": "FAILED",
    "startDate": "2024-10-05T12:49:30.381000+09:00",
    "stopDate": "2024-10-05T12:49:30.700000+09:00",
    "input": "{\n  \"ArticleID\": \"9999\"\n}",
    "inputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "REDRIVABLE"
}

04 Parallel ステートで処理を並列に実行する

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text": "こんにちは"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "End": true
            }
          }
        },
        {
          "StartAt": "Pass",
          "States": {
            "Pass": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text": "こんにちは"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "Pass",
>           "States": {
>             "Pass": {
>               "Type": "Pass",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text": "\u3053\u3093\u306b\u3061\u306f"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "Pass",
                    "States": {
                        "Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T13:02:00.973000+09:00",
    "revisionId": "806915e6-4bc5-411a-848e-2ae90e24699b"
}

IAMポリシーのアタッチ

コマンド
# AWS管理ポリシー
AWS_MGMT_POLICY_NAME="TranslateReadOnly" \
&& echo ${AWS_MGMT_POLICY_NAME}

# IAMロールにポリシーをアタッチ
aws iam attach-role-policy \
    --role-name ${IAM_ROLE_NAME} \
    --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # AWS管理ポリシー
[cloudshell-user@ip-10-130-38-2 ~]$ AWS_MGMT_POLICY_NAME="TranslateReadOnly" \
> && echo ${AWS_MGMT_POLICY_NAME}
TranslateReadOnly
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールにポリシーをアタッチ
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam attach-role-policy \
>     --role-name ${IAM_ROLE_NAME} \
>     --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

実行1

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d51f1c6b-181a-473f-a1c3-521f20904b5b",
    "startDate": "2024-10-05T13:03:04.957000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d51f1c6b-181a-473f-a1c3-521f20904b5b

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d51f1c6b-181a-473f-a1c3-521f20904b5b",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "d51f1c6b-181a-473f-a1c3-521f20904b5b",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T13:03:04.957000+09:00",
    "stopDate": "2024-10-05T13:03:05.533000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"Hi\"},{\"Item\":{\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレー
ションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

実行2

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0002"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0002"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0002"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d93793bc-4991-4862-a5aa-c257886c27c2",
    "startDate": "2024-10-05T13:05:54.504000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d93793bc-4991-4862-a5aa-c257886c27c2

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:d93793bc-4991-4862-a5aa-c257886c27c2",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "d93793bc-4991-4862-a5aa-c257886c27c2",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T13:05:54.504000+09:00",
    "stopDate": "2024-10-05T13:05:55.038000+09:00",
    "input": "{\n  \"ArticleID\": \"0002\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"Hi\"},{\"Item\":{\"ArticleID\":{\"S\":\"0002\"},\"Detail\":{\"S\":\"AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る
方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

05 Output の調整 + DB(Amazon DynamoDB)の更新

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S": "0001"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S": "Hello"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "Pass",
          "States": {
            "Pass": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S": "0001"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S": "Hello"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "Pass",
>           "States": {
>             "Pass": {
>               "Type": "Pass",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S": "0001"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S": "Hello"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "Pass",
                    "States": {
                        "Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T13:08:55.596000+09:00",
    "revisionId": "6ef1997f-93d5-454e-9ce0-b95733da2332"
}

実行1

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ceaadef2-5ba3-4a3e-977b-621df6ebde65",
    "startDate": "2024-10-05T13:17:37.654000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ceaadef2-5ba3-4a3e-977b-621df6ebde65

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ceaadef2-5ba3-4a3e-977b-621df6ebde65",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "ceaadef2-5ba3-4a3e-977b-621df6ebde65",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T13:17:37.654000+09:00",
    "stopDate": "2024-10-05T13:17:38.366000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"Item\":{\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバー
レスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステ
ップ間の入力と出力を追跡しながら集中管理できます。\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

DynamoDBテーブル項目取得

コマンド
# キー指定
TABLE_KEY=$(cat << EOF
{
    "${PARTITION_KEY}": {"S": "0001"}
}
EOF
) \
&& echo ${TABLE_KEY} | python -m json.tool

# 項目取得
aws dynamodb get-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --key "${TABLE_KEY}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # キー指定
[cloudshell-user@ip-10-130-38-2 ~]$ TABLE_KEY=$(cat << EOF
> {
>     "${PARTITION_KEY}": {"S": "0001"}
> }
> EOF
> ) \
> && echo ${TABLE_KEY} | python -m json.tool
{
    "ArticleID": {
        "S": "0001"
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # 項目取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb get-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --key "${TABLE_KEY}"
{
    "Item": {
        "EnglishVersion": {
            "S": "Hello"
        },
        "ArticleID": {
            "S": "0001"
        },
        "Detail": {
            "S": "AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。"
        }
    }
}

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S": "0001"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S": "Hello"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "Pass",
          "States": {
            "Pass": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S": "0001"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S": "Hello"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "Pass",
>           "States": {
>             "Pass": {
>               "Type": "Pass",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S": "0001"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S": "Hello"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "Pass",
                    "States": {
                        "Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T13:47:27.983000+09:00",
    "revisionId": "0afad79d-98d3-4051-8226-6d13828fb3ad"
}

実行2

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:2717d00c-7027-4e70-86d0-af999894f334",
    "startDate": "2024-10-05T13:48:07.442000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:2717d00c-7027-4e70-86d0-af999894f334

実行詳細を取得

コマンド
# 実行詳細を取得
aws stepfunctions get-execution-history \
    --execution-arn ${LATEST_EXECUTION_ARN} \
    --no-cli-pager

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 実行詳細を取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions get-execution-history \
>     --execution-arn ${LATEST_EXECUTION_ARN} 
{
    "events": [
        {
            "timestamp": "2024-10-05T14:15:18.088000+09:00",
            "type": "ExecutionStarted",
            "id": 1,
            "previousEventId": 0,
            "executionStartedEventDetails": {
                "input": "{\n  \"ArticleID\": \"0001\"\n}",
                "inputDetails": {
                    "truncated": false
                },
                "roleArn": "arn:aws:iam::999999999999:role/service-role/StepFunctions-H4BStateMachine-role"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.147000+09:00",
            "type": "TaskStateEntered",
            "id": 2,
            "previousEventId": 0,
            "stateEnteredEventDetails": {
                "name": "DynamoDB GetItem",
                "input": "{\n  \"ArticleID\": \"0001\"\n}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.147000+09:00",
            "type": "TaskScheduled",
            "id": 3,
            "previousEventId": 2,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem",
                "region": "ap-northeast-1",
                "parameters": "{\"TableName\":\"Article\",\"Key\":{\"ArticleID\":{\"S\":\"0001\"}}}"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.216000+09:00",
            "type": "TaskStarted",
            "id": 4,
            "previousEventId": 3,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.303000+09:00",
            "type": "TaskSucceeded",
            "id": 5,
            "previousEventId": 4,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフ
ローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "TaskStateExited",
            "id": 6,
            "previousEventId": 5,
            "stateExitedEventDetails": {
                "name": "DynamoDB GetItem",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフ
ローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "ChoiceStateEntered",
            "id": 7,
            "previousEventId": 6,
            "stateEnteredEventDetails": {
                "name": "Choice - Item is present",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "ChoiceStateExited",
            "id": 8,
            "previousEventId": 7,
            "stateExitedEventDetails": {
                "name": "Choice - Item is present",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフ
ローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "ParallelStateEntered",
            "id": 9,
            "previousEventId": 8,
            "stateEnteredEventDetails": {
                "name": "Parallel",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "ParallelStateStarted",
            "id": 10,
            "previousEventId": 9
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "TaskStateEntered",
            "id": 11,
            "previousEventId": 10,
            "stateEnteredEventDetails": {
                "name": "TranslateText",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "TaskScheduled",
            "id": 12,
            "previousEventId": 11,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText",
                "region": "ap-northeast-1",
                "parameters": "{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"Text\":\"AWS Step Functions は、最新のアプ
リケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加され
たワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "PassStateEntered",
            "id": 13,
            "previousEventId": 10,
            "stateEnteredEventDetails": {
                "name": "Pass",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.334000+09:00",
            "type": "PassStateExited",
            "id": 14,
            "previousEventId": 13,
            "stateExitedEventDetails": {
                "name": "Pass",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフ
ローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.421000+09:00",
            "type": "TaskStarted",
            "id": 15,
            "previousEventId": 12,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.566000+09:00",
            "type": "TaskSucceeded",
            "id": 16,
            "previousEventId": 15,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText",
                "output": "{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.595000+09:00",
            "type": "TaskStateExited",
            "id": 17,
            "previousEventId": 16,
            "stateExitedEventDetails": {
                "name": "TranslateText",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフ
ローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.595000+09:00",
            "type": "TaskStateEntered",
            "id": 18,
            "previousEventId": 17,
            "stateEnteredEventDetails": {
                "name": "DynamoDB UpdateItem - EnglishVersion",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.595000+09:00",
            "type": "TaskScheduled",
            "id": 19,
            "previousEventId": 18,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem",
                "region": "ap-northeast-1",
                "parameters": "{\"TableName\":\"Article\",\"Key\":{\"ArticleID\":{\"S\":\"0001\"}},\"UpdateExpression\":\"SET EnglishVersion = :EnglishVersionRef\",\"ExpressionAttributeValues\":{\":EnglishVersionRef\":{\"S\":\"Hello\"}}}"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.671000+09:00",
            "type": "TaskStarted",
            "id": 20,
            "previousEventId": 19,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem"
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.768000+09:00",
            "type": "TaskSucceeded",
            "id": 21,
            "previousEventId": 20,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem",
                "output": "{}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.795000+09:00",
            "type": "TaskStateExited",
            "id": 22,
            "previousEventId": 21,
            "stateExitedEventDetails": {
                "name": "DynamoDB UpdateItem - EnglishVersion",
                "output": "{}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.795000+09:00",
            "type": "ParallelStateSucceeded",
            "id": 23,
            "previousEventId": 22
        },
        {
            "timestamp": "2024-10-05T14:15:18.795000+09:00",
            "type": "ParallelStateExited",
            "id": 24,
            "previousEventId": 22,
            "stateExitedEventDetails": {
                "name": "Parallel",
                "output": "[{},{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割さ
れフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}]",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T14:15:18.860000+09:00",
            "type": "ExecutionSucceeded",
            "id": 25,
            "previousEventId": 24,
            "executionSucceededEventDetails": {
                "output": "[{},{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割さ
れフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}]",
                "outputDetails": {
                    "truncated": false
                }
            }
        }
    ]
}

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "Pass",
          "States": {
            "Pass": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "Pass",
>           "States": {
>             "Pass": {
>               "Type": "Pass",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "Pass",
                    "States": {
                        "Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T14:23:41.202000+09:00",
    "revisionId": "36c6053f-76b5-4809-b258-cd108440d27a"
}

実行2

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ddabc388-83a5-4dc2-bb85-3e1b577f40d4",
    "startDate": "2024-10-05T14:24:07.024000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ddabc388-83a5-4dc2-bb85-3e1b577f40d4

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:ddabc388-83a5-4dc2-bb85-3e1b577f40d4",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "ddabc388-83a5-4dc2-bb85-3e1b577f40d4",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T14:24:07.024000+09:00",
    "stopDate": "2024-10-05T14:24:07.770000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"Item\":{\"EnglishVersion\":{\"S\":\"Hello\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジ
ックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

DynamoDBテーブル項目取得

コマンド
# キー指定
TABLE_KEY=$(cat << EOF
{
    "${PARTITION_KEY}": {"S": "0001"}
}
EOF
) \
&& echo ${TABLE_KEY} | python -m json.tool

# 項目取得
aws dynamodb get-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --key "${TABLE_KEY}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # キー指定
[cloudshell-user@ip-10-130-38-2 ~]$ TABLE_KEY=$(cat << EOF
> {
>     "${PARTITION_KEY}": {"S": "0001"}
> }
> EOF
> ) \
> && echo ${TABLE_KEY} | python -m json.tool
{
    "ArticleID": {
        "S": "0001"
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # 項目取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb get-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --key "${TABLE_KEY}"
{
    "Item": {
        "EnglishVersion": {
            "S": "AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps."
        },
        "ArticleID": {
            "S": "0001"
        },
        "Detail": {
            "S": "AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。"
        }
    }
}

実行3

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0002"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0002"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0002"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:f9ca113c-9e77-4dc2-a9d6-91ab7c5bb6ec",
    "startDate": "2024-10-05T14:26:39.953000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:f9ca113c-9e77-4dc2-a9d6-91ab7c5bb6ec

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:f9ca113c-9e77-4dc2-a9d6-91ab7c5bb6ec",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "f9ca113c-9e77-4dc2-a9d6-91ab7c5bb6ec",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T14:26:39.953000+09:00",
    "stopDate": "2024-10-05T14:26:40.694000+09:00",
    "input": "{\n  \"ArticleID\": \"0002\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"Item\":{\"ArticleID\":{\"S\":\"0002\"},\"Detail\":{\"S\":\"AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

DynamoDBテーブル項目取得

コマンド
# キー指定
TABLE_KEY=$(cat << EOF
{
    "${PARTITION_KEY}": {"S": "0002"}
}
EOF
) \
&& echo ${TABLE_KEY} | python -m json.tool

# 項目取得
aws dynamodb get-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --key "${TABLE_KEY}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # キー指定
[cloudshell-user@ip-10-130-38-2 ~]$ TABLE_KEY=$(cat << EOF
> {
>     "${PARTITION_KEY}": {"S": "0002"}
> }
> EOF
> ) \
> && echo ${TABLE_KEY} | python -m json.tool
{
    "ArticleID": {
        "S": "0002"
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # 項目取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb get-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --key "${TABLE_KEY}"
{
    "Item": {
        "EnglishVersion": {
            "S": "For AWS beginners, titled “AWS Hands-on for Beginners,” we have prepared hands-on sessions for each theme that you can proceed at your own pace while watching videos explaining the operation procedures for those who are using AWS for the first time or for those who touch the target service for the first time."
        },
        "ArticleID": {
            "S": "0002"
        },
        "Detail": {
            "S": "AWS 初心者向けに「AWS Hands-on for Beginners」と題し、初めて AWS を利用する方や、初めて対象のサービスを触る方向けに、操作手順の解説動画を見ながら自分のペースで進められるハンズオンをテーマごとにご用意しています。"
        }
    }
}

06 Parallel ステートのもう一方の実装 - テキストを音声化する

S3バケット作成

コマンド
# バケット名
date_var=$(date +%Y%m%d) \
&& echo ${date_var}
S3_BUCKET_NAME="h4b-stepfunctions-output-${date_var}" \
&& echo ${S3_BUCKET_NAME}

# S3バケット作成
aws s3 mb s3://${S3_BUCKET_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # バケット名
[cloudshell-user@ip-10-130-38-2 ~]$ date_var=$(date +%Y%m%d) \
> && echo ${date_var}
20241005
[cloudshell-user@ip-10-130-38-2 ~]$ S3_BUCKET_NAME="h4b-stepfunctions-output-${date_var}" \
> && echo ${S3_BUCKET_NAME}
h4b-stepfunctions-output-20241005
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # S3バケット作成
[cloudshell-user@ip-10-130-38-2 ~]$ aws s3 mb s3://${S3_BUCKET_NAME}
make_bucket: h4b-stepfunctions-output-20241005

IAMポリシーのアタッチ

コマンド
# AWS管理ポリシー (AmazonPollyFullAcces)
AWS_MGMT_POLICY_NAME="AmazonPollyFullAccess" \
&& echo ${AWS_MGMT_POLICY_NAME}

# IAMロールにポリシーをアタッチ
aws iam attach-role-policy \
    --role-name ${IAM_ROLE_NAME} \
    --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

# AWS管理ポリシー (AmazonS3FullAccess)
AWS_MGMT_POLICY_NAME="AmazonS3FullAccess" \
&& echo ${AWS_MGMT_POLICY_NAME}

# IAMロールにポリシーをアタッチ
aws iam attach-role-policy \
    --role-name ${IAM_ROLE_NAME} \
    --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # AWS管理ポリシー (AmazonPollyFullAcces)
[cloudshell-user@ip-10-130-38-2 ~]$ AWS_MGMT_POLICY_NAME="AmazonPollyFullAccess" \
> && echo ${AWS_MGMT_POLICY_NAME}
AmazonPollyFullAccess
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールにポリシーをアタッチ
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam attach-role-policy \
>     --role-name ${IAM_ROLE_NAME} \
>     --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # AWS管理ポリシー (AmazonS3FullAccess)
[cloudshell-user@ip-10-130-38-2 ~]$ AWS_MGMT_POLICY_NAME="AmazonS3FullAccess" \
> && echo ${AWS_MGMT_POLICY_NAME}
AmazonS3FullAccess
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールにポリシーをアタッチ
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam attach-role-policy \
>     --role-name ${IAM_ROLE_NAME} \
>     --policy-arn arn:aws:iam::aws:policy/${AWS_MGMT_POLICY_NAME}

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "StartSpeechSynthesisTask",
          "States": {
            "StartSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "OutputFormat": "mp3",
                "OutputS3BucketName": "${S3_BUCKET_NAME}",
                "Text.$": "$.Item.Detail.S",
                "VoiceId": "Mizuki"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "StartSpeechSynthesisTask",
>           "States": {
>             "StartSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "OutputFormat": "mp3",
>                 "OutputS3BucketName": "${S3_BUCKET_NAME}",
>                 "Text.$": "$.Item.Detail.S",
>                 "VoiceId": "Mizuki"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "StartSpeechSynthesisTask",
                    "States": {
                        "StartSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "OutputFormat": "mp3",
                                "OutputS3BucketName": "h4b-stepfunctions-output-20241005",
                                "Text.$": "$.Item.Detail.S",
                                "VoiceId": "Mizuki"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T14:31:14.767000+09:00",
    "revisionId": "9a993117-0374-4556-a1c1-53b0eb1e958c"
}

実行1

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:4d09480c-d77e-49e6-925d-71da9591d7cb",
    "startDate": "2024-10-05T14:45:56.184000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:4d09480c-d77e-49e6-925d-71da9591d7cb

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:4d09480c-d77e-49e6-925d-71da9591d7cb",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "4d09480c-d77e-49e6-925d-71da9591d7cb",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T14:45:56.184000+09:00",
    "stopDate": "2024-10-05T14:45:57.029000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T05:45:56.717Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/6dc0d67f-cc22-4cf8-9bdc-adb846a9a865.mp3\",\"RequestCharacters\":130,\"TaskId\":\"6dc0d67f-cc22-4cf8-9bdc-adb846a9a865\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

S3バケット内オブジェクト確認

コマンド
# S3バケット内オブジェクト確認
aws s3api list-object-versions \
    --bucket ${S3_BUCKET_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # S3バケット内オブジェクト確認
[cloudshell-user@ip-10-130-38-2 ~]$ aws s3api list-object-versions \
>     --bucket ${S3_BUCKET_NAME}
{
    "Versions": [
        {
            "ETag": "\"680ea0a65a4d0f0473fce0c836707788\"",
            "Size": 120887,
            "StorageClass": "STANDARD",
            "Key": "6dc0d67f-cc22-4cf8-9bdc-adb846a9a865.mp3",
            "VersionId": "null",
            "IsLatest": true,
            "LastModified": "2024-10-05T05:46:00+00:00",
            "Owner": {
                "DisplayName": "user01",
                "ID": "974cd83317461904fcbad632a42045ad8cd85cb24065c65f2ca62b7e35d84078"
            }
        }
    ],
    "RequestCharged": null
}

07 ステートで「タスクが終わるまで待つ」を実装する

ステートマシーン更新1

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "StartSpeechSynthesisTask",
          "States": {
            "StartSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "OutputFormat": "mp3",
                "OutputS3BucketName": "${S3_BUCKET_NAME}",
                "Text.$": "$.Item.Detail.S",
                "VoiceId": "Mizuki"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
              "End": true
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "StartSpeechSynthesisTask",
>           "States": {
>             "StartSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "OutputFormat": "mp3",
>                 "OutputS3BucketName": "${S3_BUCKET_NAME}",
>                 "Text.$": "$.Item.Detail.S",
>                 "VoiceId": "Mizuki"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
>               "End": true
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "StartSpeechSynthesisTask",
                    "States": {
                        "StartSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "OutputFormat": "mp3",
                                "OutputS3BucketName": "h4b-stepfunctions-output-20241005",
                                "Text.$": "$.Item.Detail.S",
                                "VoiceId": "Mizuki"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
                            "End": true
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T14:31:14.767000+09:00",
    "revisionId": "9a993117-0374-4556-a1c1-53b0eb1e958c"
}

実行1

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:a619afda-57a8-4e39-ae62-e156fb93df09",
    "startDate": "2024-10-05T15:13:38.566000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:a619afda-57a8-4e39-ae62-e156fb93df09

実行詳細を取得

コマンド
# 実行詳細を取得
aws stepfunctions get-execution-history \
    --execution-arn ${LATEST_EXECUTION_ARN} \
    --no-cli-pager

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 実行詳細を取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions get-execution-history \
>     --execution-arn ${LATEST_EXECUTION_ARN} \
>     --no-cli-pager
{
    "events": [
        {
            "timestamp": "2024-10-05T15:13:38.566000+09:00",
            "type": "ExecutionStarted",
            "id": 1,
            "previousEventId": 0,
            "executionStartedEventDetails": {
                "input": "{\n  \"ArticleID\": \"0001\"\n}",
                "inputDetails": {
                    "truncated": false
                },
                "roleArn": "arn:aws:iam::999999999999:role/service-role/StepFunctions-H4BStateMachine-role"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.624000+09:00",
            "type": "TaskStateEntered",
            "id": 2,
            "previousEventId": 0,
            "stateEnteredEventDetails": {
                "name": "DynamoDB GetItem",
                "input": "{\n  \"ArticleID\": \"0001\"\n}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.624000+09:00",
            "type": "TaskScheduled",
            "id": 3,
            "previousEventId": 2,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem",
                "region": "ap-northeast-1",
                "parameters": "{\"TableName\":\"Article\",\"Key\":{\"ArticleID\":{\"S\":\"0001\"}}}"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.705000+09:00",
            "type": "TaskStarted",
            "id": 4,
            "previousEventId": 3,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.784000+09:00",
            "type": "TaskSucceeded",
            "id": 5,
            "previousEventId": 4,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "getItem",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレ スオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステッ プ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "TaskStateExited",
            "id": 6,
            "previousEventId": 5,
            "stateExitedEventDetails": {
                "name": "DynamoDB GetItem",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレ スオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステッ プ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "ChoiceStateEntered",
            "id": 7,
            "previousEventId": 6,
            "stateEnteredEventDetails": {
                "name": "Choice - Item is present",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ 間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "ChoiceStateExited",
            "id": 8,
            "previousEventId": 7,
            "stateExitedEventDetails": {
                "name": "Choice - Item is present",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレ スオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステッ プ間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "ParallelStateEntered",
            "id": 9,
            "previousEventId": 8,
            "stateEnteredEventDetails": {
                "name": "Parallel",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ 間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "ParallelStateStarted",
            "id": 10,
            "previousEventId": 9
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "TaskStateEntered",
            "id": 11,
            "previousEventId": 10,
            "stateEnteredEventDetails": {
                "name": "TranslateText",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ 間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "TaskScheduled",
            "id": 12,
            "previousEventId": 11,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText",
                "region": "ap-northeast-1",
                "parameters": "{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"Text\":\"AWS Step Functions は、最新のアプ リケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加され たワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "TaskStateEntered",
            "id": 13,
            "previousEventId": 10,
            "stateEnteredEventDetails": {
                "name": "StartSpeechSynthesisTask",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ 間の入力と出力を追跡しながら集中管理できます。\"}}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.815000+09:00",
            "type": "TaskScheduled",
            "id": 14,
            "previousEventId": 13,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:polly",
                "resource": "startSpeechSynthesisTask",
                "region": "ap-northeast-1",
                "parameters": "{\"OutputFormat\":\"mp3\",\"OutputS3BucketName\":\"h4b-stepfunctions-output-20241005\",\"VoiceId\":\"Mizuki\",\"Text\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、 複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。\"}"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.879000+09:00",
            "type": "TaskStarted",
            "id": 15,
            "previousEventId": 14,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:polly",
                "resource": "startSpeechSynthesisTask"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:38.885000+09:00",
            "type": "TaskStarted",
            "id": 16,
            "previousEventId": 12,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.038000+09:00",
            "type": "TaskSucceeded",
            "id": 17,
            "previousEventId": 16,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:translate",
                "resource": "translateText",
                "output": "{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.112000+09:00",
            "type": "TaskSucceeded",
            "id": 18,
            "previousEventId": 15,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:polly",
                "resource": "startSpeechSynthesisTask",
                "output": "{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:13:39.055Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/483e68f0-d557-4464-8607-1a0fc351c5a6.mp3\",\"RequestCharacters\":130,\"TaskId\":\"483e68f0-d557-4464-8607-1a0fc351c5a6\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.145000+09:00",
            "type": "TaskStateExited",
            "id": 19,
            "previousEventId": 17,
            "stateExitedEventDetails": {
                "name": "TranslateText",
                "output": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレ スオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステッ プ間の入力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.145000+09:00",
            "type": "TaskStateExited",
            "id": 20,
            "previousEventId": 18,
            "stateExitedEventDetails": {
                "name": "StartSpeechSynthesisTask",
                "output": "{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:13:39.055Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/483e68f0-d557-4464-8607-1a0fc351c5a6.mp3\",\"RequestCharacters\":130,\"TaskId\":\"483e68f0-d557-4464-8607-1a0fc351c5a6\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.145000+09:00",
            "type": "TaskStateEntered",
            "id": 21,
            "previousEventId": 19,
            "stateEnteredEventDetails": {
                "name": "DynamoDB UpdateItem - EnglishVersion",
                "input": "{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ 間の入力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SourceLanguageCode\":\"ja\",\"TargetLanguageCode\":\"en\",\"TranslatedText\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}}",
                "inputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.145000+09:00",
            "type": "TaskScheduled",
            "id": 22,
            "previousEventId": 21,
            "taskScheduledEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem",
                "region": "ap-northeast-1",
                "parameters": "{\"TableName\":\"Article\",\"Key\":{\"ArticleID\":{\"S\":\"0001\"}},\"UpdateExpression\":\"SET EnglishVersion = :EnglishVersionRef\",\"ExpressionAttributeValues\":{\":EnglishVersionRef\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"}}}"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.227000+09:00",
            "type": "TaskStarted",
            "id": 23,
            "previousEventId": 22,
            "taskStartedEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem"
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.305000+09:00",
            "type": "TaskSucceeded",
            "id": 24,
            "previousEventId": 23,
            "taskSucceededEventDetails": {
                "resourceType": "aws-sdk:dynamodb",
                "resource": "updateItem",
                "output": "{}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.364000+09:00",
            "type": "TaskStateExited",
            "id": 25,
            "previousEventId": 24,
            "stateExitedEventDetails": {
                "name": "DynamoDB UpdateItem - EnglishVersion",
                "output": "{}",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.364000+09:00",
            "type": "ParallelStateSucceeded",
            "id": 26,
            "previousEventId": 25
        },
        {
            "timestamp": "2024-10-05T15:13:39.364000+09:00",
            "type": "ParallelStateExited",
            "id": 27,
            "previousEventId": 25,
            "stateExitedEventDetails": {
                "name": "Parallel",
                "output": "[{},{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:13:39.055Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/483e68f0-d557-4464-8607-1a0fc351c5a6.mp3\",\"RequestCharacters\":130,\"TaskId\":\"483e68f0-d557-4464-8607-1a0fc351c5a6\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}]",
                "outputDetails": {
                    "truncated": false
                }
            }
        },
        {
            "timestamp": "2024-10-05T15:13:39.418000+09:00",
            "type": "ExecutionSucceeded",
            "id": 28,
            "previousEventId": 27,
            "executionSucceededEventDetails": {
                "output": "[{},{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:13:39.055Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/483e68f0-d557-4464-8607-1a0fc351c5a6.mp3\",\"RequestCharacters\":130,\"TaskId\":\"483e68f0-d557-4464-8607-1a0fc351c5a6\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}]",
                "outputDetails": {
                    "truncated": false
                }
            }
        }
    ]
}

ステートマシーン更新2

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "StartSpeechSynthesisTask",
          "States": {
            "StartSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "OutputFormat": "mp3",
                "OutputS3BucketName": "${S3_BUCKET_NAME}",
                "Text.$": "$.Item.Detail.S",
                "VoiceId": "Mizuki"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
              "ResultPath": "$.Result",
              "Next": "GetSpeechSynthesisTask"
            },
            "GetSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "TaskId.$": "$.Result.SynthesisTask.TaskId"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
              "End": true,
              "ResultPath": "$.Result"
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "StartSpeechSynthesisTask",
>           "States": {
>             "StartSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "OutputFormat": "mp3",
>                 "OutputS3BucketName": "${S3_BUCKET_NAME}",
>                 "Text.$": "$.Item.Detail.S",
>                 "VoiceId": "Mizuki"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
>               "ResultPath": "$.Result",
>               "Next": "GetSpeechSynthesisTask"
>             },
>             "GetSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "TaskId.$": "$.Result.SynthesisTask.TaskId"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
>               "End": true,
>               "ResultPath": "$.Result"
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "StartSpeechSynthesisTask",
                    "States": {
                        "StartSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "OutputFormat": "mp3",
                                "OutputS3BucketName": "h4b-stepfunctions-output-20241005",
                                "Text.$": "$.Item.Detail.S",
                                "VoiceId": "Mizuki"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
                            "ResultPath": "$.Result",
                            "Next": "GetSpeechSynthesisTask"
                        },
                        "GetSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "TaskId.$": "$.Result.SynthesisTask.TaskId"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
                            "End": true,
                            "ResultPath": "$.Result"
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T15:34:59.694000+09:00",
    "revisionId": "63cba198-0feb-4470-9b6c-9ace6ce1de90"
}

実行2

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:8638e7d8-d93c-40f1-97e9-fbdeebb38ca9",
    "startDate": "2024-10-05T15:37:12.842000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:8638e7d8-d93c-40f1-97e9-fbdeebb38ca9

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:8638e7d8-d93c-40f1-97e9-fbdeebb38ca9",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "8638e7d8-d93c-40f1-97e9-fbdeebb38ca9",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T15:37:12.842000+09:00",
    "stopDate": "2024-10-05T15:37:13.639000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケ
ストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入
力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:37:13.304Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/5dd55cd5-ab58-407f-b2e6-5bada047d7ea.mp3\",\"RequestCharacters\":130,\"TaskId\":\"5dd55cd5-ab58-407f-b2e6-5bada047d7ea\",\"TaskStatus\":\"scheduled\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

ステートマシーン更新3

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "StartSpeechSynthesisTask",
          "States": {
            "StartSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "OutputFormat": "mp3",
                "OutputS3BucketName": "${S3_BUCKET_NAME}",
                "Text.$": "$.Item.Detail.S",
                "VoiceId": "Mizuki"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
              "ResultPath": "$.Result",
              "Next": "GetSpeechSynthesisTask"
            },
            "GetSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "TaskId.$": "$.Result.SynthesisTask.TaskId"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
              "ResultPath": "$.Result",
              "Next": "Choice - Task is completed"
            },
            "Choice - Task is completed": {
              "Type": "Choice",
              "Choices": [
                {
                  "Variable": "$.Result.SynthesisTask.TaskStatus",
                  "StringMatches": "completed",
                  "Next": "Pass"
                }
              ],
              "Default": "Wait"
            },
            "Pass": {
              "Type": "Pass",
              "End": true
            },
            "Wait": {
              "Type": "Wait",
              "Seconds": 5,
              "Next": "GetSpeechSynthesisTask"
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "StartSpeechSynthesisTask",
>           "States": {
>             "StartSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "OutputFormat": "mp3",
>                 "OutputS3BucketName": "${S3_BUCKET_NAME}",
>                 "Text.$": "$.Item.Detail.S",
>                 "VoiceId": "Mizuki"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
>               "ResultPath": "$.Result",
>               "Next": "GetSpeechSynthesisTask"
>             },
>             "GetSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "TaskId.$": "$.Result.SynthesisTask.TaskId"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
>               "ResultPath": "$.Result",
>               "Next": "Choice - Task is completed"
>             },
>             "Choice - Task is completed": {
>               "Type": "Choice",
>               "Choices": [
>                 {
>                   "Variable": "$.Result.SynthesisTask.TaskStatus",
>                   "StringMatches": "completed",
>                   "Next": "Pass"
>                 }
>               ],
>               "Default": "Wait"
>             },
>             "Pass": {
>               "Type": "Pass",
>               "End": true
>             },
>             "Wait": {
>               "Type": "Wait",
>               "Seconds": 5,
>               "Next": "GetSpeechSynthesisTask"
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "StartSpeechSynthesisTask",
                    "States": {
                        "StartSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "OutputFormat": "mp3",
                                "OutputS3BucketName": "h4b-stepfunctions-output-20241005",
                                "Text.$": "$.Item.Detail.S",
                                "VoiceId": "Mizuki"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
                            "ResultPath": "$.Result",
                            "Next": "GetSpeechSynthesisTask"
                        },
                        "GetSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "TaskId.$": "$.Result.SynthesisTask.TaskId"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
                            "ResultPath": "$.Result",
                            "Next": "Choice - Task is completed"
                        },
                        "Choice - Task is completed": {
                            "Type": "Choice",
                            "Choices": [
                                {
                                    "Variable": "$.Result.SynthesisTask.TaskStatus",
                                    "StringMatches": "completed",
                                    "Next": "Pass"
                                }
                            ],
                            "Default": "Wait"
                        },
                        "Pass": {
                            "Type": "Pass",
                            "End": true
                        },
                        "Wait": {
                            "Type": "Wait",
                            "Seconds": 5,
                            "Next": "GetSpeechSynthesisTask"
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T15:43:31.062000+09:00",
    "revisionId": "ffbf5650-0871-45d7-8767-8bf20244736d"
}

実行3

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7ca911af-27e2-409b-af7f-b6293152828a",
    "startDate": "2024-10-05T15:44:26.587000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7ca911af-27e2-409b-af7f-b6293152828a

ステートマシンの実行結果の取得

コマンド
# ステートマシンの実行結果の取得
aws stepfunctions describe-execution \
    --execution-arn ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行結果の取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions describe-execution \
>     --execution-arn ${LATEST_EXECUTION_ARN}
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:7ca911af-27e2-409b-af7f-b6293152828a",
    "stateMachineArn": "arn:aws:states:ap-northeast-1:999999999999:stateMachine:H4BStateMachine",
    "name": "7ca911af-27e2-409b-af7f-b6293152828a",
    "status": "SUCCEEDED",
    "startDate": "2024-10-05T15:44:26.587000+09:00",
    "stopDate": "2024-10-05T15:44:32.822000+09:00",
    "input": "{\n  \"ArticleID\": \"0001\"\n}",
    "inputDetails": {
        "included": true
    },
    "output": "[{},{\"Item\":{\"EnglishVersion\":{\"S\":\"AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps.\"},\"ArticleID\":{\"S\":\"0001\"},\"Detail\":{\"S\":\"AWS Step Functions は、最新のアプリケーションにサーバーレスオーケ
ストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入
力と出力を追跡しながら集中管理できます。\"}},\"Result\":{\"SynthesisTask\":{\"CreationTime\":\"2024-10-05T06:44:27.128Z\",\"OutputFormat\":\"mp3\",\"OutputUri\":\"https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/d36d39e9-7cc4-4ce6-8152-a3ccfd2b8ade.mp3\",\"RequestCharacters\":130,\"TaskId\":\"d36d39e9-7cc4-4ce6-8152-a3ccfd2b8ade\",\"TaskStatus\":\"completed\",\"TextType\":\"text\",\"VoiceId\":\"Mizuki\"}}}]",
    "outputDetails": {
        "included": true
    },
    "redriveCount": 0,
    "redriveStatus": "NOT_REDRIVABLE",
    "redriveStatusReason": "Execution is SUCCEEDED and cannot be redriven"
}

08 音声ファイルの出力先情報を DynamoDB テーブルに格納する

ステートマシーン更新

ステートマシンの定義

コマンド
# ステートマシンの定義
DEFINITION_JSON=$(cat << EOF
{
  "Comment": "A description of my state machine",
  "StartAt": "DynamoDB GetItem",
  "States": {
    "DynamoDB GetItem": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
      "Parameters": {
        "TableName": "Article",
        "Key": {
          "ArticleID": {
            "S.$": "$.ArticleID"
          }
        }
      },
      "Next": "Choice - Item is present"
    },
    "Choice - Item is present": {
      "Type": "Choice",
      "Default": "Fail",
      "Choices": [
        {
          "Variable": "$.Item",
          "IsPresent": true,
          "Next": "Parallel"
        }
      ]
    },
    "Parallel": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "TranslateText",
          "States": {
            "TranslateText": {
              "Type": "Task",
              "Parameters": {
                "SourceLanguageCode": "ja",
                "TargetLanguageCode": "en",
                "Text.$": "$.Item.Detail.S"
              },
              "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
              "Next": "DynamoDB UpdateItem - EnglishVersion",
              "ResultPath": "$.Result"
            },
            "DynamoDB UpdateItem - EnglishVersion": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                "ExpressionAttributeValues": {
                  ":EnglishVersionRef": {
                    "S.$": "$.Result.TranslatedText"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "StartSpeechSynthesisTask",
          "States": {
            "StartSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "OutputFormat": "mp3",
                "OutputS3BucketName": "${S3_BUCKET_NAME}",
                "Text.$": "$.Item.Detail.S",
                "VoiceId": "Mizuki"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
              "ResultPath": "$.Result",
              "Next": "GetSpeechSynthesisTask"
            },
            "GetSpeechSynthesisTask": {
              "Type": "Task",
              "Parameters": {
                "TaskId.$": "$.Result.SynthesisTask.TaskId"
              },
              "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
              "ResultPath": "$.Result",
              "Next": "Choice - Task is completed"
            },
            "Choice - Task is completed": {
              "Type": "Choice",
              "Choices": [
                {
                  "Variable": "$.Result.SynthesisTask.TaskStatus",
                  "StringMatches": "completed",
                  "Next": "DynamoDB UpdateItem - mp3 URL"
                }
              ],
              "Default": "Wait"
            },
            "DynamoDB UpdateItem - mp3 URL": {
              "Type": "Task",
              "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
              "Parameters": {
                "TableName": "Article",
                "Key": {
                  "ArticleID": {
                    "S.$": "$.Item.ArticleID.S"
                  }
                },
                "UpdateExpression": "SET S3URL = :S3URLRef",
                "ExpressionAttributeValues": {
                  ":S3URLRef": {
                    "S.$": "$.Result.SynthesisTask.OutputUri"
                  }
                }
              },
              "End": true
            },
            "Wait": {
              "Type": "Wait",
              "Seconds": 5,
              "Next": "GetSpeechSynthesisTask"
            }
          }
        }
      ]
    },
    "Fail": {
      "Type": "Fail"
    }
  },
  "TimeoutSeconds": 30
}
EOF
) \
&& echo ${DEFINITION_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの定義
[cloudshell-user@ip-10-130-38-2 ~]$ DEFINITION_JSON=$(cat << EOF
> {
>   "Comment": "A description of my state machine",
>   "StartAt": "DynamoDB GetItem",
>   "States": {
>     "DynamoDB GetItem": {
>       "Type": "Task",
>       "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
>       "Parameters": {
>         "TableName": "Article",
>         "Key": {
>           "ArticleID": {
>             "S.$": "$.ArticleID"
>           }
>         }
>       },
>       "Next": "Choice - Item is present"
>     },
>     "Choice - Item is present": {
>       "Type": "Choice",
>       "Default": "Fail",
>       "Choices": [
>         {
>           "Variable": "$.Item",
>           "IsPresent": true,
>           "Next": "Parallel"
>         }
>       ]
>     },
>     "Parallel": {
>       "Type": "Parallel",
>       "End": true,
>       "Branches": [
>         {
>           "StartAt": "TranslateText",
>           "States": {
>             "TranslateText": {
>               "Type": "Task",
>               "Parameters": {
>                 "SourceLanguageCode": "ja",
>                 "TargetLanguageCode": "en",
>                 "Text.$": "$.Item.Detail.S"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
>               "Next": "DynamoDB UpdateItem - EnglishVersion",
>               "ResultPath": "$.Result"
>             },
>             "DynamoDB UpdateItem - EnglishVersion": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
>                 "ExpressionAttributeValues": {
>                   ":EnglishVersionRef": {
>                     "S.$": "$.Result.TranslatedText"
>                   }
>                 }
>               },
>               "End": true
>             }
>           }
>         },
>         {
>           "StartAt": "StartSpeechSynthesisTask",
>           "States": {
>             "StartSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "OutputFormat": "mp3",
>                 "OutputS3BucketName": "${S3_BUCKET_NAME}",
>                 "Text.$": "$.Item.Detail.S",
>                 "VoiceId": "Mizuki"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
>               "ResultPath": "$.Result",
>               "Next": "GetSpeechSynthesisTask"
>             },
>             "GetSpeechSynthesisTask": {
>               "Type": "Task",
>               "Parameters": {
>                 "TaskId.$": "$.Result.SynthesisTask.TaskId"
>               },
>               "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
>               "ResultPath": "$.Result",
>               "Next": "Choice - Task is completed"
>             },
>             "Choice - Task is completed": {
>               "Type": "Choice",
>               "Choices": [
>                 {
>                   "Variable": "$.Result.SynthesisTask.TaskStatus",
>                   "StringMatches": "completed",
>                   "Next": "DynamoDB UpdateItem - mp3 URL"
>                 }
>               ],
>               "Default": "Wait"
>             },
>             "DynamoDB UpdateItem - mp3 URL": {
>               "Type": "Task",
>               "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
>               "Parameters": {
>                 "TableName": "Article",
>                 "Key": {
>                   "ArticleID": {
>                     "S.$": "$.Item.ArticleID.S"
>                   }
>                 },
>                 "UpdateExpression": "SET S3URL = :S3URLRef",
>                 "ExpressionAttributeValues": {
>                   ":S3URLRef": {
>                     "S.$": "$.Result.SynthesisTask.OutputUri"
>                   }
>                 }
>               },
>               "End": true
>             },
>             "Wait": {
>               "Type": "Wait",
>               "Seconds": 5,
>               "Next": "GetSpeechSynthesisTask"
>             }
>           }
>         }
>       ]
>     },
>     "Fail": {
>       "Type": "Fail"
>     }
>   },
>   "TimeoutSeconds": 30
> }
> EOF
> ) \
> && echo ${DEFINITION_JSON} | python -m json.tool
{
    "Comment": "A description of my state machine",
    "StartAt": "DynamoDB GetItem",
    "States": {
        "DynamoDB GetItem": {
            "Type": "Task",
            "Resource": "arn:aws:states:::aws-sdk:dynamodb:getItem",
            "Parameters": {
                "TableName": "Article",
                "Key": {
                    "ArticleID": {
                        "S.$": "$.ArticleID"
                    }
                }
            },
            "Next": "Choice - Item is present"
        },
        "Choice - Item is present": {
            "Type": "Choice",
            "Default": "Fail",
            "Choices": [
                {
                    "Variable": "$.Item",
                    "IsPresent": true,
                    "Next": "Parallel"
                }
            ]
        },
        "Parallel": {
            "Type": "Parallel",
            "End": true,
            "Branches": [
                {
                    "StartAt": "TranslateText",
                    "States": {
                        "TranslateText": {
                            "Type": "Task",
                            "Parameters": {
                                "SourceLanguageCode": "ja",
                                "TargetLanguageCode": "en",
                                "Text.$": "$.Item.Detail.S"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:translate:translateText",
                            "Next": "DynamoDB UpdateItem - EnglishVersion",
                            "ResultPath": "$.Result"
                        },
                        "DynamoDB UpdateItem - EnglishVersion": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET EnglishVersion = :EnglishVersionRef",
                                "ExpressionAttributeValues": {
                                    ":EnglishVersionRef": {
                                        "S.$": "$.Result.TranslatedText"
                                    }
                                }
                            },
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "StartSpeechSynthesisTask",
                    "States": {
                        "StartSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "OutputFormat": "mp3",
                                "OutputS3BucketName": "h4b-stepfunctions-output-20241005",
                                "Text.$": "$.Item.Detail.S",
                                "VoiceId": "Mizuki"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:startSpeechSynthesisTask",
                            "ResultPath": "$.Result",
                            "Next": "GetSpeechSynthesisTask"
                        },
                        "GetSpeechSynthesisTask": {
                            "Type": "Task",
                            "Parameters": {
                                "TaskId.$": "$.Result.SynthesisTask.TaskId"
                            },
                            "Resource": "arn:aws:states:::aws-sdk:polly:getSpeechSynthesisTask",
                            "ResultPath": "$.Result",
                            "Next": "Choice - Task is completed"
                        },
                        "Choice - Task is completed": {
                            "Type": "Choice",
                            "Choices": [
                                {
                                    "Variable": "$.Result.SynthesisTask.TaskStatus",
                                    "StringMatches": "completed",
                                    "Next": "DynamoDB UpdateItem - mp3 URL"
                                }
                            ],
                            "Default": "Wait"
                        },
                        "DynamoDB UpdateItem - mp3 URL": {
                            "Type": "Task",
                            "Resource": "arn:aws:states:::aws-sdk:dynamodb:updateItem",
                            "Parameters": {
                                "TableName": "Article",
                                "Key": {
                                    "ArticleID": {
                                        "S.$": "$.Item.ArticleID.S"
                                    }
                                },
                                "UpdateExpression": "SET S3URL = :S3URLRef",
                                "ExpressionAttributeValues": {
                                    ":S3URLRef": {
                                        "S.$": "$.Result.SynthesisTask.OutputUri"
                                    }
                                }
                            },
                            "End": true
                        },
                        "Wait": {
                            "Type": "Wait",
                            "Seconds": 5,
                            "Next": "GetSpeechSynthesisTask"
                        }
                    }
                }
            ]
        },
        "Fail": {
            "Type": "Fail"
        }
    },
    "TimeoutSeconds": 30
}

ステートマシーン更新

コマンド
# ステートマシーン更新
aws stepfunctions update-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --definition "${DEFINITION_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーン更新
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions update-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --definition "${DEFINITION_JSON}"
{
    "updateDate": "2024-10-05T15:49:53.077000+09:00",
    "revisionId": "bcf07b3d-41a5-4b19-a89e-187d330b5722"
}

実行

インプットJSON

コマンド
# インプットJSON
INPUT_JSON=$(cat << EOF
{
  "ArticleID": "0001"
}
EOF
) \
&& echo ${INPUT_JSON} | python -m json.tool

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # インプットJSON
[cloudshell-user@ip-10-130-38-2 ~]$ INPUT_JSON=$(cat << EOF
> {
>   "ArticleID": "0001"
> }
> EOF
> ) \
> && echo ${INPUT_JSON} | python -m json.tool
{
    "ArticleID": "0001"
}

ステートマシンの実行

コマンド
# ステートマシンの実行
aws stepfunctions start-execution \
    --state-machine-arn ${STATE_MACHINE_ARN} \
    --input "${INPUT_JSON}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシンの実行
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions start-execution \
>     --state-machine-arn ${STATE_MACHINE_ARN} \
>     --input "${INPUT_JSON}"
{
    "executionArn": "arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:2321fa41-1189-4f08-8757-a99afc8a405d",
    "startDate": "2024-10-05T15:51:24.977000+09:00"
}

最新の実行ARNを取得

コマンド
# 最新の実行ARNを取得
LATEST_EXECUTION_ARN=$(
    aws stepfunctions list-executions \
        --state-machine-arn ${STATE_MACHINE_ARN} \
        --max-results 1 \
        --query "executions[0].executionArn" \
        --output text
) \
&& echo ${LATEST_EXECUTION_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # 最新の実行ARNを取得
[cloudshell-user@ip-10-130-38-2 ~]$ LATEST_EXECUTION_ARN=$(
>     aws stepfunctions list-executions \
>         --state-machine-arn ${STATE_MACHINE_ARN} \
>         --max-results 1 \
>         --query "executions[0].executionArn" \
>         --output text
> ) \
> && echo ${LATEST_EXECUTION_ARN}
arn:aws:states:ap-northeast-1:999999999999:execution:H4BStateMachine:2321fa41-1189-4f08-8757-a99afc8a405d

DynamoDBテーブル項目取得

コマンド
# キー指定
TABLE_KEY=$(cat << EOF
{
    "${PARTITION_KEY}": {"S": "0001"}
}
EOF
) \
&& echo ${TABLE_KEY} | python -m json.tool

# 項目取得
aws dynamodb get-item \
    --table-name ${DYNAMODB_TABLE_NAME} \
    --key "${TABLE_KEY}"

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # キー指定
[cloudshell-user@ip-10-130-38-2 ~]$ TABLE_KEY=$(cat << EOF
> {
>     "${PARTITION_KEY}": {"S": "0001"}
> }
> EOF
> ) \
> && echo ${TABLE_KEY} | python -m json.tool
{
    "ArticleID": {
        "S": "0001"
    }
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # 項目取得
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb get-item \
>     --table-name ${DYNAMODB_TABLE_NAME} \
>     --key "${TABLE_KEY}"
{
    "Item": {
        "EnglishVersion": {
            "S": "AWS Step Functions provides serverless orchestration for modern applications. Orchestration enables centralized management of a workflow divided into multiple steps and flow logic added while tracking input and output between steps."
        },
        "S3URL": {
            "S": "https://s3.ap-northeast-1.amazonaws.com/h4b-stepfunctions-output-20241005/7866a771-c7c7-4791-a098-e4a5fe11405f.mp3"
        },
        "ArticleID": {
            "S": "0001"
        },
        "Detail": {
            "S": "AWS Step Functions は、最新のアプリケーションにサーバーレスオーケストレーションを提供します。オーケストレーションにより、複数のステップに分割されフローロジックが追加されたワークフローを、ステップ間の入力と出力を追跡しながら集中管理できます。"
        }
    }
}

S3バケット内オブジェクト確認

コマンド
# S3バケット内オブジェクト確認 (最新のオブジェクト)
aws s3api list-object-versions \
    --bucket ${S3_BUCKET_NAME} \
    --query 'Versions | sort_by(@, &LastModified)[-1]' \
    --output json

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # S3バケット内オブジェクト確認
[cloudshell-user@ip-10-130-38-2 ~]$ aws s3api list-object-versions \
>     --bucket ${S3_BUCKET_NAME} \
>     --query 'Versions | sort_by(@, &LastModified)[-1]' \
>     --output json
{
    "ETag": "\"680ea0a65a4d0f0473fce0c836707788\"",
    "Size": 120887,
    "StorageClass": "STANDARD",
    "Key": "7866a771-c7c7-4791-a098-e4a5fe11405f.mp3",
    "VersionId": "null",
    "IsLatest": true,
    "LastModified": "2024-10-05T06:51:29+00:00",
    "Owner": {
        "DisplayName": "user01",
        "ID": "974cd83317461904fcbad632a42045ad8cd85cb24065c65f2ca62b7e35d84078"
    }
}

09 リソースの削除 + まとめ + Next Action 案

ステートマシーン削除

コマンド
# ステートマシーン削除
aws stepfunctions delete-state-machine \
    --state-machine-arn ${STATE_MACHINE_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ステートマシーンを削除
[cloudshell-user@ip-10-130-38-2 ~]$ aws stepfunctions delete-state-machine \
>     --state-machine-arn ${STATE_MACHINE_ARN}

DynamoDBテーブル削除

コマンド
# DynamoDBテーブル削除
aws dynamodb delete-table \
    --table-name ${DYNAMODB_TABLE_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # DynamoDBテーブル削除
[cloudshell-user@ip-10-130-38-2 ~]$ aws dynamodb delete-table \
>     --table-name ${DYNAMODB_TABLE_NAME}
{
    "TableDescription": {
        "TableName": "Article",
        "TableStatus": "DELETING",
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ap-northeast-1:999999999999:table/Article",
        "TableId": "1c4d1b92-1d33-4166-8ab9-9950e54fa0d6",
        "DeletionProtectionEnabled": false
    }
}

S3バケット削除

コマンド
# オブジェクト一覧取得
OBJECT_LIST=$(
    aws s3api list-object-versions \
        --bucket ${S3_BUCKET_NAME} \
        --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}'
) \
&& echo ${OBJECT_LIST}

# JSONフォーマットの確認
echo ${OBJECT_LIST} | python -m json.tool

# バケットを空にする
aws s3api delete-objects \
    --bucket ${S3_BUCKET_NAME} \
    --delete "${OBJECT_LIST}"

# S3バケットを削除
aws s3api delete-bucket \
    --bucket ${S3_BUCKET_NAME}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # オブジェクト一覧取得
[cloudshell-user@ip-10-130-38-2 ~]$ OBJECT_LIST=$(
>     aws s3api list-object-versions \
>         --bucket ${S3_BUCKET_NAME} \
>         --query='{Objects: Versions[].{Key:Key,VersionId:VersionId}}'
> ) \
> && echo ${OBJECT_LIST}
{ "Objects": [ { "Key": "483e68f0-d557-4464-8607-1a0fc351c5a6.mp3", "VersionId": "null" }, { "Key": "5dd55cd5-ab58-407f-b2e6-5bada047d7ea.mp3", "VersionId": "null" }, { "Key": "6dc0d67f-cc22-4cf8-9bdc-adb846a9a865.mp3", "VersionId": "null" }, { "Key": "7866a771-c7c7-4791-a098-e4a5fe11405f.mp3", "VersionId": "null" }, { "Key": "d36d39e9-7cc4-4ce6-8152-a3ccfd2b8ade.mp3", "VersionId": "null" } ] }
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # JSONフォーマットの確認
[cloudshell-user@ip-10-130-38-2 ~]$ echo ${OBJECT_LIST} | python -m json.tool
{
    "Objects": [
        {
            "Key": "483e68f0-d557-4464-8607-1a0fc351c5a6.mp3",
            "VersionId": "null"
        },
        {
            "Key": "5dd55cd5-ab58-407f-b2e6-5bada047d7ea.mp3",
            "VersionId": "null"
        },
        {
            "Key": "6dc0d67f-cc22-4cf8-9bdc-adb846a9a865.mp3",
            "VersionId": "null"
        },
        {
            "Key": "7866a771-c7c7-4791-a098-e4a5fe11405f.mp3",
            "VersionId": "null"
        },
        {
            "Key": "d36d39e9-7cc4-4ce6-8152-a3ccfd2b8ade.mp3",
            "VersionId": "null"
        }
    ]
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # バケットを空にする
[cloudshell-user@ip-10-130-38-2 ~]$ aws s3api delete-objects \
>     --bucket ${S3_BUCKET_NAME} \
>     --delete "${OBJECT_LIST}"
{
    "Deleted": [
        {
            "Key": "5dd55cd5-ab58-407f-b2e6-5bada047d7ea.mp3",
            "VersionId": "null"
        },
        {
            "Key": "6dc0d67f-cc22-4cf8-9bdc-adb846a9a865.mp3",
            "VersionId": "null"
        },
        {
            "Key": "483e68f0-d557-4464-8607-1a0fc351c5a6.mp3",
            "VersionId": "null"
        },
        {
            "Key": "d36d39e9-7cc4-4ce6-8152-a3ccfd2b8ade.mp3",
            "VersionId": "null"
        },
        {
            "Key": "7866a771-c7c7-4791-a098-e4a5fe11405f.mp3",
            "VersionId": "null"
        }
    ]
}
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # S3バケットを削除
[cloudshell-user@ip-10-130-38-2 ~]$ aws s3api delete-bucket \
>     --bucket ${S3_BUCKET_NAME}

IAM関連削除

コマンド
# ロールにアタッチされているポリシーをリスト
POLICIES=$(
    aws iam list-attached-role-policies \
        --role-name ${IAM_ROLE_NAME} \
        --query 'AttachedPolicies[*].PolicyArn' \
        --output text
) \
&& echo ${POLICIES}

# リスト内のポリシーをデタッチする
for POLICY in ${POLICIES}; do
    aws iam detach-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-arn ${POLICY}
done

# IAMロールの削除
aws iam delete-role \
    --role-name ${IAM_ROLE_NAME}

# IAMポリシーの削除
aws iam delete-policy \
    --policy-arn ${IAM_POLICY_ARN}

出力
[cloudshell-user@ip-10-130-38-2 ~]$ # ロールにアタッチされているポリシーをリスト
[cloudshell-user@ip-10-130-38-2 ~]$ POLICIES=$(
>     aws iam list-attached-role-policies \
>         --role-name ${IAM_ROLE_NAME} \
>         --query 'AttachedPolicies[*].PolicyArn' \
>         --output text
> ) \
> && echo ${POLICIES}
arn:aws:iam::999999999999:policy/service-role/XRayAccessPolicy-H4BStateMachine arn:aws:iam::aws:policy/TranslateReadOnly arn:aws:iam::aws:policy/AmazonPollyFullAccess arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess arn:aws:iam::aws:policy/AmazonS3FullAccess
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # リスト内のポリシーをデタッチする
[cloudshell-user@ip-10-130-38-2 ~]$ for POLICY in ${POLICIES}; do
>     aws iam detach-role-policy \
>         --role-name ${IAM_ROLE_NAME} \
>         --policy-arn ${POLICY}
> done
[cloudshell-user@ip-10-130-38-2 ~]$ 
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMロールの削除
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam delete-role \
>     --role-name ${IAM_ROLE_NAME}
[cloudshell-user@ip-10-130-38-2 ~]$
[cloudshell-user@ip-10-130-38-2 ~]$ # IAMポリシーの削除
[cloudshell-user@ip-10-130-38-2 ~]$ aws iam delete-policy \
>     --policy-arn ${IAM_POLICY_ARN}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?