2

ラムダ関数を作成するためにServerless Frameworkを使用し、ステップ関数を定義するserverless-step-functionsプラグインを使用しています。serverless-step-functionsプラグインの定義を使用してAWSステップ関数を呼び出す方法は?

serverless.ymlファイルに定義された名前を使用して、ラムダ関数の1つからステップ関数を直接呼び出すことはできますか?

サーバレス環境変数使用して解く

答えて

1

:機能で

environment: 
    MYFUNCTION_ARN: "arn:aws:states:${self:provider.region}:${self:provider.environment.AWS_ACCOUNT}:stateMachine:${self:service}-${self:provider.stage}-myFunction" 

var params = { 
    stateMachineArn: process.env.MYFUNCTION_ARN 
}; 
7

私は同じ問題と、この質問と自己の答えを解決しようとしていたが非常に有用でした。しかし、今後の読者を助けるために、より詳細な答えと実例を追加したいと思います。


に必要となる可能性がある二つのものがあります。

の1-ステートマシンを起動し
2-(通常はテスト目的のために)ステートマシンから1つの特定の関数を呼び出します

は、次のデモでは両方のケースを使用します。

まず、serverless.ymlファイルで、ステートマシン、ラムダ関数、正しいIAMアクセス許可を宣言する必要があります。

service: test-state-machine 

provider: 
    name: aws 
    runtime: nodejs4.3 
    region: us-east-1 
    stage: dev 
    environment: 
    AWS_ACCOUNT: 1234567890 # use your own AWS ACCOUNT number here 

    # define the ARN of the State Machine 
    STEP_FUNCTION_ARN: "arn:aws:states:${self:provider.region}:${self:provider.environment.AWS_ACCOUNT}:stateMachine:${self:service}-${self:provider.stage}-lambdaStateMachine" 

    # define the ARN of function step that we want to invoke 
    FUNCTION_ARN: "arn:aws:lambda:${self:provider.region}:${self:provider.environment.AWS_ACCOUNT}:function:${self:service}-${self:provider.stage}-stateMachineFirstStep" 

functions: 
    # define the Lambda function that will start the State Machine 
    lambdaStartStateMachine: 
    handler: handler.lambdaStartStateMachine 
    role: stateMachine # we'll define later in this file 

    # define the Lambda function that will execute an arbitrary step 
    lambdaInvokeSpecificFuncFromStateMachine: 
    handler: handler.lambdaInvokeSpecificFuncFromStateMachine 
    role: specificFunction # we'll define later in this file 

    stateMachineFirstStep: 
    handler: handler.stateMachineFirstStep 

# define the State Machine 
stepFunctions: 
    stateMachines: 
    lambdaStateMachine: 
     Comment: "A Hello World example" 
     StartAt: firstStep 
     States: 
     firstStep: 
      Type: Task 
      Resource: stateMachineFirstStep 
      End: true  

# define the IAM permissions of our Lambda functions 
resources: 
    Resources: 
    stateMachine: 
     Type: AWS::IAM::Role 
     Properties: 
     RoleName: stateMachine 
     AssumeRolePolicyDocument: 
      Version: '2012-10-17' 
      Statement: 
      - Effect: Allow 
       Principal: 
       Service: 
        - lambda.amazonaws.com 
       Action: sts:AssumeRole 
     Policies: 
      - PolicyName: stateMachine 
      PolicyDocument: 
       Version: '2012-10-17' 
       Statement: 
       - Effect: "Allow" 
        Action: 
        - "states:StartExecution" 
        Resource: "${self:provider.environment.STEP_FUNCTION_ARN}" 
    specificFunction: 
     Type: AWS::IAM::Role 
     Properties: 
     RoleName: specificFunction 
     AssumeRolePolicyDocument: 
      Version: '2012-10-17' 
      Statement: 
      - Effect: Allow 
       Principal: 
       Service: 
        - lambda.amazonaws.com 
       Action: sts:AssumeRole 
     Policies: 
      - PolicyName: specificFunction 
      PolicyDocument: 
       Version: '2012-10-17' 
       Statement: 
       - Effect: "Allow" 
        Action: 
        - "lambda:InvokeFunction" 
        Resource: "${self:provider.environment.FUNCTION_ARN}" 

package: 
    exclude: 
    - node_modules/** 
    - .serverless/** 

plugins: 
    - serverless-step-functions 

handler.jsファイル内のラムダ関数を定義します。実行

const AWS = require('aws-sdk'); 

module.exports.lambdaStartStateMachine = (event, context, callback) => { 
    const stepfunctions = new AWS.StepFunctions(); 
    const params = { 
    stateMachineArn: process.env.STEP_FUNCTION_ARN, 
    input: JSON.stringify({ "msg": "some input" }) 
    }; 

    // start a state machine 
    stepfunctions.startExecution(params, (err, data) => { 
    if (err) { 
     callback(err, null); 
     return; 
    } 

    const response = { 
     statusCode: 200, 
     body: JSON.stringify({ 
     message: 'started state machine', 
     result: data 
     }) 
    }; 

    callback(null, response); 
    }); 
}; 

module.exports.lambdaInvokeSpecificFuncFromStateMachine = (event, context, callback) => { 
    const lambda = new AWS.Lambda(); 
    const params = { 
    FunctionName: process.env.FUNCTION_ARN, 
    Payload: JSON.stringify({ message: 'invoked specific function' }) 
    }; 

    // invoke a specific function of a state machine 
    lambda.invoke(params, (err, data) => { 
    if (err) { 
     callback(err, null); 
     return; 
    } 

    const response = { 
     statusCode: 200, 
     body: JSON.stringify({ 
     message: 'invoke specific function of a state machine', 
     result: data 
     }) 
    }; 

    callback(null, response); 
    }); 
}; 

module.exports.stateMachineFirstStep = (event, context, callback) => { 
    const response = { 
    statusCode: 200, 
    body: JSON.stringify({ 
     message: 'state machine first step', 
     input: event 
    }), 
    }; 

    callback(null, response); 
}; 

デプロイ:使用

serverless deploy stepf 
serverless deploy 

テスト:

serverless invoke -f lambdaStartStateMachine 
serverless invoke -f lambdaInvokeSpecificFuncFromStateMachine 
0

をここでは、あなたが、今日それを解決する方法です。あなたのserverless.yml

、あなたのstepFunctionsともOutputsを定義します。

# define your step functions 
stepFunctions: 
    stateMachines: 
    myStateMachine: 
     name: stateMachineSample 
     events: 
     - http: 
      path: my-trigger 
      method: GET 

# make it match your step functions definition 
Outputs: 
    myStateMachine: 
     Value: 
     Ref: StateMachineSample 

次にあなたが${self:resources.Outputs.fipeStateMachine.Value}を使用して環境として、あなたのステートマシンARNを設定することができます。