

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Step Functions の Versions and Aliases による継続的デプロイの管理
<a name="concepts-cd-aliasing-versioning"></a>

Step Functions を使用して、ステートマシンの*バージョニング*と*エイリアス*を通じてワークフローの継続的なデプロイを管理できます。*バージョン*とは、実行可能なステートマシンの番号が付けられた変更不可能なスナップショットです。*エイリアス*とはステートマシンの最大 2 つのバージョンを示すポインタです。

ステートマシンの複数のバージョンを管理し、プロダクションワークフローへのデプロイを管理できます。エイリアスを使用すると、異なるワークフローバージョン間でトラフィックをルーティングし、それらのワークフローを実稼働環境に段階的にデプロイできます。

さらに、バージョンまたはエイリアスを使用してステートマシンの実行を開始できます。ステートマシンの実行を開始するときにバージョンまたはエイリアスを使用しない場合、Step Functions はステートマシン定義の最新リビジョンを使用します。

**ステートマシンのリビジョン**  
ステートマシンは 1 つまたは複数のリビジョンを持つことができます。[UpdateStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html) API アクションを使用してステートマシンを更新すると、新しいステートマシンリビジョンが作成されます。*リビジョン*は、ステートマシンの定義と設定の不変で読み取り専用のスナップショットです。ステートマシンの実行をリビジョンから開始することはできず、リビジョンには ARN がありません。リビジョンには、共通の一意の識別子 (UUID) である、`revisionId` があります。

**Topics**
+ [バージョン](concepts-state-machine-version.md)
+ [Aliases](concepts-state-machine-alias.md)
+ [Versions and Aliases の承認](auth-version-alias.md)
+ [バージョンまたはエイリアスへの実行の関連付け](execution-alias-version-associate.md)
+ [デプロイの例](example-alias-version-deployment.md)
+ [バージョンの段階的なデプロイ](version-rolling-deployment.md)

# Step Functions ワークフローのステートマシンバージョン
<a name="concepts-state-machine-version"></a>

*バージョン*とは、ステートマシンの番号が付けられた**変更不可能な**スナップショットです。そのステートマシンに作成された最新リビジョンのバージョンを公開します。各バージョンには、ステートマシン ARN とコロン (:) で区切られたバージョン番号を組み合わせた一意の Amazon リソースネーム (ARN) があります。次の例は、ステートマシンのバージョン ARN の形式を示しています。

```
arn:partition:states:region:account-id:stateMachine:myStateMachine:1
```

ステートマシンのバージョンの使用を開始するには、最初のバージョンを公開する必要があります。バージョンを公開した後は、バージョン ARN を使用して [StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html) API アクションを呼び出すことができます。バージョンを編集することはできませんが、ステートマシンを更新して新しいバージョンを公開することができます。ステートマシンの複数のバージョンを公開することもできます。

![\[バージョンがステートマシンの変更不可能なスナップショットであることを示すあいまいな説明図。\]](http://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/images/versioning-concept.png)


ステートマシンの新しいバージョンを公開すると、Step Functions によってバージョン番号が割り当てられます。バージョン番号は 1 から始まり、新しいバージョンごとに単純に増えていきます。バージョン番号は特定のステートマシンでは再利用されません。ステートマシンのバージョン 10 を削除して新しいバージョンを公開する場合、Step Functions はそれをバージョン 11 として公開します。

以下のプロパティはステートマシンのすべてのバージョンで同じになります。
+ ステートマシンのすべてのバージョンは同じタイプ [(標準または Express)](choosing-workflow-type.md) を共有します。
+ ステートマシンの名前または作成日をバージョン間で変更することはできません。
+ タグはステートマシンにグローバルに適用されます。[TagResource](https://docs.aws.amazon.com/step-functions/latest/apireference/API_TagResource.html) と [UntagResource](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UntagResource.html) の API アクションを使用して、ステートマシンのタグを管理できます。

ステートマシンには各バージョンと [revision](concepts-cd-aliasing-versioning.md#statemachinerev) に含まれるプロパティも含まれますが、これらのプロパティは 2 つのバージョンまたはリビジョンによって異なる場合があります。これらのプロパティには、[ステートマシン定義](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html#StepFunctions-UpdateStateMachine-request-definition)、[IAM ロール](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html#StepFunctions-UpdateStateMachine-request-roleArn)、[トレーシング設定](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html#StepFunctions-UpdateStateMachine-request-tracingConfiguration)、[ログ記録設定](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html#StepFunctions-UpdateStateMachine-request-loggingConfiguration)が含まれます。

## ステートマシンのバージョンの公開 (コンソール)
<a name="procedure-create-versions"></a>

ステートマシンのバージョンは最大 1000 個公開できます。このソフトリミットの引き上げをリクエストするには、[AWS マネジメントコンソール](https://docs.aws.amazon.com/servicequotas/latest/userguide/request-quota-increase.html) の **[サポートセンター]** ページを使用してください。未使用のバージョンはコンソールから手動で削除するか、[DeleteStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineVersion.html) API アクションを呼び出して削除できます。

**ステートマシンのバージョンを公開するには**

1. [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)を開き、既存のステートマシンを選択します。

1. **[ステートマシンの詳細]** ページで、**[編集]** を選択します。

1. 必要に応じてステートマシン定義を編集し、**[保存]** を選択します。

1. [**Publish version**] を選択します。

1. (オプション) 表示されたダイアログボックスの **[説明]** フィールドに、ステートマシンのバージョンに関する簡単な説明を入力します。

1. **[公開]** を選択します。

**注記**  
ステートマシンの新しいバージョンを公開すると、Step Functions によってバージョン番号が割り当てられます。バージョン番号は 1 から始まり、新しいバージョンごとに単純に増えていきます。バージョン番号は特定のステートマシンでは再利用されません。ステートマシンのバージョン 10 を削除して新しいバージョンを公開する場合、Step Functions はそれをバージョン 11 として公開します。

## Step Functions API 操作によるバージョンの管理
<a name="manage-versions-with-api"></a>

Step Functions には、ステートマシンのバージョンを公開および管理するための次の API オペレーションが用意されています。
+ [PublishStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_PublishStateMachineVersion.html) - ステートマシンの現在の [revision](concepts-cd-aliasing-versioning.md#statemachinerev) からバージョンを公開します。
+ [UpdateStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html) - 同じリクエストでステートマシンを更新して `publish` パラメータを `true` に設定した場合、新しいステートマシンバージョンが公開されます。
+ [CreateStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachine.html) - `publish` パラメータを `true` に設定した場合、ステートマシンの最初のリビジョンが公開されます。
+ [ListStateMachineVersions](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListStateMachineVersions.html) - 指定されたステートマシン ARN のバージョンを一覧表示します。
+ [DescribeStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeStateMachine.html) - `stateMachineArn` で指定されているバージョン ARN のステートマシンバージョン詳細を返します。
+ [DeleteStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineVersion.html) - ステートマシンのバージョンを削除します。

`myStateMachine` を使用して と呼ばれるステートマシンの現在のリビジョンから新しいバージョンを発行するには AWS Command Line Interface、 `publish-state-machine-version` コマンドを使用します。

```
aws stepfunctions publish-state-machine-version --state-machine-arn arn:aws:states:region:account-id:stateMachine:myStateMachine
```

レスポンスは `stateMachineVersionArn` を返します。例えば、前のコマンドは `arn:aws:states:region:account-id:stateMachine:myStateMachine:1` のレスポンスを返します。

**注記**  
ステートマシンの新しいバージョンを公開すると、Step Functions によってバージョン番号が割り当てられます。バージョン番号は 1 から始まり、新しいバージョンごとに単純に増えていきます。バージョン番号は特定のステートマシンでは再利用されません。ステートマシンのバージョン 10 を削除して新しいバージョンを公開する場合、Step Functions はそれをバージョン 11 として公開します。

## コンソールからのステートマシンバージョンの実行
<a name="procedure-run-version"></a>

ステートマシンバージョンの使用を開始するには、まず現在のステートマシン [revision](concepts-cd-aliasing-versioning.md#statemachinerev) からバージョンを公開する必要があります。バージョンを公開するには、Step Functions コンソールを使用するか、[PublishStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_PublishStateMachineVersion.html) API アクションを呼び出します。`publish` というオプションパラメータを使用して [UpdateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachineAlias.html) API アクションを呼び出し、ステートマシンを更新してそのバージョンを公開することもできます。

コンソールを使用するか、[StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html) API アクションを呼び出してバージョン ARN を指定することにより、バージョンの実行を開始できます。[エイリアス](concepts-state-machine-alias.md)を使用してバージョンの実行を開始することもできます。エイリアスは[ルーティング設定](concepts-state-machine-alias.md#alias-routing-config)に基づいて、トラフィックを特定のバージョンにルーティングします。

バージョンを使用せずにステートマシンの実行を開始すると、Step Functions はステートマシンの最新リビジョンを使用して実行します。Step Functions が実行をバージョンに関連付ける方法については、「[バージョンまたはエイリアスへの実行の関連付け](execution-alias-version-associate.md)」を参照してください。

**ステートマシンバージョンを使用して実行を開始するには**

1. [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)を開き、1 つ以上のバージョンを公開している既存のステートマシンを選択します。バージョンを公開する方法については、「[ステートマシンのバージョンの公開 (コンソール)](#procedure-create-versions)」を参照してください。

1. **[ステートマシンの詳細]**」ページで **[バージョン]** タブを選択します。

1. **[バージョン]** セクションで、次の操作を行います。

   1. 実行の開始に使用するバージョンを選択します。

   1. **[実行のスタート]** を選択します。

1. (オプション) **[実行を開始]** ダイアログボックスで、実行の名前を入力します。

1. (オプション) 必要に応じて実行の入力を指定し、**[実行を開始]** を選択します。

# Step Functions ワークフローのステートマシンエイリアス
<a name="concepts-state-machine-alias"></a>

*エイリアス*は、同じステートマシンの最大 2 つのバージョンを示すポインタです。ステートマシンには複数のエイリアスを作成できます。各エイリアスには固有の Amazon リソースネーム (ARN) があります。エイリアス ARN は、ステートマシンの ARN とエイリアス名をコロン (:) で区切ったものです。次の例は、ステートマシンのエイリアス ARN の形式を示しています。

```
arn:partition:states:region:account-id:stateMachine:myStateMachine:aliasName
```

エイリアスを使用すると、2 つのステートマシンバージョンのいずれかの間で[トラフィックをルーティング](#alias-routing-config)できます。1 つのバージョンを指すエイリアスを作成することもできます。エイリアスはステートマシンのバージョンのみを指すことができます。エイリアスを使って別のエイリアスを指すことはできません。ステートマシンの別のバージョンを指すよう、エイリアスを更新することもできます。

![\[エイリアスが v1 にリクエストの 80%、v2 に 20% を送信することを示す図。\]](http://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/images/aliases-concept.png)


**Topics**
+ [ステートマシンエイリアスの作成 (コンソール)](#procedure-create-aliases)
+ [API を使用したエイリアスの管理](#manage-aliases-with-api)
+ [エイリアスのルーティング設定](#alias-routing-config)
+ [エイリアスを使用したステートマシンの実行 (コンソール)](#procedure-run-exec-with-alias)

## ステートマシンエイリアスの作成 (コンソール)
<a name="procedure-create-aliases"></a>

Step Functions コンソールを使用するか、[CreateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachineAlias.html) API アクションを呼び出すと、ステートマシンごとに最大 100 個のエイリアスを作成できます。このソフトリミットの引き上げをリクエストするには、[AWS マネジメントコンソール](https://docs.aws.amazon.com/servicequotas/latest/userguide/request-quota-increase.html) の **[サポートセンター]** ページを使用してください。未使用のエイリアスをコンソールから削除するか、[DeleteStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineAlias.html) API アクションを呼び出してください。

**ステートマシンエイリアスを作成するには**

1. [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)を開き、既存のステートマシンを選択します。

1. **[ステートマシンの詳細]**」ページで **[エイリアス]** タブを選択します。

1. **[新しいエイリアスの作成]** を選択します。

1. **[Create alias (エイリアスの作成)] ページ**で、以下の操作を行います。

   1. **[エイリアス名]** を入力します。

   1. (オプション) [**Description (説明)**] で、エイリアスの説明を入力します。

1. エイリアスのルーティングを設定するには、「[エイリアスのルーティング設定](#alias-routing-config)」を参照してください。

1. **[エイリアスを作成]** を選択します。

## Step Functions API 操作によるエイリアスの管理
<a name="manage-aliases-with-api"></a>

Step Functions では、ステートマシンエイリアスの作成と管理やエイリアスに関する情報の取得に使用できる、以下の API 操作を使用できます。
+ [CreateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachineAlias.html) - ステートマシンのエイリアスを作成します。
+ [DescribeStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeStateMachineAlias.html) - ステートマシンエイリアスに関する詳細を返します。
+ [ListStateMachineAliases](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListStateMachineAliases.html) - 指定されたステートマシン ARN のエイリアスを一覧表示します。
+ [UpdateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachineAlias.html) - `description` または `routingConfiguration` を変更することにより、既存のステートマシンエイリアスの設定を更新します。
+ [DeleteStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineAlias.html) – ステートマシンのエイリアスを削除します。

`myStateMachine` を使用して という名前のステートマシンのバージョン 1 `PROD`を指す という名前のエイリアスを作成するには AWS Command Line Interface、 `create-state-machine-alias` コマンドを使用します。

```
aws stepfunctions create-state-machine-alias --name PROD  --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":100}]"
```

## エイリアスのルーティング設定
<a name="alias-routing-config"></a>

エイリアスを使用すると、ステートマシンの 2 つのバージョンの間で実行トラフィックをルーティングできます。例えば、ステートマシンの新しいバージョンを起動するとします。エイリアスにルーティングを設定すると、新しいバージョンのデプロイに伴うリスクを軽減できます。ルーティングを設定すると、ほとんどのトラフィックを以前のテスト済みバージョンのステートマシンに送信できます。これで、新しいバージョンが安全にロールフォワードされることが確認できるまで、新しいバージョンが受け取るトラフィックの割合が少なくなります。

ルーティング設定を定義するには、エイリアスが指すステートマシンの両方のバージョン必ず公開してください。エイリアスから実行を開始すると、Step Functions は、ルーティング設定で指定されたバージョンから実行するステートマシンのバージョンをランダムに選択します。この選択は、エイリアスルーティング設定の各バージョンに割り当てられたトラフィックの割合に基づいて行われます。



**エイリアスのルーティング設定を構成するには**
+ **[エイリアスの作成]** ページの **[ルーティングの設定]**」で、次の操作を行います。

  1. **[バージョン]** では、エイリアスが指す最初のステートマシンのバージョンを選択します。

  1. **[2 つのバージョン間でトラフィックを分割します]** チェックボックスを選択します。
**ヒント**  
1 つのバージョンを指定するには、**[2 つのバージョン間でトラフィックを分割します]** チェックボックスをオフにします。

  1. **[バージョン]** では、エイリアスが指す必要のある 2 番目のバージョンを選択します。

  1. **[トラフィックの割合]** フィールドに、各バージョンにルーティングするトラフィックの割合を指定します。例えば、実行トラフィックの 60 パーセントを最初のバージョンにルーティングして、40 パーセントのトラフィックを 2 番目のバージョンにルーティングするには、**60** と **40** を入力します。

     トラフィックの割合の合計は 100% になる必要があります。

## エイリアスを使用したステートマシンの実行 (コンソール)
<a name="procedure-run-exec-with-alias"></a>

ステートマシンの実行は、コンソールからエイリアスを使用して開始することも、エイリアスの ARN を指定して [StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html) API アクションを呼び出すことによって開始することもできます。次に、Step Functions はエイリアスで指定されたバージョンを実行します。ステートマシンの実行を開始するときにバージョンまたはエイリアスを指定していない場合、Step Functions はデフォルトで最新のリビジョンを使用します。

**エイリアスを使用してステートマシンの実行を開始するには**

1. [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)を開き、エイリアスを作成した既存のステートマシンを選択します。エイリアスの作成の詳細については、「[ステートマシンエイリアスの作成 (コンソール)](#procedure-create-aliases)」を参照してください。

1. **[ステートマシンの詳細]**」ページで **[エイリアス]** タブを選択します。

1. **[エイリアス]** セクションで、次の操作を行います。

   1. 実行の開始に使用するエイリアスを選択します。

   1. **[実行のスタート]** を選択します。

1. (オプション) **[実行を開始]** ダイアログボックスで、実行の名前を入力します。

1. 必要に応じて実行の入力を指定し、**[実行を開始]** を選択します。

# Step Functions ワークフローでの Versions and Aliases の承認
<a name="auth-version-alias"></a>

バージョンまたはエイリアスを使用して Step Functions API アクションを呼び出すには、適切なアクセス許可が必要です。バージョンまたはエイリアスに API アクションを呼び出す権限を与えるために、Step Functions はバージョン ARN またはエイリアス ARN の代わりにステートマシンの ARN を使用します。特定のバージョンまたはエイリアスの権限の範囲を制限することもできます。詳細については、「[アクセス許可を制限する](#auth-scope-permission-version-alias)」を参照してください。

次の `myStateMachine` という名前の IAM ポリシー例を使用し [CreateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachineAlias.html) API アクションを呼び出して、ステートマシンエイリアスを作成できます。

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "states:CreateStateMachineAlias",
      "Resource": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine"
    }
  ]
}
```

ステートマシンのバージョンまたはエイリアスを使用して、API アクションへのアクセスを許可または拒否するアクセス許可を設定する場合は、次の点を考慮してください。
+ [CreateStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachine.html) API アクションおよび [UpdateStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachine.html) API アクションの `publish` パラメータを使用して新しいステートマシンのバージョンをパブリッシュする場合は、[PublishStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_PublishStateMachineVersion.html) API アクションの `ALLOW` のアクセス許可も必要です。
+ [DeleteStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachine.html) API アクションは、ステートマシンに関連付けられているすべてのバージョニングとエイリアスを削除します。

## バージョンまたはエイリアスのアクセス許可の範囲を制限する
<a name="auth-scope-permission-version-alias"></a>

修飾子を使用して、バージョンまたはエイリアスに必要なアクセス許可の権限をさらに制限できます。修飾子は、バージョン番号またはエイリアス名を指します。修飾子を使用して、ステートマシンを修飾します。次の例は、修飾子として `PROD` という名前のエイリアスを使用するステートマシン ARN です。

```
arn:aws:states:region:account-id:stateMachine:myStateMachine:PROD
```

修飾 ARN と非修飾 ARN の詳細については、「[バージョンまたはエイリアスへの実行の関連付け](execution-alias-version-associate.md)」を参照してください。

IAM ポリシーの `Condition` ステートメントで、`states:StateMachineQualifier` という名前のオプションのコンテキストキーを使用して、アクセス許可の範囲を制限します。例えば、`myStateMachine` という名前のステートマシンの次の IAM ポリシーは、`PROD` またはバージョン `1` という名前のエイリアスを使って、[DescribeStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeStateMachine.html) API アクションを呼び出すアクセスを拒否します。

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Deny",
      "Action": "states:DescribeStateMachine",
      "Resource": "arn:aws:states:us-east-1:123456789012:stateMachine:myStateMachine",
      "Condition": {
        "ForAnyValue:StringEquals": {
          "states:StateMachineQualifier": [
            "PROD",
            "1"
          ]
        }
      }
    }
  ]
}
```

次のリストでは、`StateMachineQualifier` コンテキストキーを使用して、アクセス許可の範囲を制限できる API アクションを指定しています。
+ [CreateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_CreateStateMachineAlias.html)
+ [DeleteStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineAlias.html)
+ [DeleteStateMachineVersion](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DeleteStateMachineVersion.html)
+ [DescribeStateMachine](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeStateMachine.html)
+ [DescribeStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeStateMachineAlias.html)
+ [ListExecutions](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListExecutions.html)
+ [ListStateMachineAliases](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListStateMachineAliases.html)
+ [StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html)
+ [StartSyncExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartSyncExecution.html)
+ [UpdateStateMachineAlias](https://docs.aws.amazon.com/step-functions/latest/apireference/API_UpdateStateMachineAlias.html)

# Step Functions が実行をバージョンまたはエイリアスに関連付ける方法
<a name="execution-alias-version-associate"></a>

Step Functions は、[StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html) API アクションの呼び出しに使用する Amazon リソースネーム (ARN) に基づいて、実行をバージョンまたはエイリアスに関連付けます。Step Functions は、実行開始時にこのアクションを実行します。

修飾 ARN または非修飾 ARN を使用して、ステートマシンの実行を開始できます。
+ **修飾 ARN** - バージョン番号またはエイリアス名が末尾に付加されたステートマシン ARN を指します。

  修飾 ARN の以下の例は、`myStateMachine` というステートマシンのバージョン `3` を表しています。

  ```
  arn:aws:states:region:account-id:stateMachine:myStateMachine:3
  ```

  修飾 ARN の以下の例は、`myStateMachine` というステートマシンの `PROD` というエイリアスを表しています。

  ```
  arn:aws:states:region:account-id:stateMachine:myStateMachine:PROD
  ```
+ **非修飾 ARN** - バージョン番号やエイリアス名が末尾に付加されていないステートマシン ARN を指します。

  ```
  arn:aws:states:region:account-id:stateMachine:myStateMachine
  ```

例えば、修飾 ARN がバージョン `3` を参照している場合、Step Functions は実行をこのバージョンに関連付けます。実行は、バージョン `3` を指すエイリアスには関連付けられません。

修飾 ARN がエイリアスを参照している場合、Step Functions は実行をそのエイリアスとエイリアスが指すバージョンに関連付けます。1 つの実行は、1 つのエイリアスにのみ関連付けることができます。

**注記**  
修飾 ARN を使用して実行を開始した場合、そのバージョンが同じステートマシン [revision](concepts-cd-aliasing-versioning.md#statemachinerev) を使用していても、Step Functions はその実行をバージョンに関連付けません。例えば、バージョン 3 が最新リビジョンを使用しているが非修飾 ARN で実行を開始した場合、Step Functions はその実行をバージョン 3 に関連付けません。

## 特定のバージョンまたはエイリアスで開始された実行の表示
<a name="view-version-alias-executions"></a>

Step Functions では、バージョンまたはエイリアスで開始された実行を以下の方法で表示できます。

### API アクションの使用
<a name="view-executions-api-actions"></a>

[DescribeExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_DescribeExecution.html) と [ListExecutions](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListExecutions.html) の API アクションを呼び出すことにより、バージョンまたはエイリアスに関連付けられているすべての実行を表示できます。これらの API アクションは、実行を開始するために使用されたバージョンまたはエイリアスの ARN を返します。これらのアクションは、実行のステータスや ARN など、その他の詳細も返します。

ステートマシンのエイリアス ARN またはバージョン ARN を指定して、特定のエイリアスまたはバージョンに関連する実行を一覧表示することもできます。

[ListExecutions](https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListExecutions.html) API アクションの、以下のレスポンス例は、ステートマシンの実行を開始するために使用される *myFirstExecution* という名前のエイリアスの ARN を示しています。

次のコードスニペットの*斜体で示されている*テキストは、リソース固有の情報を表しています。

```
{
    "executions": [
        {
            "executionArn": "arn:aws:states:region:account-id:execution:myStateMachine:myFirstExecution",
            "stateMachineArn": "arn:aws:states:region:account-id:stateMachine:myStateMachine",
            "stateMachineAliasArn": "arn:aws:states:region:account-id:stateMachine:myStateMachine:PROD",
            "name": "myFirstExecution",
            "status": "SUCCEEDED",
            "startDate": "2023-04-20T23:07:09.477000+00:00",
            "stopDate": "2023-04-20T23:07:09.732000+00:00"
        }
    ]
}
```

### Step Functions コンソールの使用
<a name="view-executions-console"></a>

[Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)から、バージョンまたはエイリアスによって開始された実行を確認することもできます。以下の手順は、特定のバージョンで開始された実行を表示する方法を示しています。

1. [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/)を開き、[バージョン](concepts-state-machine-version.md#procedure-create-versions)を公開したか[エイリアス](concepts-state-machine-alias.md#procedure-create-aliases)を作成した既存のステートマシンを選択します。この例は、特定のステートマシンバージョンで開始された実行を表示する方法を示しています。

1. **[バージョン]** タブを選択し、**[バージョン]** リストからバージョンを選択します。
**ヒント**  
プロパティまたは値ボックスでフィルタリングして、特定のバージョンを検索します。

1. *[バージョンの詳細]* ページには、選択したバージョンで開始された進行中および過去のステートマシン実行のリストが表示されます。

以下のイメージは、*[バージョンの詳細]* コンソールページを示しています。このページには、`MathAddDemo` というステートマシンのバージョン *4* によって開始された実行が一覧表示されます。このリストには、`PROD` というエイリアスによって開始された実行も表示されます。このエイリアスは実行トラフィックをバージョン *4* にルーティングしました。

![\[ステートマシンバージョンの [詳細] コンソールページを示すスクリーンショットの例\]](http://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/images/view-version-executions.png)


### CloudWatch メトリクスの使用
<a name="view-executions-metrics"></a>

[Qualified ARN](#qualifiedARN) で開始するステートマシンの実行ごとに、Step Functions は、現在出力されているメトリクスと同じ名前と値を持つ追加のメトリクスを出力します。これらの追加メトリクスには、実行を開始するバージョン識別子とエイリアス名のそれぞれのディメンションが含まれます。これらのメトリクスを使用すると、ステートマシンの実行をバージョンレベルでモニタリングし、ロールバックシナリオがいつ必要になるかを判断できます。これらのメトリクスに基づいて [Amazon CloudWatch アラームを作成](procedure-cw-metrics.md#monitoring-using-cloudwatch-console-set-alarm)することもできます。

Step Functions は、エイリアスまたはバージョンで開始した実行について、以下のメトリクスを出力します。
+ `ExecutionTime`
+ `ExecutionsAborted`
+ `ExecutionsFailed`
+ `ExecutionsStarted`
+ `ExecutionsSucceeded`
+ `ExecutionsTimedOut `

バージョン ARN で実行を開始した場合、Step Functions は、`StateMachineArn` を含むメトリクスと、`Version` ディメンションを含む 2 つ目のメトリクスを公開します。

エイリアス ARN を使用して実行を開始した場合、Step Functions は次のメトリクスを出力します。
+ 非修飾 ARN とバージョンの 2 つのメトリクス。
+ `StateMachineArn` と `Alias` のディメンションを含むメトリクス。

# 例: Step Functions でのエイリアスとバージョンのデプロイ
<a name="example-alias-version-deployment"></a>

次の Canary デプロイ手法の例は、 AWS Command Line Interfaceを使用して新しいステートマシンバージョンをデプロイする方法を示しています。この例では、作成したエイリアスは実行トラフィックの 20 パーセントを新しいバージョンにルーティングします。次に、残りの 80% を以前のバージョンにルーティングします。新しいステートマシン[バージョン](concepts-state-machine-version.md)をデプロイし、[エイリアス](concepts-state-machine-alias.md)を使用して実行トラフィックをシフトするには、次の手順を実行します。

1. 

**現在のステートマシンリビジョンからバージョンを公開します。**  
 AWS CLI で **publish-state-machine-version** コマンドを使用して、`myStateMachine:` というステートマシンの現在のリビジョンからバージョンを公開します。

   ```
   aws stepfunctions publish-state-machine-version --state-machine-arn arn:aws:states:region:account-id:stateMachine:myStateMachine
   ```

   レスポンスでは、公開したバージョンの `stateMachineVersionArn` が返されます。例えば、`arn:aws:states:region:account-id:stateMachine:myStateMachine:1`。

1. 

**ステートマシンバージョンを指すエイリアスを作成します。**  
**create-state-machine-alias** コマンドを使用して、`myStateMachine` のバージョン 1 を指す `PROD` というエイリアスを作成します。

   ```
   aws stepfunctions create-state-machine-alias --name PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":100}]"
   ```

1. 

**エイリアスによって開始された実行で、正しい公開バージョンが使用されていることを確認します。**  
**start-execution** コマンドにエイリアス **PROD** の ARN を指定して、`myStateMachine` の新しい実行を開始します。

   ```
   aws stepfunctions start-execution 
     --state-machine-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD
     --input "{}"
   ```

   [StartExecution](https://docs.aws.amazon.com/step-functions/latest/apireference/API_StartExecution.html) リクエストでステートマシン ARN を指定すると、実行を開始するためにエイリアスで指定されたバージョンではなく、ステートマシンの最新の [revision](concepts-cd-aliasing-versioning.md#statemachinerev) が使用されます。

1. 

**ステートマシン定義を更新し、新しいバージョンを公開します。**  
`myStateMachine` を更新して新しいバージョンを公開します。これを行うには、**update-state-machine** コマンドのオプションの `publish` パラメータを使用します。

   ```
   aws stepfunctions update-state-machine
       --state-machine-arn arn:aws:states:region:account-id:stateMachine:myStateMachine
       --definition $UPDATED_STATE_MACHINE_DEFINITION
       --publish
   ```

   レスポンスでは、新しいバージョンの `stateMachineVersionArn` が返されます。例えば、`arn:aws:states:region:account-id:stateMachine:myStateMachine:2`。

1. 

**両方のバージョンを指すようエイリアスを更新し、エイリアスの[ルーティング設定](concepts-state-machine-alias.md#alias-routing-config)を指定します。**  
**update-state-machine-alias** コマンドを使用して、エイリアス `PROD` のルーティング設定を更新します。実行トラフィックの 80% がバージョン 1 に送られ、残りの 20% がバージョン 2 に送られるようエイリアスを設定します。

   ```
   aws stepfunctions update-state-machine-alias --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":80}, {\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:2\",\"weight\":20}]"
   ```

1. 

**バージョン 1 をバージョン 2 に置き換えます。**  
新しいステートマシンバージョンが正しく動作することを確認したら、新しいステートマシンバージョンをデプロイできます。そのためには、エイリアスを再度更新して、実行トラフィックの 100% を新しいバージョンに割り当てます。

   **update-state-machine-alias** コマンドを使用して、バージョン 2 のエイリアス `PROD` のルーティング設定を 100% に設定します。

   ```
   aws stepfunctions update-state-machine-alias --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:2\",\"weight\":100}]"
   ```

**ヒント**  
バージョン 2 のデプロイをロールバックするには、エイリアスのルーティング設定を編集して、新しくデプロイされたバージョンにトラフィックの 100% を移行します。  

```
aws stepfunctions update-state-machine-alias 
  --state-machine-alias-arn arn:aws:states:region:account-id:stateMachineAlias:myStateMachine:PROD 
  --routing-configuration "[{\"stateMachineVersionArn\":\"arn:aws:states:region:account-id:stateMachine:myStateMachine:1\",\"weight\":100}]"
```

バージョンとエイリアスを使用すると、他のタイプのデプロイを実行できます。例えば、ステートマシンの新しいバージョンの*ローリングデプロイ*を実行できます。そのためには、新しいバージョンを指すエイリアスのルーティング設定の加重割合を徐々に増やします。

バージョンとエイリアスを使って*ブルー/グリーンデプロイ*を行うこともできます。そのためには、ステートマシンの現在のバージョン 1 を実行する `green` というエイリアスを作成します。次に、新しいバージョン(例: `2`)を実行する `blue` という別のエイリアスを作成します。新しいバージョンをテストするには、`blue` エイリアスに実行トラフィックを送信します。新しいバージョンが正しく動作することが確認できたら、新しいバージョンを指すよう `green` エイリアスを更新します。

# Step Functions のステートマシンバージョンの段階的なデプロイの実行
<a name="version-rolling-deployment"></a>

ローリングデプロイとは、アプリケーションの以前のバージョンを新しいバージョンのアプリケーションに徐々に置き換えるデプロイ戦略です。ステートマシンバージョンのローリングデプロイを行うには、徐々に増加する実行トラフィックを新しいバージョンに送信します。トラフィックの量と増加率は、設定するパラメータです。

次のいずれかのオプションを使用して、バージョンのローリングデプロイを実行できます。
+ [Step Functions コンソール](https://console.aws.amazon.com/states/home?region=us-east-1#/) - 同じステートマシンの 2 つのバージョンを指すエイリアスを作成します。このエイリアスでは、2 つのバージョン間でトラフィックをシフトするようルーティングを設定します。コンソールを使用したバージョンのロールアウトに関する詳細については、「[バージョン](concepts-state-machine-version.md)」および「[Aliases](concepts-state-machine-alias.md)」を参照してください。
+ **AWS CLI と SDK のスクリプト** - AWS CLI または AWS SDK を使用してシェルスクリプトを作成します。詳細については、AWS CLI および AWS SDK の使用に関する以下のセクションを参照してください。
+ **AWS CloudFormation テンプレート** - `[AWS::StepFunctions::StateMachineVersion](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html)` と `[AWS::StepFunctions::StateMachineAlias](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html)` のリソースを使用して複数のステートマシンバージョンを公開し、そのうちの 1 つまたは 2 つのバージョンを指すエイリアスを作成します。

## AWS CLI を使用してステートマシンの新しいバージョンをデプロイします。
<a name="version-canary-deploy-cli"></a>

このセクションのスクリプト例は、AWS CLI を使用してトラフィックを以前のステートマシンバージョンから新しいステートマシンバージョンに徐々に移行する方法を示しています。このサンプルスクリプトを使用することも、必要に応じて更新することもできます。

このスクリプトは、エイリアスを使用して新しいステートマシンバージョンをデプロイするための Canary デプロイを示しています。以下の手順は、このスクリプトが実行するタスクの概要を示しています。

1. `publish_revision` パラメータが true に設定されている場合は、最新の [revision](concepts-cd-aliasing-versioning.md#statemachinerev) を、ステートマシンの次のバージョンとして公開します。デプロイが成功すると、このバージョンが新しいライブバージョンになります。

   `publish_revision` パラメータを false に設定すると、スクリプトは、最後に公開されたバージョンのステートマシンをデプロイします。

1. エイリアスがまだ存在しない場合は作成します。エイリアスが存在しない場合は、このエイリアスへのトラフィックの 100% を新しいバージョンにルーティングして、スクリプトを終了します。

1. エイリアスのルーティング設定を更新して、トラフィックのごく一部を以前のバージョンから新しいバージョンにシフトします。canary のこの割合は `canary_percentage` パラメータで設定します。

1. デフォルトでは、設定可能な CloudWatch アラームを 60 秒ごとにモニタリングします。これらのアラームのいずれかが発せられたら、トラフィックの 100% を以前のバージョンにルーティングし、すぐにデプロイをロールバックしてください。

   `alarm_polling_interval` で定義した時間間隔 (秒単位) が経過したら、引き続きアラームをモニタリングします。`canary_interval_seconds` で定義した時間間隔が経過するまでモニタリングを続けます。

1. `canary_interval_seconds` の間にアラームが鳴らなかった場合は、トラフィックの 100% を新しいバージョンにシフトします。

1. 新しいバージョンが正常にデプロイされたら、`history_max` パラメータで指定された番号より古いバージョンをすべて削除します。



```
#!/bin/bash
# 
# AWS StepFunctions example showing how to create a canary deployment with a
# State Machine Alias and versions.
# 
# Requirements: AWS CLI installed and credentials configured.
# 
# A canary deployment deploys the new version alongside the old version, while
# routing only a small fraction of the overall traffic to the new version to
# see if there are any errors. Only once the new version has cleared a testing
# period will it start receiving 100% of traffic.
# 
# For a Blue/Green or All at Once style deployment, you can set the
# canary_percentage to 100. The script will immediately shift 100% of traffic
# to the new version, but keep on monitoring the alarms (if any) during the
# canary_interval_seconds time interval. If any alarms raise during this period,
# the script will automatically rollback to the previous version.
# 
# Step Functions allows you to keep a maximum of 1000 versions in version history
# for a state machine. This script has a version history deletion mechanism at
# the end, where it will delete any versions older than the limit specified.
# 
# For an example that also demonstrates linear (or rolling) deployments, see the following: 
# https://github.com/aws-samples/aws-stepfunctions-examples/blob/main/gradual-deploy/sfndeploy.py

set -euo pipefail

# ******************************************************************************
# you can safely change the variables in this block to your values
state_machine_name="my-state-machine"
alias_name="alias-1"
region="us-east-1"

# array of cloudwatch alarms to poll during the test period.
# to disable alarm checking, set alarm_names=()
alarm_names=("alarm1" "alarm name with a space")

# true to publish the current revision as the next version before deploy.
# false to deploy the latest version from the state machine's version history.
publish_revision=true

# true to force routing configuration update even if the current routing
# for the alias does not have a 100% routing config.
# false will abandon deploy attempt if current routing config not 100% to a
# single version.
# Be careful when you combine this flag with publish_revision - if you just
# rerun the script you might deploy the newly published revision from the
# previous run.
force=false

# percentage of traffic to route to the new version during the test period
canary_percentage=10

# how many seconds the canary deployment lasts before full deploy to 100%
canary_interval_seconds=300

# how often to poll the alarms
alarm_polling_interval=60

# how many versions to keep in history. delete versions prior to this.
# set to 0 to disable old version history deletion.
history_max=0
# ******************************************************************************

#######################################
# Update alias routing configuration.
# 
# If you don't specify version 2 details, will only create 1 routing entry. In
# this case the routing entry weight must be 100.
# 
# Globals:
#   alias_arn
# Arguments:
#   1. version 1 arn
#   2. version 1 weight
#   3. version 2 arn (optional)
#   4. version 2 weight (optional)
#######################################
function update_routing() {
  if [[ $# -eq 2 ]]; then
    local routing_config="[{\"stateMachineVersionArn\": \"$1\", \"weight\":$2}]"
  elif [[ $# -eq 4 ]]; then
    local routing_config="[{\"stateMachineVersionArn\": \"$1\", \"weight\":$2}, {\"stateMachineVersionArn\": \"$3\", \"weight\":$4}]"
  else
    echo "You have to call update_routing with either 2 or 4 input arguments." >&2
    exit 1
  fi
  
  ${aws} update-state-machine-alias --state-machine-alias-arn ${alias_arn} --routing-configuration "${routing_config}"
}

# ******************************************************************************
# pre-run validation
if [[ (("${#alarm_names[@]}" -gt 0)) ]]; then
  alarm_exists_count=$(aws cloudwatch describe-alarms --alarm-names "${alarm_names[@]}" --alarm-types "CompositeAlarm" "MetricAlarm" --query "length([MetricAlarms, CompositeAlarms][])" --output text)

  if [[ (("${#alarm_names[@]}" -ne "${alarm_exists_count}")) ]]; then
    echo All of the alarms to monitor do not exist in CloudWatch: $(IFS=,; echo "${alarm_names[*]}") >&2
    echo Only the following alarm names exist in CloudWatch:
    aws cloudwatch describe-alarms --alarm-names "${alarm_names[@]}" --alarm-types "CompositeAlarm" "MetricAlarm" --query "join(', ', [MetricAlarms, CompositeAlarms][].AlarmName)" --output text
    exit 1
  fi
fi

if [[ (("${history_max}" -gt 0)) && (("${history_max}" -lt 2)) ]]; then
  echo The minimum value for history_max is 2. This is the minimum number of older state machine versions to be able to rollback in the future. >&2
  exit 1
fi
# ******************************************************************************
# main block follows

account_id=$(aws sts get-caller-identity --query Account --output text)

sm_arn="arn:aws:states:${region}:${account_id}:stateMachine:${state_machine_name}"

# the aws command we'll be invoking a lot throughout.
aws="aws stepfunctions"

# promote the latest revision to the next version
if [[ "${publish_revision}" = true ]]; then
  new_version=$(${aws} publish-state-machine-version --state-machine-arn=$sm_arn --query stateMachineVersionArn --output text)
  echo Published the current revision of state machine as the next version with arn: ${new_version}
else
  new_version=$(${aws} list-state-machine-versions --state-machine-arn ${sm_arn} --max-results 1 --query "stateMachineVersions[0].stateMachineVersionArn" --output text)
  echo "Since publish_revision is false, using the latest version from the state machine's version history: ${new_version}"
fi

# find the alias if it exists
alias_arn_expected="${sm_arn}:${alias_name}"
alias_arn=$(${aws} list-state-machine-aliases --state-machine-arn ${sm_arn} --query "stateMachineAliases[?stateMachineAliasArn==\`${alias_arn_expected}\`].stateMachineAliasArn" --output text)

if [[ "${alias_arn_expected}" == "${alias_arn}" ]]; then
  echo Found alias ${alias_arn}

  echo Current routing configuration is:
  ${aws} describe-state-machine-alias --state-machine-alias-arn "${alias_arn}" --query routingConfiguration
else
  echo Alias does not exist. Creating alias ${alias_arn_expected} and routing 100% traffic to new version ${new_version}
  
  ${aws} create-state-machine-alias --name "${alias_name}" --routing-configuration "[{\"stateMachineVersionArn\": \"${new_version}\", \"weight\":100}]"

  echo Done!
  exit 0
fi

# find the version to which the alias currently points (the current live version)
old_version=$(${aws} describe-state-machine-alias --state-machine-alias-arn $alias_arn --query "routingConfiguration[?weight==\`100\`].stateMachineVersionArn" --output text)

if [[ -z "${old_version}" ]]; then
  if [[ "${force}" = true ]]; then
    echo Force setting is true. Will force update to routing config for alias to point 100% to new version.
    update_routing "${new_version}" 100
    
    echo Alias ${alias_arn} now pointing 100% to ${new_version}.
    echo Done!
    exit 0
  else
    echo Alias ${alias_arn} does not have a routing config entry with 100% of the traffic. This means there might be a deploy in progress, so not starting another deploy at this time. >&2
    exit 1
  fi
fi

if [[ "${old_version}" == "${new_version}" ]]; then
  echo The alias already points to this version. No update necessary.
  exit 0
fi

echo Switching ${canary_percentage}% to new version ${new_version}
(( old_weight = 100 - ${canary_percentage} ))
update_routing "${new_version}" ${canary_percentage} "${old_version}" ${old_weight}

echo New version receiving ${canary_percentage}% of traffic.
echo Old version ${old_version} is still receiving ${old_weight}%.

if [[ ${#alarm_names[@]} -eq 0 ]]; then
  echo No alarm_names set. Skipping cloudwatch monitoring.
  echo Will sleep for ${canary_interval_seconds} seconds before routing 100% to new version.
  sleep ${canary_interval_seconds}
  echo Canary period complete. Switching 100% of traffic to new version...
else
  echo Checking if alarms fire for the next ${canary_interval_seconds} seconds.

  (( total_wait = canary_interval_seconds + $(date +%s) ))

  now=$(date +%s)
  while [[ ((${now} -lt ${total_wait})) ]]; do
    alarm_result=$(aws cloudwatch describe-alarms --alarm-names "${alarm_names[@]}" --state-value ALARM --alarm-types "CompositeAlarm" "MetricAlarm" --query "join(', ', [MetricAlarms, CompositeAlarms][].AlarmName)" --output text)

    if [[ ! -z "${alarm_result}" ]]; then
      echo The following alarms are in ALARM state: ${alarm_result}. Rolling back deploy. >&2
      update_routing "${old_version}" 100

      echo Rolled back to ${old_version}
      exit 1
    fi
  
    echo Monitoring alarms...no alarms have triggered.
    sleep ${alarm_polling_interval}
    now=$(date +%s)
  done

  echo No alarms detected during canary period. Switching 100% of traffic to new version...
fi

update_routing "${new_version}" 100

echo Version ${new_version} is now receiving 100% of traffic.

if [[ (("${history_max}" -eq 0 ))]]; then
  echo Version History deletion is disabled. Remember to prune your history, the default limit is 1000 versions.
  echo Done!
  exit 0
fi

echo Keep the last ${history_max} versions. Deleting any versions older than that...

# the results are sorted in descending order of the version creation time
version_history=$(${aws} list-state-machine-versions --state-machine-arn ${sm_arn} --max-results 1000 --query "join(\`\"\\n\"\`, stateMachineVersions[].stateMachineVersionArn)" --output text)

counter=0

while read line; do
  ((counter=${counter} + 1))

  if [[ (( ${counter} -gt ${history_max})) ]]; then
    echo Deleting old version ${line}
    ${aws} delete-state-machine-version --state-machine-version-arn ${line}
  fi
done <<< "${version_history}"

echo Done!
```

## AWS SDK を使用してステートマシンの新しいバージョンをデプロイします。
<a name="version-deploy-sdk"></a>

[aws-stepfunctions-examples](https://github.com/aws-samples/aws-stepfunctions-examples/tree/main/gradual-deploy) にあるサンプルスクリプトは、AWS SDK for Python を使用して、トラフィックを以前のバージョンから新しいバージョンのステートマシンに徐々に移行する方法を示しています。このサンプルスクリプトを使用することも、必要に応じて更新することもできます。

このスクリプトは、以下のデプロイ戦略を示しています。
+ **Canary** - トラフィックは 2 つの増分で移行されます。

  1 回目の増分では、トラフィックのごく一部、例えば 10% が新しいバージョンにシフトされます。2 回目の増分では、指定した秒単位の時間間隔が経過する前に、残りのトラフィックが新しいバージョンにシフトされます。残りのトラフィックの新しいバージョンへの切り替えは、指定された時間間隔内に CloudWatch アラームがオフになっていない場合にのみ行われます。
+  **リニアまたはローリング** - トラフィックを同じ間隔で、各増分の間隔を同じ秒数空けて新しいバージョンにシフトします。

  例えば、増加率を **600** 秒のうちの `--interval` の **20** として指定すると、このデプロイでは、新しいバージョンがトラフィックの 100% を受信するまで、600 秒ごとに 20% ずつトラフィックが増加します。

  このデプロイでは、いずれかの CloudWatch アラームがオフになると、すぐに新しいバージョンがロールバックされます。
+ **All at Once または Blue/Green** - トラフィックの 100% をただちに新しいバージョンに移行します。このデプロイでは新しいバージョンをモニタリングし、CloudWatch アラームがオフになっている場合は、以前のバージョンに自動的にロールバックします。

## AWS CloudFormation を使用して、新しいステートマシンバージョンをデプロイします。
<a name="version-deploy-cfn"></a>

次の CloudFormation テンプレートの例では、`MyStateMachine` というステートマシンの 2 つのバージョンを公開しています。両方のバージョンを指す `PROD` というエイリアスを作成して、バージョン `2` をデプロイします。

この例では、このバージョンがトラフィックの 100% を受信するまで、5 分ごとにトラフィックの 10% がバージョン `2` に移行されます。この例は、CloudWatch アラームを設定する方法も示しています。設定したアラームのいずれかが `ALARM` 状態になると、デプロイは失敗し、すぐにロールバックされます。

```
MyStateMachine:
  Type: AWS::StepFunctions::StateMachine
  Properties:
    Type: STANDARD
    StateMachineName: MyStateMachine
    RoleArn: arn:aws:iam::account-id:role/myIamRole
    Definition:
      StartAt: PassState
      States:
        PassState:
          Type: Pass
          Result: Result
          End: true

MyStateMachineVersionA:
  Type: AWS::StepFunctions::StateMachineVersion
  Properties:
    Description: Version 1
    StateMachineArn: !Ref MyStateMachine

MyStateMachineVersionB:
  Type: AWS::StepFunctions::StateMachineVersion
  Properties:
    Description: Version 2
    StateMachineArn: !Ref MyStateMachine

PROD:
  Type: AWS::StepFunctions::StateMachineAlias
  Properties:
    Name: PROD
    Description: The PROD state machine alias taking production traffic.
    DeploymentPreference:
      StateMachineVersionArn: !Ref MyStateMachineVersionB
      Type: LINEAR
      Percentage: 10
      Interval: 5
      Alarms:
        # A list of alarms that you want to monitor. If any of these alarms trigger, rollback the deployment immediately by pointing 100 percent of traffic to the previous version.
        - !Ref CloudWatchAlarm1
        - !Ref CloudWatchAlarm2
```