답변

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- (일반적으로 테스트 목적으로) 국가 기계에서 하나 개의 특정 함수를 호출

를 시작


필요할 수있는 두 가지가 있습니다 다음 데모에서는 두 경우를 모두 사용합니다.

먼저 서버 유형, 람다 함수 및 올바른 IAM 권한을 선언하려면 serverless.yml 파일을 구성해야합니다.

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

당신이 요즘 그것을 해결하는 방법입니다. ${self:resources.Outputs.fipeStateMachine.Value}를 사용하여 환경으로

# 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 

그런 다음 당신이 설정할 수 있습니다 상태 머신 ARN : 당신의 serverless.yml에서

, 당신의 stepFunctions 또한 Outputs을 정의합니다.