Search code examples
javaamazon-web-servicesaws-lambdaaws-step-functions

How to trigger AWS step function from aws lambda


I need to trigger a step function from a lambda handler (written in java). Lambda has full access to AWS step functions via IAM.

I have tried the following, I see no error, I get 200 Json back, but step function is not executing.

This is the code I tried:

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 logs:

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

Solution

  • I figured it out.

    AWSStepFunctionsAsyncClient needs to be built with clientConfig and the correct region. It is US-EAST-1 for us. Strange that no exception comes when we don't do it and nothing happens too. That is, instead of

    AWSStepFunctionsAsync client = AWSStepFunctionsAsyncClientBuilder.defaultClient();
    

    we should use:

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

    After that, I encountered HTTP connect time out issue. To fix that, egress rules of the lambda have been fixed so that it could fire a HTTP call outside the VPC to the step function endpoint, over the internet.