Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking aws Step function from java lambda

I have created a step function in aws. Name of my state machine is 'TestStep'. which is used to iterate a number from 1 to 1000.

I have created an IAM Role which has "AWSStepFunctionsFullAccess" policy.

I created one java lambda to access this step function. My code is given below.

 final StateMachine stateMachine = stateMachine().comment("Iterator State Machine Example").startAt("ConfigureCount")
             .state("ConfigureCount", taskState()
               .resource("arn:aws:lambda:us-east-1:ACCOUNTID:function:TestStep")
               .transition(end()))
       .build();
final AWSStepFunctions client = AWSStepFunctionsClientBuilder.defaultClient();
        client.createStateMachine(new CreateStateMachineRequest()
                                                  .withName("TestStep")
                                                  .withRoleArn("arn:aws:iam::ACCOUNTID:role/ROLENAME")
                                                  .withDefinition(stateMachine));

But I am getting an error like below. Please help me to get this correctly. When i am calling it from java the step function should be triggered and work...

enter image description here

like image 605
ShaiNe Ram Avatar asked Mar 01 '18 06:03

ShaiNe Ram


People also ask

Can I call a step function from Lambda?

With asynchronous invocation, Step Functions pauses the workflow execution until a task token is returned. Services that you can configure to invoke Step Functions include: AWS Lambda, using the StartExecution call.

How to work with AWS Lambda Java?

The following steps for working with the AWS Lambda Java function are listed below: First, you need to implement the code as a method in a handler class. Open Eclipse and from the toolbar, open the Amazon Web Services menu (homepage icon). Click on the “ New AWS Lambda Java project ” option.

Are there any examples of the AWS step functions in Java?

The examples listed on this page are code samples written in Java (SDK V1) that demonstrate how to interact with the AWS Step Functions. For more information, see the AWS SDK for Java Developer Guide and the AWS Step Functions Developer Guide.

How do I invoke a lambda function from Java code?

To invoke this function from Java code, we’ll first define POJOs representing the input and output JSON: Next we’ll define an interface representing our microservice, and annotate it with the name of the Lambda function to invoke when it’s called:

How to build a Lambda flow with step function?

With the Step Function, we can build a flow in multiple ways. I can use the Parallel state, sending the same request to all the Lambda functions, and each of them will take care to discard the event. So, for example, if the input contains Monday, all the other Lambda like Tuesday will discard the request.


2 Answers

Happy to inform you that I found the solution. The above code I mentioned is for creating a new state machine and trying to run the newly created state machine from java lambda. For my scenario that is just call a step function which is already created in aws step function please follow the below steps.

First, add dependency in pom.xml

<dependency>
   <groupId>com.amazonaws</groupId>
   <artifactId>aws-java-sdk-stepfunctions</artifactId>
   <version>1.11.285</version>

then use the below code to call a step function from your java

  awsStepfunctionClient.startExecution(StartExecutionRequest);
like image 199
ShaiNe Ram Avatar answered Oct 27 '22 01:10

ShaiNe Ram


For the AWS Java SDK v2, the dependency for the pom is:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sfn</artifactId>
    <version>2.16.59</version>
<dependency>

Then use the standard builder pattern for the client (and StartExecutionRequest object) to trigger your execution:

SfnClient sfc = SfnClient.builder()
        .region()
        .build();

sfc.startExecution(startExecutionRequest);
like image 36
jmaes12345 Avatar answered Oct 27 '22 00:10

jmaes12345