LoginSignup
1
1

More than 1 year has passed since last update.

【AWS】CDKv2で環境構築 StepFunctions編

Last updated at Posted at 2022-06-26

はじめに

現在運用中の環境をテスト用として複製する際にCDKv2を用いて
コード化することになったのでその学びです。

DynamoDB編はこちら
Lambda編はこちら
Cognito編はこちら
apigateway編はこちら

準備

このへんの記事でまずは環境設定
https://aws.amazon.com/jp/getting-started/guides/setup-cdk/

今回はTypeScriptを用いて実装しました。

$ cdk init --language typescript

StateMachine作成

今回作成するものは指定時刻になるとLambdaを呼び出すシンプルなものです。

main-stack.ts
import * as stepfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';

export class MainStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // 指定時間まで待つ
    const wait = new stepfn.Wait(this, 'Wait until a specific date and time', {
      time: stepfn.WaitTime.timestampPath('$.timestamp'), // 今回は入力された項目のtimestamp項目を参照
    });

    // 指定時間後に呼び出すタスク(Lambda)
    const sample_task = new tasks.LambdaInvoke(this, 'Scheduled task', {
      lambdaFunction: sample_fn, // Lambda編で作成したsample_fn
    });

    // ステートマシンの作成
    new stepfn.StateMachine(this, 'Sample-Schedule', {
      definition: wait // 上で作成したwait
        .next(sample_task), // 上で作成したtask
      stateMachineName: 'Sample-Schedule',
    });

まずはtaskを定義する。
StateMachine()でステートマシンを作成し、
definition:でその内容(順序)を指定する。
next()をつなげて順序を定義していく。

Waitの種類については以下参照↓
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.WaitTime.html

ドキュメント

Taskについては別途参照
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions_tasks-readme.html

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