So I took the Hello World Step function example and wanted a Lambda to trigger the execution
My Lambda code is as below:
The code invokes a Step Function. We need the Step function ARN
The startExecution method is blocking and returns (in success) an executionArn, that can be used to find the execution in AWS console.
The describeExecution method helps identify the status of the execution. A response of "SUCCEEDED" indicates that the step function succeeded.
I ran the lambda through the test button on AWS:
The step by step execution was
I also wanted to pass some inputs to my step function from Lambda. The new version of Hello World takes an input:
The only change to my Lambda was to change the Step Function StartExecutionRequest to add input:
My Lambda code is as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | public class StepFnExecutorLambda implements RequestHandler<String, Boolean> { private AWSStepFunctions awsStepFunctionsClient; public StepFnExecutorLambda() { AWSCredentials awsCredentials = new BasicAWSCredentials("AccessKey", "SecretKey"); AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials); ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setClientExecutionTimeout(2 * 60 * 1000);//2 mins awsStepFunctionsClient = AWSStepFunctionsClient.builder() .withCredentials(awsCredentialsProvider) .withRegion(Regions.US_EAST_1) .withClientConfiguration(clientConfiguration) .build(); } @Override public Boolean handleRequest(String input, Context context) { LambdaLogger logger = context.getLogger(); logger.log("In Handler: Executing " + context.getFunctionName() + ", " + context.getFunctionVersion()); logger.log(input); //This input is not really used now // Start state machine StartExecutionRequest startExecutionRequest = new StartExecutionRequest(); startExecutionRequest.setStateMachineArn("arn:aws:states:us-east-1:360767005075:stateMachine:TestStateMachine"); StartExecutionResult startExecutionResult = awsStepFunctionsClient.startExecution(startExecutionRequest); String executionArn = startExecutionResult.getExecutionArn(); logger.log("Execution started with arn " + executionArn); //Check Step Execution Completion String status = ""; do { DescribeExecutionRequest describeExecutionRequest = new DescribeExecutionRequest(); describeExecutionRequest.setExecutionArn(executionArn); DescribeExecutionResult describeExecutionResult = awsStepFunctionsClient.describeExecution(describeExecutionRequest); status = describeExecutionResult.getStatus(); logger.log("Execution with arn " + executionArn + " returned status " + status); } while ("RUNNING".equals(status)); //return execution arn logger.log("Final Status " + executionArn + " : " + status); return "SUCCEEDED".equals(status); } } |
Amazon Resource Names (ARNs) uniquely identify AWS resources. We require an
ARN when you need to specify a resource unambiguously across all of AWS, such as in IAM policies, Amazon Relational Database Service (Amazon RDS) tags, and API calls. |
START RequestId: 58652a12-181e-48f9-a951-208d4abbbb83 Version: $LATEST In Handler: Executing testStepFn, $LATEST key1 Execution started with arn arn:aws:states:us-east-1:360767005075:execution:TestStateMachine:4ad95d88-0064-4079-87b8-1252d7a09b7d Execution with arn arn:aws:states:us-east-1:360767005075:execution:TestStateMachine:4ad95d88-0064-4079-87b8-1252d7a09b7d returned status RUNNING Execution with arn arn:aws:states:us-east-1:360767005075:execution:TestStateMachine:4ad95d88-0064-4079-87b8-1252d7a09b7d returned status SUCCEEDED Final Status arn:aws:states:us-east-1:360767005075:execution:TestStateMachine:4ad95d88-0064-4079-87b8-1252d7a09b7d : SUCCEEDED END RequestId: 58652a12-181e-48f9-a951-208d4abbbb83 REPORT RequestId: 58652a12-181e-48f9-a951-208d4abbbb83 Duration: 4118.52 ms Billed Duration: 4200 ms Memory Size: 512 MB Max Memory Used: 125 MB Init Duration: 1573.71 msLooking at the execution ARN on Step functions Console
The step by step execution was
I also wanted to pass some inputs to my step function from Lambda. The new version of Hello World takes an input:
The only change to my Lambda was to change the Step Function StartExecutionRequest to add input:
StartExecutionRequest startExecutionRequest = new StartExecutionRequest(); startExecutionRequest.setStateMachineArn("arn:aws:states:us-east-1:466170491455:stateMachine:TestStateMachine"); startExecutionRequest.setInput("{\"IsHelloWorldExample\": true}");This code ran cleanly on execution:
START RequestId: 47c62fe2-8fdd-4fc1-8185-00b388519109 Version: $LATEST In Handler: Executing InvokeStepFn, $LATEST Help Execution started with arn arn:aws:states:us-east-1:466170491455:execution:TestStateMachine:STEP_FN_REQ_ID Execution with arn arn:aws:states:us-east-1:466170491455:execution:TestStateMachine:STEP_FN_REQ_ID returned status RUNNING ... Execution with arn arn:aws:states:us-east-1:466170491455:execution:TestStateMachine:STEP_FN_REQ_ID returned status SUCCEEDED Final Status arn:aws:states:us-east-1:466170491455:execution:TestStateMachine:STEP_FN_REQ_ID : SUCCEEDED END RequestId: 47c62fe2-8fdd-4fc1-8185-00b388519109 REPORT RequestId: 47c62fe2-8fdd-4fc1-8185-00b388519109 Duration: 7517.94 ms Billed Duration: 7600 ms Memory Size: 512 MB Max Memory Used: 126 MB Init Duration: 1557.02 ms
No comments:
Post a Comment