Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS CDK x AWS Step Functions ~ 2025 年の開発体験 ~

Avatar for WinterYukky WinterYukky
July 12, 2025
170

AWS CDK x AWS Step Functions ~ 2025 年の開発体験 ~

AWS CDK Conference Japan 2025 のサブトラックで登壇した際の資料です。
https://jawsug-cdk.connpass.com/event/356357/

AWS Step Functions を利用することで AWS Lambda よりも手離れのいいサーバーレスワークフローを構築することができます。
Step Functions 自体や Step Functions 周辺のアップデートによって開発体験も変わりつつあります。それらを簡易にまとめ、2025年の今 Step Functions を AWS CDK で書くならどう構築していくことがいいのかを説明しています。

Avatar for WinterYukky

WinterYukky

July 12, 2025
Tweet

Transcript

  1. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 1 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. AWS CDK x AWS Step Functions ~ 2025 年の開発体験 ~ Yukihiro Yoshikawa A W S C D K C O N F E R E N C E J A P A N 2 0 2 5 P R E S E N T E D B Y J A W S - U G アマゾンウェブサービス ジャパン合同会社 ソリューションアーキテクト
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 2 • ⼤阪の⿃類アイコン SA • ⾃動化や IaC が⼤好物 • 最近は AWS が開発しているAI 専⽤チップセット (Inferentia/Trainium) にも夢中 • 会場の予約など本イベントの AWS 側の主担当を務めています ⾃⼰紹介 吉川 幸弘 (ゆっきー) アマゾン ウェブ サービス ジャパン合同会社 ソリューションアーキテクト 2
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 3 こんなことはありませんか? たまに実⾏する API をサーバーレスに作りたい・・・ ちょっとしたバッチ/APIを作りたいけど AWS Lambda の EOL 対応をしたくない・・・ AWS の操作をイベントや定期的に実⾏したい 簡単なワークフローだけど時間がかかるので Lambda では要件を満たせない それ、Step Functions の出番かも︕
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 4 AWS Step Functions とは • 分散アプリケーションのための ビジュアルワークフロー • サーバーレスでインフラ管理不要 • 使った分だけの従量課⾦ • AWS の API や HTTP API も簡単に実⾏ • AWS Lambda のような EOL 対応不要
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 5 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Query Language
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 6 新たな Query Language として JSONata をサポート AWS Blog | Simplifying developer experience with variables and JSONata in AWS Step Functions https://aws.amazon.com/jp/blogs/compute/simplifying-developer-experience-with- variables-and-jsonata-in-aws-step-functions/ JSONata を使うと構⽂がシンプルかつ直感的に︕ JSONPath では AWS Lambda が必要だったものも JSONata ならプログラ ムチックにも書けるため、Lambda を必要としないことも少なくない AWS re:Invent 2024 直前
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 7 JSONata なら JSONPath では難しかったデータ変換も可能 https://gist.github.com/WinterYukky/83537215b3c6ed0ba0851a48c8d92c15 JSONata ならこれだけで任意の JSON ⼊⼒を Amazon DynamoDB テーブルへ書き込める︕ ⼊⼒例 DynamoDB テーブルへ書き込める形式に︕
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 8 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. JSONata の実用例
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Control plane Remote SWE Agents https://github.com/aws-samples/remote-swe-agents Amazon Bedrock Agent Workers Amazon Elastic Compute Cloud (Amazon EC2) Amazon API Gateway AWS Lambda Users Slack スレッドごとに インスタンスを起動・⾃動停⽌ リモート型 ⾮同期 OSS の開発エージェント • エージェントごとに専⽤のリモート開発環境を割り当て • Slack 上にいる、同僚や部下のような利⽤体験を志向 • エージェントの挙動をユーザーは細かく関知せず、 タスク完了後に成果を確認する(⾮同期) Slack
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 10 Remote SWE Agents での簡略化例 https://github.com/aws-samples/remote-swe-agents/blob/main/cdk/lib/constructs/ec2-gc/sfn/asl.json Before After AI Agent のサンドボックス⽤ EC2 イ ンスタンスの⾃動停⽌フローを JSONata に切り替えて簡略化 • JSONata で⼗分データを加⼯でき るためデータ加⼯⽤ Lambda が不 要に • JSONata ならデータの動的な切り 替えも⼿軽なためケース分けのた めに使っていたChoice (ステート) も不要に
  11. © 2024, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. • Step Functions の変数と JSONata データ変換 が CDK でも簡単に使えるようになった • 従来の JSONPath ではステート間での 値の受け渡しが複雑だった • InputPath, Parameters, ResultPath, ResultSelector が Arguments だけになる • ステートマシン内でのデータ操作のための Lambda 関数を減らせる • 各ステートの静的メソッド .jsonata() / .jsonPath() を使うことで 適切なプロパティだけが使える 11 AWS Step Functions L2 コンストラクトでJSONata に対応 aws-cdk v2.178.0 (2025/2/6) https://github.com/aws/aws-cdk/releases/tag/v2.178.0 変数と JSONata を使った AWS Step Functions での開発者エクスペリエンスの簡素化 | Amazon Web Services ブログ https://aws.amazon.com/jp/blogs/news/simplifying- developer-experience-with-variables-and-jsonata-in-aws-step-functions/ new StateMachine(this, 'StateMachine', { definition: workflow, queryLanguage: QueryLanguage.JSONATA, }); TypeScript // コンストラクタでのステート作成(レガシー) new sfn.Pass(this, 'Constructor Pattern', { queryLanguage: sfn.QueryLanguage.JSONATA, // or JSON_PATH }); // 今後はコンストラクタではなく static method の利⽤を推奨 // JSONata sfn.Pass.jsonata(this, 'JSONata Pattern', { outputs: { foo: 'bar' }, }); // JSONPath sfn.Pass.jsonPath(this, 'JSONPath Pattern', { outputPath: '$.status', }); TypeScript ステートマシン全体での設定 ステートごとの設定
  12. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 12 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. DefinitionBody の使い分け
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 13 AWS CDK で DefinitionBody を定義する方法は 3 つある DefinitionBody はワークフローの定義本体 (ASL) DefinitionBody.fromChainable() AWS CDK の⾔語で記述 権限の⾃動付与機能、再利⽤性が⾼さが特徴 DefinitionBody.fromFile() ASL を保存したファイルパスを指定 Step Functions Workflow Studio からエクス ポートしたファイルが使える DefinitionBody.fromString() ASL を⽂字列で指定 AWS CloudFormation と同じ体験ができるが、 利⽤頻度はそこまで多くない ABC
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 14 AWS Toolkit の Step Functions サポート ※Visual Studio Code AWS Toolkit 拡張をインストール Step Functions Workflow Studio が IDE 内で使える xxx.asl.yaml DefinitionBody.fromChainable を使っている場合もプレビュー可能 AWS CDK StateMachine コンストラクトには Step Functions のアイコンが表⽰される cdk synth
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 15 DefinitionBody.fromFile() を使う場合 • Step Functions Workflow Studio による開発⽀援を受けられる • JSONata などの構⽂補完 • IDE で扱う場合ファイル名は以下の 形式にしておくと⾃動認識される • xxx.asl.json • xxx.asl.yaml • AWS CDK で作成するリソースの参 照は definitionSubstitutions を利⽤
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 16 DefinitionBody.fromChainable() を使う場合 • 権限の⾃動付与によって試⾏回数 を減らせる • Fragment (コンポーネント化) によ る再利⽤性の⾼さ • ここを活⽤していく⽅針だと かなり強みが出てくる • Step Functions を広く利⽤す る組織ではこちらがおすすめ • 図のコンストラクトでは、開発者 が任意のアクションを埋め込めら れる設計になっている Discord の Webhook Server コンストラクトの例。API Backend が Step Functions で実装されていて、ユー ザーイベントに対して以下 から任意のアクションを埋 め込められる設計にしてい る • state (Fragment) • lambda • asyncLambda
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 17 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. まとめ
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 18 まとめ • 新規ワークフローには JSONata を使おう • 公式ドキュメントでも JSONata が推奨されている • JSONata Playground が Web 上で公開されているため、データ変換をいつでもテストできる • DefinitionBody の使い分けは習熟度や組織による • 初期は Step Functions Workflow Studio を使うのがおすすめ • 慣れてきたら再利⽤可能な Fragment を開発して開発速度を⾼めよう https://docs.aws.amazon.com/step-functions/latest/dg/transforming-data.html 公式ドキュメントにて JSONata が推奨されている箇所
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 19 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark.