📝

CloudFormation, SAM, CDK で Step Functions をデプロイしてみた

に公開

CloudFormation, SAM, CDK で Lambda をデプロイしてみた
上記の Step Functions バージョンです。

前提

  • ステートマシン定義は以下の通りです
{
  "Comment": "A description of my state machine",
  "StartAt": "成功",
  "States": {
    "成功": {
      "Type": "Succeed"
    }
  },
  "QueryLanguage": "JSONata"
}
  • デプロイ手順については上述の Lambda のブログをご参照ください

CloudFormation

コードは以下の通りです。

AWSTemplateFormatVersion: "2010-09-09"
Description: "Simple Step Functions State Machine with Succeed state"

Resources:
  SimpleStateMachine:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      StateMachineName: SimpleSucceedStateMachine-CF
      DefinitionString: |
        {
          "Comment": "A description of my state machine",
          "StartAt": "成功",
          "States": {
            "成功": {
              "Type": "Succeed"
            }
          },
          "QueryLanguage": "JSONata"
        }
      RoleArn: !GetAtt StepFunctionsRole.Arn

  StepFunctionsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: states.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess

SAM

コードは以下の通りです。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  SimpleStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Name: SimpleSucceedStateMachine-SAM
      DefinitionUri: statemachine/simple_statemachine.asl.json
      Role: !GetAtt StepFunctionsRole.Arn

  StepFunctionsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: states.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess

CDK

コードは以下の通りです。

lib/cdk-lambda-project-stack.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as stepfunctions from 'aws-cdk-lib/aws-stepfunctions';
import * as iam from 'aws-cdk-lib/aws-iam';

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

    const stepFunctionsRole = new iam.Role(this, 'StepFunctionsRole', {
      assumedBy: new iam.ServicePrincipal('states.amazonaws.com'),
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName('AWSStepFunctionsFullAccess')
      ]
    });

    const definition = {
      "Comment": "A description of my state machine",
      "StartAt": "成功",
      "States": {
        "成功": {
          "Type": "Succeed"
        }
      },
      "QueryLanguage": "JSONata"
    };

    new stepfunctions.CfnStateMachine(this, 'SimpleStateMachine', {
      stateMachineName: 'SimpleSucceedStateMachine-CDK',
      definitionString: JSON.stringify(definition),
      roleArn: stepFunctionsRole.roleArn
    });
  }
}

まとめ

今回は AWS の IaC ツールで Step Functions をデプロイしてみました。
どなたかの参考になれば幸いです。

参考資料

Discussion