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

AWS Step Functions は CDK で書こう!

AWS Step Functions は CDK で書こう!

AWS Step FunctionsはAWS CDKで書くと、他のリソースをまとめて作ったり、IAM Policyを書かなくても済んだりします。JSONataにも対応しました。

JAWS DAYS 2025の登壇資料です。

Kenji Kono

March 05, 2025
Tweet

More Decks by Kenji Kono

Other Decks in Programming

Transcript

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

    rights reserved. Amazon Confidential and Trademark. © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. AWS Step Functions は CDK で書こう︕ ⾼野 賢司 J A W S D A Y S 2 0 2 5 シニア ソリューション アーキテクト アマゾン ウェブ サービス ジャパン合同会社 2025/2/26
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. • Infrastructure as Code (IaC) の 技術⽀援をリード • Baseline Environment on AWS (BLEA) の 開発コアメンバー https://github.com/aws-samples/baseline-environment-on-aws @konokenj こ う の け ん じ ⾼野 賢司 シニア ソリューション アーキテクト @名古屋 アマゾン ウェブ サービス ジャパン合同会社
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 3 AWS Step Functions で変数をサポート start Step 1 Step 2 Step 3 Step 4 end “value” : 1 “value” : 1 • Step Functions 内で変数をサポート – アップデート前はステップ間で値を引き渡していく必要 – アップデートにより変数の保存と参照が簡素化 • ワークフローの途中で値の更新が可能 • 変数のスコープ – 変数に格納したのち、後続のステップから参照可能 – 例外として Map, Parallel内の変数はその内部でのみ参照可能
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. JSONPath + Intrinsic Functions + Lambda JSONata JSONata によるデータ変換 • JSONataはOSSのJSON⽤データクエリおよび変換⾔語 • ⽇付・時刻のフォーマット、⽂字列操作、四則演算などが可能 4 "Calculate": { "QueryLanguage": "JSONata-2.0", "Type": "Task", ... "Assign": { "sum.$": "$sum($states.result.Product.(Price * Count))" } } "Get prices and quantities": { "Type": "Task", ... "ResultSelector": { "Prices.$": "$.Product.Price", "Counts.$": "$.Product.Count" }, "ResultPath": "$.productInfo", "Next": "Calculate" }, "Calculate": { "Type": "Task", "Resource": "arn:...:lambda:invoke", "Parameters": { "FunctionName": "lambdaArn", "Payload.$": "$.productInfo" }, "ResultSelector": { "sum.$": "$.Payload.sum" }, "ResultPath": "$.ProductSum", ... } 商品の価格・数量から合計⾦額を計算する例
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. JSONata によるデータ変換 • JSONataはJSONPathと⽐較し変換フィールドの数を減らして⼊出⼒処理を簡略化 5 Task State Input Argument Output Assign Output Variables Action - Service Integration - HTTPS Endpoint - Activity Worker JSONata JSONPath Task State Input Parameters ResultSelector Assign Output Variables Action - Service Integration - HTTPS Endpoint - Activity Worker InputPath ResultPath OutputPath
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. • Step Functions の変数と JSONata データ変換 が CDK でも簡単に使えるようになった • 従来の JSONPath ではステート間での 値の受け渡しが複雑だった • InputPath, Parameters, ResultPath, ResultSelector が Arguments だけになる • ステートマシン内でのデータ操作のための Lambda 関数を減らせる • 各ステートの静的メソッド .jsonata() / .jsonPath() を使うことで 適切なプロパティだけが使える 6 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 ステートマシン全体での設定 ステートごとの設定
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. • S3 バケットや Lambda 関数、ECS クラスタ等 必要なリソースを画面遷移せずに作れる • grant メソッドで IAM ポリシーを自動設定 • JSONata / JSONPath で型定義が分かれている ため使えるプロパティだけが提案される • --hotswap で数秒でデプロイ。 --hotswap-fallback はホットスワップ不可の 場合のみ CloudFormation 経由でデプロイ 7 AWS Step Functions は CDK で書こう︕ # ステートマシンを変更したら CDK でデプロイ npx cdk deploy --all --require-approval=never --hotswap-fallback # そのままテストしよう!! aws s3 cp test.json s3://example/ # AWS CLI のファイル⼊⼒ (file://) を使えば、⼀度実⾏したイベントを再現できる aws stepfunctions start-execution --state-machine-arn *** --input file://testevent.json console Lambda 関数に S3 バケットへの読み取り・書き込み権限を付与 const procFunc = new PythonFunction(this, 'ProcessFunc', { entry: 'backend/etl', runtime: Runtime.PYTHON_3_13, index: 'etl/process.py', }); rawDataBucket.grantRead(procFunc); workBucket.grantReadWrite(procFunc); TypeScript
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. • 自動スキャンは Pro Tier のみ • 検出対象は Amazon CodeGuru Detector Library で確認できる https://docs.aws.amazon.com/codegur u/detector-library/typescript/tags/aws- cdk/ 8 2024/4/30 ⼀般提供開始 (GA) https://aws.amazon.com/about-aws/whats-new/2024/04/amazon-q- developer-generally-available/ Amazon CodeWhisperer にて AI を活⽤した新しいコード修正、 IaC サポート、および Visual Studio との統合提供を開始 https://aws.amazon.com/jp/blogs/news/amazon-codewhisperer-offers-new- ai-powered-code-remediation-iac-support-and-integration-with-visual-studio/ CDK なら Amazon Q Developer で セキュリティスキャン もしてくれるよ
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. 新しく stable になったモジュール • appconfig (v2.130.0) • apigatewayv2 (v2.112.0) • synthetics (v2.99.0) • batch (v2.96.0) • appsync (v2.60.0) 新しい L2 コンストラクト(一部) • ecs.ServiceManagedVolume (v2.122.0) • cloudfront.KeyValueStore (v2.118.0) • stepfunctions.DistributedMap (v2.127.0) • codedeploy.EcsDeploymentGroup (v2.50.0) • codebuild.Fleet (v2.145.0) • codepipeline.Pipeline (PipelineType.V2 / v2.133.0 からデフォルト) 9 CDK の最新情報は X で配信しています AWS CDK の アップデート情報を つぶやいています #cdk_releases * v2.29.0 (2022年6⽉) から https://twitter.com/hashtag/ cdk_releases?src=hashtag_click&f=live Community News https://x.com/konokenj
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

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