6
0

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】Terraformで拡張するStepFunctionsを構築しよう!

Last updated at Posted at 2025-07-15

こんにちは。
この記事は「2025 Japan AWS Jr. Chanpions 夏のQiitaリレー 」の12日目の記事です。

過去の投稿(リンク集)はこちらからご覧ください!

はじめに

本記事はTerraformを使って拡張するStepFunctions構築紹介記事となります。
タイトルからイメージするのが難しいと思うので概要と想定状況についてまずは説明いたします。

本記事で紹介すること

簡単に言うと下記のようなstore_codesの中にコードを追加するだけで

locals {
  store_codes = [
    "01",
    "02",
    "03",
    "04", #例えばこの二つを追加すると
    "05"  #例えばこの二つを追加すると
  ]
}

下記のように並列に処理を追加していくStepFunctionsステートマシンの構築をするTerraformコードの紹介となります。

stepfunctions_graph.png

store_codesの中に"04" "05"を追加することで下記のようにステートマシンが拡張される。

stepfunctions_graph (1).png

使いどころ

例えば下記のようにお店ごとにサーバーを用意し、そのサーバーに特定のバッチ処理が必要な場合にStepFunctionsでバッチ処理をLambda関数で実装しStepFunctionsをスケジュールで実行するということがあると思います。

この時、継続的にお店が増えていく場合、このステートマシンにそのお店用の処理を追加していく必要があります。

image.png

これをTerraformで、store_codesにお店コードを追加するだけで、ステートマシンを拡張してくれるのでお店追加作業が楽になるというものになります。

Terraformソースの紹介

本記事で紹介するソースは下記に公開しております。

Terraformのフォルダ構成は下記のようになります。

.
└── project/
    ├── main/
    │   ├── local.tf
    │   ├── main.tf
    │   └── provider.tf
    │   
    └── module/
        ├── iam/
        │   ├── output.tf
        │   ├── resource_iam.tf
        │   └── variables.tf
        ├── lambda/
        │   ├── lambda_code/
        │   │   ├── test-lambda.py
        │   │   └── test-lambda.zip
        │   ├── output.tf
        │   ├── resource_lambda.tf
        │   └── variables.tf
        └── sfsm/
            ├── resource_sfsm.tf
            ├── sfsm_definition.json
            └── variables.tf

Terraformで拡張するための具体的な方法

どうやってお店コードをステートマシンに追加しているかについて少し解説いたします。
まずは下記のsfsm_definition.jsonをご一読ください。

sfsm_definition.json

{
    "Comment":"A Step Functions stete machine",
    "StartAt": "Stores_batch",
    "States": {
        "Stores_batch":{
            "Type": "Parallel",
            "Branches":[
                %{~ for index, store_code in VAR_STORE_CODES ~}
                %{ if index!=0},
                %{ endif}
                {
                    "StartAt":"${store_code}_batch",
                    "States":{
                        "${store_code}_batch":{
                            "Type":"Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals":[
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds":1,
                                    "MaxAttempts":2,
                                    "BackoffRate":3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next":"${store_code}_batch Pass"
                                }
                            ],
                            "Next":"${store_code}_batch Pass"
                        },
                        "${store_code}_batch Pass":{
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
                %{ endfor ~}
            ],
        "End":true
        }
    },
    "TimeoutSeconds":${VAR_TIMEOUTSECONDS}
}

重要なポイントのみを抜きだすと下記のようになっています。

%{~ for index, store_code in VAR_STORE_CODES ~}
%{ if index!=0},
%{ endif}
#ここに繰り返したい処理を書く
%{ endfor ~}

%{ for index, store_code in VAR_STORE_CODES ~} %{ endfor ~}

これらで囲まれている部分が、リストにあるお店コード分繰り返されます。

%{ if index!=0}, %{ endif}

index が 0 (最初の要素) の場合を除いて、各要素の前にカンマが挿入されます。

つまりsfsm_definition.jsonstore_codes"01""05"まで入っている場合は下記のようなjsonになるということになります。

0105の時のsfsm_definition.json

{
    "Comment": "A Step Functions stete machine",
    "StartAt": "Stores_batch",
    "States": {
        "Stores_batch": {
            "Type": "Parallel",
            "Branches": [
                {
                    "StartAt": "01_batch",
                    "States": {
                        "01_batch": {
                            "Type": "Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next": "01_batch Pass"
                                }
                            ],
                            "Next": "01_batch Pass"
                        },
                        "01_batch Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "02_batch",
                    "States": {
                        "02_batch": {
                            "Type": "Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next": "02_batch Pass"
                                }
                            ],
                            "Next": "02_batch Pass"
                        },
                        "02_batch Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "03_batch",
                    "States": {
                        "03_batch": {
                            "Type": "Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next": "03_batch Pass"
                                }
                            ],
                            "Next": "03_batch Pass"
                        },
                        "03_batch Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "04_batch",
                    "States": {
                        "04_batch": {
                            "Type": "Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next": "04_batch Pass"
                                }
                            ],
                            "Next": "04_batch Pass"
                        },
                        "04_batch Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                },
                {
                    "StartAt": "05_batch",
                    "States": {
                        "05_batch": {
                            "Type": "Task",
                            "Resource": "${VAR_LAMBDA_ARN}",
                            "Retry": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 3
                                }
                            ],
                            "Catch": [
                                {
                                    "ErrorEquals": [
                                        "States.ALL"
                                    ],
                                    "Next": "05_batch Pass"
                                }
                            ],
                            "Next": "05_batch Pass"
                        },
                        "05_batch Pass": {
                            "Type": "Pass",
                            "End": true
                        }
                    }
                }
            ],
            "End": true
        }
    },
    "TimeoutSeconds": ${VAR_TIMEOUTSECONDS}
}

おわりに

今回はシンプルな構成でご紹介したので、イマイチありがたみを実感できなったかもしれません。。
しかしもう少し複雑なステートマシンかつ大量のお店に一気に拡張したい場合などには大きな作業工数削減効果が見込めるかと思います。
また、store_codesにお店コードを追加するだけでよいので作業ミスが起きにくいのも嬉しいポイントです。
また、今回はすべてのお店で同じLambda関数を実行していますが、Terraformの書き方を工夫すれば、お店ごとに別のLambdaを起動することも可能です。
皆さんもぜひ活用してみてください。
それではまた。

6
0
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
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?