如何从 aws lambda 触发 AWS step 函数

How to trigger AWS step function from aws lambda

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

我已经尝试了以下方法,没有发现任何错误,我得到了 200 Json,但是没有执行步骤函数。

这是我试过的代码:

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

我明白了。

A​​WSStepFunctionsAsyncClient 需要使用 clientConfig 和正确的区域构建。对我们来说是 US-EAST-1。奇怪的是,当我们不这样做时也没有例外,也没有任何反应。也就是说,而不是

AWSStepFunctionsAsync client = AWSStepFunctionsAsyncClientBuilder.defaultClient();

我们应该使用:

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

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