如何从AWS Lambda触发AWS Step函数

时间:2019-04-02 13:31:08

标签: java amazon-web-services aws-step-functions

我需要从lambda处理程序(用Java编写)触发一个step函数。 Lambda可以通过IAM完全访问AWS步骤功能。

我尝试了以下操作,没有看到错误,返回200的Json,但是step函数未执行。

这是我尝试的代码:

StartExecutionRequest startExecutionRequest = new StartExecutionRequest();
startExecutionRequest.setStateMachineArn(stateMachineArn);
logger.info("stateMachineArn: "+stateMachineArn);
logger.info("stateMachineInputJson: "+stateMachineInputJson.toString());

AWSStepFunctionsAsync client = AWSStepFunctionsAsyncClientBuilder.defaultClient();
logger.info("startExecutionRequest: "+startExecutionRequest);
try {
    logger.info("startExecutionAsync now");
    client.startExecutionAsync(startExecutionRequest);
    logger.info("startExecutionAsync done");
    return new Response(200,"","stepFunctionTriggered");
} 
catch (Exception e) {
    logger.error("Exception while starting execution:"+ e);
    return  new Response(400,"","Error occured while executing Step Function");
}

Lambda日志:

START RequestId: 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 Version: $LATEST
2019-04-02 18:17:56 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 INFO  LaunchStepFunction:39 - stateMachineArn: arn:aws:states:xxxxx
2019-04-02 18:17:56 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 INFO  LaunchStepFunction:40 - stateMachineInputJson: {}
2019-04-02 18:18:01 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 INFO  LaunchStepFunction:43 - startExecutionRequest: {StateMachineArn: arn:aws:states:us-east-1:xxx:stateMachine:xxxx,}
2019-04-02 18:18:01 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 INFO  LaunchStepFunction:45 - startExecutionAsync now
2019-04-02 18:18:01 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56 INFO  LaunchStepFunction:47 - startExecutionAsync done
END RequestId: 2c6ac51d-1262-4fbf-acdc-ce706d5fbe56

1 个答案:

答案 0 :(得分:1)

我知道了。

AWSStepFunctionsAsyncClient必须使用clientConfig和正确的区域来构建。对我们来说是US-EAST-1。奇怪的是,如果我们不这样做,也不会例外,也不会发生任何事情。也就是说,代替

AWSStepFunctionsAsync client = AWSStepFunctionsAsyncClientBuilder.defaultClient();

我们应该使用:

AWSStepFunctionsAsyncClientBuilder.standard()
            .withClientConfiguration(new ClientConfiguration())
            .withRegion(Regions.US_EAST_1)
            .build();

在那之后,我遇到了HTTP连接超时问题。为了解决此问题,已修复lambda的出口规则,以使其可以通过Internet在VPC外部向步骤函数端点触发HTTP调用。