|
62 | 62 | import com.amazonaws.services.sqs.model.ReceiveMessageRequest; |
63 | 63 | import com.amazonaws.services.sqs.model.SendMessageRequest; |
64 | 64 | import com.amazonaws.services.stepfunctions.AWSStepFunctionsClient; |
65 | | -import com.amazonaws.services.stepfunctions.model.CreateStateMachineRequest; |
66 | | -import com.amazonaws.services.stepfunctions.model.StateMachineType; |
| 65 | +import com.amazonaws.services.stepfunctions.model.*; |
| 66 | + |
67 | 67 | import java.io.File; |
68 | 68 | import java.io.IOException; |
69 | 69 | import java.net.http.HttpClient; |
@@ -671,94 +671,89 @@ private static void setupStepFunctions() { |
671 | 671 | .withEndpointConfiguration(endpointConfiguration) |
672 | 672 | .build(); |
673 | 673 |
|
| 674 | + var sfnName = "test-state-machine"; |
| 675 | + String existingStateMachineArn = null; |
| 676 | + var listRequest = new ListStateMachinesRequest(); |
| 677 | + var listResponse = stepFunctionsClient.listStateMachines(listRequest); |
| 678 | + existingStateMachineArn = listResponse.getStateMachines().stream() |
| 679 | + .filter(machine -> machine.getName().equals(sfnName)) |
| 680 | + .findFirst() |
| 681 | + .map(StateMachineListItem::getStateMachineArn) |
| 682 | + .orElse(null); |
| 683 | + |
| 684 | + if (existingStateMachineArn != null) { |
| 685 | + logger.info("State machine already exists, skipping creation"); |
| 686 | + } else { |
| 687 | + logger.info("State machine not found, creating a new one"); |
| 688 | + String trustPolicy = |
| 689 | + "{" |
| 690 | + + "\"Version\": \"2012-10-17\"," |
| 691 | + + "\"Statement\": [" |
| 692 | + + " {" |
| 693 | + + " \"Effect\": \"Allow\"," |
| 694 | + + " \"Principal\": {" |
| 695 | + + " \"Service\": \"states.amazonaws.com\"" |
| 696 | + + " }," |
| 697 | + + " \"Action\": \"sts:AssumeRole\"" |
| 698 | + + " }" |
| 699 | + + "]}"; |
| 700 | + var roleRequest = |
| 701 | + new CreateRoleRequest() |
| 702 | + .withRoleName(sfnName + "-role") |
| 703 | + .withAssumeRolePolicyDocument(trustPolicy); |
| 704 | + var roleArn = iamClient.createRole(roleRequest).getRole().getArn(); |
| 705 | + String policyDocument = |
| 706 | + "{" |
| 707 | + + "\"Version\": \"2012-10-17\"," |
| 708 | + + "\"Statement\": [" |
| 709 | + + " {" |
| 710 | + + " \"Effect\": \"Allow\"," |
| 711 | + + " \"Action\": [" |
| 712 | + + " \"lambda:InvokeFunction\"" |
| 713 | + + " ]," |
| 714 | + + " \"Resource\": [" |
| 715 | + + " \"*\"" |
| 716 | + + " ]" |
| 717 | + + " }" |
| 718 | + + "]}"; |
| 719 | + var policyRequest = |
| 720 | + new PutRolePolicyRequest() |
| 721 | + .withRoleName(sfnName + "-role") |
| 722 | + .withPolicyName(sfnName + "-policy") |
| 723 | + .withPolicyDocument(policyDocument); |
| 724 | + iamClient.putRolePolicy(policyRequest); |
| 725 | + // Simple state machine definition - a basic Hello World |
| 726 | + String stateMachineDefinition = |
| 727 | + "{" |
| 728 | + + " \"Comment\": \"A Hello World example of the Amazon States Language using a Pass state\"," |
| 729 | + + " \"StartAt\": \"HelloWorld\"," |
| 730 | + + " \"States\": {" |
| 731 | + + " \"HelloWorld\": {" |
| 732 | + + " \"Type\": \"Pass\"," |
| 733 | + + " \"Result\": \"Hello World!\"," |
| 734 | + + " \"End\": true" |
| 735 | + + " }" |
| 736 | + + " }" |
| 737 | + + "}"; |
| 738 | + // Create state machine using the role |
| 739 | + var sfnRequest = |
| 740 | + new CreateStateMachineRequest() |
| 741 | + .withName(sfnName) |
| 742 | + .withRoleArn(roleArn) |
| 743 | + .withDefinition(stateMachineDefinition) |
| 744 | + .withType(StateMachineType.STANDARD); |
| 745 | + var createResponse = stepFunctionsClient.createStateMachine(sfnRequest); |
| 746 | + existingStateMachineArn = createResponse.getStateMachineArn(); |
| 747 | + } |
| 748 | + |
| 749 | + String finalExistingStateMachineArn = existingStateMachineArn; |
674 | 750 | get( |
675 | | - "/sfn/createstatemachine/:name", |
| 751 | + "/sfn/describestatemachine/:name", |
676 | 752 | (req, res) -> { |
677 | 753 | var name = req.params(":name"); |
678 | | - String trustPolicy = |
679 | | - "{" |
680 | | - + "\"Version\": \"2012-10-17\"," |
681 | | - + "\"Statement\": [" |
682 | | - + " {" |
683 | | - + " \"Effect\": \"Allow\"," |
684 | | - + " \"Principal\": {" |
685 | | - + " \"Service\": \"states.amazonaws.com\"" |
686 | | - + " }," |
687 | | - + " \"Action\": \"sts:AssumeRole\"" |
688 | | - + " }" |
689 | | - + "]}"; |
690 | | - var roleRequest = |
691 | | - new CreateRoleRequest() |
692 | | - .withRoleName(name + "-role") |
693 | | - .withAssumeRolePolicyDocument(trustPolicy); |
694 | | - var roleArn = iamClient.createRole(roleRequest).getRole().getArn(); |
695 | | - String policyDocument = |
696 | | - "{" |
697 | | - + "\"Version\": \"2012-10-17\"," |
698 | | - + "\"Statement\": [" |
699 | | - + " {" |
700 | | - + " \"Effect\": \"Allow\"," |
701 | | - + " \"Action\": [" |
702 | | - + " \"lambda:InvokeFunction\"" |
703 | | - + " ]," |
704 | | - + " \"Resource\": [" |
705 | | - + " \"*\"" |
706 | | - + " ]" |
707 | | - + " }" |
708 | | - + "]}"; |
709 | | - var policyRequest = |
710 | | - new PutRolePolicyRequest() |
711 | | - .withRoleName(name + "-role") |
712 | | - .withPolicyName(name + "-policy") |
713 | | - .withPolicyDocument(policyDocument); |
714 | | - iamClient.putRolePolicy(policyRequest); |
715 | | - // Simple state machine definition - a basic Hello World |
716 | | - String stateMachineDefinition = |
717 | | - "{" |
718 | | - + " \"Comment\": \"A Hello World example of the Amazon States Language using a Pass state\"," |
719 | | - + " \"StartAt\": \"HelloWorld\"," |
720 | | - + " \"States\": {" |
721 | | - + " \"HelloWorld\": {" |
722 | | - + " \"Type\": \"Pass\"," |
723 | | - + " \"Result\": \"Hello World!\"," |
724 | | - + " \"End\": true" |
725 | | - + " }" |
726 | | - + " }" |
727 | | - + "}"; |
728 | | - // Create state machine using the role |
729 | | - var sfnRequest = |
730 | | - new CreateStateMachineRequest() |
731 | | - .withName(name) |
732 | | - .withRoleArn(roleArn) |
733 | | - .withDefinition(stateMachineDefinition) |
734 | | - .withType(StateMachineType.STANDARD); |
735 | | - stepFunctionsClient.createStateMachine(sfnRequest); |
736 | | - return ""; |
737 | | - }); |
738 | | - |
739 | | - get( |
740 | | - "/sfn/error", |
741 | | - (req, res) -> { |
742 | | - setMainStatus(400); |
743 | | - var errorClient = |
744 | | - AWSStepFunctionsClient.builder() |
745 | | - .withCredentials(CREDENTIALS_PROVIDER) |
746 | | - .withEndpointConfiguration( |
747 | | - new EndpointConfiguration( |
748 | | - "http://error.test:8080", Regions.US_WEST_2.getName())) |
749 | | - .build(); |
750 | | - |
751 | | - try { |
752 | | - String invalidDefinition = "{\"Invalid\": \"Definition\"}"; |
753 | | - errorClient.createStateMachine( |
754 | | - new CreateStateMachineRequest() |
755 | | - .withName("error-state-machine") |
756 | | - .withDefinition(invalidDefinition) |
757 | | - .withRoleArn("arn:aws:iam::invalid:role/invalid")); |
758 | | - } catch (Exception ex) { |
759 | | - logger.info("Exception Caught in Sample App"); |
760 | | - ex.printStackTrace(); |
761 | | - } |
| 754 | + var describeRequest = new DescribeStateMachineRequest() |
| 755 | + .withStateMachineArn(finalExistingStateMachineArn); |
| 756 | + stepFunctionsClient.describeStateMachine(describeRequest); |
762 | 757 | return ""; |
763 | 758 | }); |
764 | 759 | } |
|
0 commit comments