我需要从 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
最佳答案
我明白了。
AWSStepFunctionsAsyncClient 需要使用 clientConfig 和正确的区域进行构建。对我们来说是 US-EAST-1。奇怪的是,当我们不这样做时,也不会出现任何异常(exception),也不会发生任何事情。也就是说,而不是
AWSStepFunctionsAsync client = AWSStepFunctionsAsyncClientBuilder.defaultClient();
我们应该使用:
AWSStepFunctionsAsyncClientBuilder.standard()
.withClientConfiguration(new ClientConfiguration())
.withRegion(Regions.US_EAST_1)
.build();
之后,我遇到了 HTTP 连接超时问题。为了解决这个问题,我们修复了 lambda 的导出规则,以便它可以通过互联网向 VPC 外部的步骤函数端点发起 HTTP 调用。
关于java - 如何从 aws lambda 触发 AWS 步骤函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55476081/