feat(eventbridge): start Step Functions targets (#4006)

Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
vamsiakandVamsi Akavarapu authored and GitHub committed 2026-09-20 11:36:26 -04:00
1 parent 275977cb75
commit 957542d172
4 files changed
+237 -2

No files matched your search

+9 -1
View File
@@ -119,10 +119,18 @@ aws events put-targets \
--endpoint-url $AWS_ENDPOINT_URL
```
## Step Functions Targets
A matching rule can start an unqualified Step Functions state machine ARN. The target supports the
full event as the default input, plus `Input`, `InputPath`, and `InputTransformer`. Floci resolves
the state machine from the account and region in its ARN.
State machine aliases and versions are not supported as EventBridge targets.
## Current Behavior
- `PutEvents` reports success once the source bus accepts an event, so target delivery failures surface only as a `WARN` in the Floci logs.
- A `Detail` forwarded to an event bus must be a JSON object, as in AWS; anything else is dropped, including an `InputPath` selecting a scalar such as `$.detail.orderId` or an envelope carrying `"detail": null`.
- A bus ARN naming another account is forwarded under that account, so the target bus and its rules resolve there.
- Onward delivery from that bus follows each target type: SQS resolves cross-account, while Lambda, SNS, Batch and Firehose resolve in the caller's account.
- Onward delivery from that bus follows each target type: SQS and Step Functions resolve cross-account, while Lambda, SNS, Batch and Firehose resolve in the caller's account.
- An event is forwarded between buses only once, matching AWS: a bus that received an event from another bus does not forward it on to a third. The second hop is dropped with only a `WARN` rather than reported to the caller.
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.MissingNode;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.services.batch.BatchService;
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
import io.github.hectorvent.floci.services.ecs.EcsService;
@@ -20,6 +21,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -47,6 +49,7 @@ public class EventBridgeInvoker {
private final EventBridgeService eventBridgeService;
private final EcsService ecsService;
private final EcsJsonHandler ecsJsonHandler;
private final StepFunctionsService stepFunctionsService;
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
private final String baseUrl;
@@ -60,6 +63,7 @@ public class EventBridgeInvoker {
EventBridgeService eventBridgeService,
EcsService ecsService,
EcsJsonHandler ecsJsonHandler,
StepFunctionsService stepFunctionsService,
RegionResolver regionResolver,
ObjectMapper objectMapper,
EmulatorConfig config) {
@@ -71,6 +75,7 @@ public class EventBridgeInvoker {
this.eventBridgeService = eventBridgeService;
this.ecsService = ecsService;
this.ecsJsonHandler = ecsJsonHandler;
this.stepFunctionsService = stepFunctionsService;
this.regionResolver = regionResolver;
this.objectMapper = objectMapper;
this.baseUrl = config.baseUrl();
@@ -83,7 +88,7 @@ public class EventBridgeInvoker {
EmulatorConfig config) {
this(lambdaService, sqsService, snsService,
null /* batch */, null /* firehose */, null /* eventBridge */, null /* ecs */,
null /* ecsJsonHandler */, null /* regionResolver */, objectMapper, config);
null /* ecsJsonHandler */, null /* stepFunctions */, null /* regionResolver */, objectMapper, config);
}
public void invokeTarget(Target target, String eventJson, String region) {
@@ -156,6 +161,12 @@ public class EventBridgeInvoker {
firehoseService.putRecord(streamArn.accountId(), streamArn.region(), streamName, record);
}
LOG.debugv("EventBridge delivered to Firehose: {0}", arn);
} else if (isStateMachineArn(arn)) {
String targetRegion = extractRegionFromArn(arn, region);
String targetAccount = AwsArnUtils.parse(arn).accountId();
RequestScopes.runAs(targetAccount,
() -> stepFunctionsService.startExecution(arn, null, payload, targetRegion));
LOG.debugv("EventBridge started Step Functions execution: {0}", arn);
} else if (arn.contains(":events:") && arn.contains(":event-bus/")) {
if (eventBridgeService == null) {
LOG.warnv("EventBridge event-bus target missing EventBridge service: {0}", arn);
@@ -424,4 +435,13 @@ public class EventBridgeInvoker {
private static String extractRegionFromArn(String arn, String defaultRegion) {
return AwsArnUtils.regionOrDefault(arn, defaultRegion);
}
private static boolean isStateMachineArn(String arn) {
if (!AwsArnUtils.isArnFor(arn, "states")) {
return false;
}
String resource = AwsArnUtils.parse(arn).resource();
String prefix = "stateMachine:";
return resource.startsWith(prefix) && resource.indexOf(':', prefix.length()) < 0;
}
}
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -45,6 +46,7 @@ class EventBridgeInvokerTest {
private FirehoseService firehoseService;
private EventBridgeService eventBridgeService;
private EcsService ecsService;
private StepFunctionsService stepFunctionsService;
private RegionResolver regionResolver;
@BeforeEach
@@ -56,6 +58,7 @@ class EventBridgeInvokerTest {
firehoseService = mock(FirehoseService.class);
eventBridgeService = mock(EventBridgeService.class);
ecsService = mock(EcsService.class);
stepFunctionsService = mock(StepFunctionsService.class);
regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn("000000000000");
when(eventBridgeService.putEvents(anyList(), anyString(), any()))
@@ -73,12 +76,58 @@ class EventBridgeInvokerTest {
ecsService,
new EcsJsonHandler(ecsService, new ObjectMapper(),
new HostVolumePolicy(emulatorConfig)),
stepFunctionsService,
regionResolver,
new ObjectMapper(),
emulatorConfig
);
}
@Test
void invokeTarget_stateMachineTargetStartsExecutionWithDefaultEvent() {
String arn = "arn:aws:states:eu-west-1:111122223333:stateMachine:orders";
String event = "{\"detail\":{\"orderId\":\"o-42\"}}";
Target target = new Target("id1", arn, null, null);
invoker.invokeTarget(target, event, "us-east-1");
verify(stepFunctionsService).startExecution(arn, null, event, "eu-west-1");
}
@Test
void invokeTarget_stateMachineTargetUsesExplicitInput() {
String arn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders";
Target target = new Target("id1", arn, "{\"source\":\"override\"}", null);
invoker.invokeTarget(target, "{\"ignored\":true}", "us-east-1");
verify(stepFunctionsService).startExecution(
arn, null, "{\"source\":\"override\"}", "us-east-1");
}
@Test
void invokeTarget_qualifiedStateMachineTargetRemainsUnsupported() {
String versionArn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders:1";
String aliasArn = "arn:aws:states:us-east-1:000000000000:stateMachine:orders:PROD";
invoker.invokeTarget(new Target("version", versionArn, "{}", null), "{}", "us-east-1");
invoker.invokeTarget(new Target("alias", aliasArn, "{}", null), "{}", "us-east-1");
verifyNoInteractions(stepFunctionsService);
}
@Test
void invokeTarget_stateMachineStartFailureDoesNotEscapeDelivery() {
String arn = "arn:aws:states:us-east-1:000000000000:stateMachine:missing";
when(stepFunctionsService.startExecution(arn, null, "{}", "us-east-1"))
.thenThrow(new IllegalStateException("missing state machine"));
assertDoesNotThrow(() -> invoker.invokeTarget(
new Target("id1", arn, "{}", null), "{\"ignored\":true}", "us-east-1"));
verify(stepFunctionsService).startExecution(arn, null, "{}", "us-east-1");
}
@Test
void invokeTarget_lambdaTargetPreservesArnAccount() {
String arn = "arn:aws:lambda:ap-south-1:100000000012:function:cross-account-function";
@@ -0,0 +1,158 @@
package io.github.hectorvent.floci.services.eventbridge;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.services.eventbridge.model.InputTransformer;
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
import io.github.hectorvent.floci.services.eventbridge.model.Target;
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@QuarkusTest
class EventBridgeStepFunctionsIntegrationTest {
private static final String SOURCE_REGION = "us-east-1";
private static final String TARGET_REGION = "eu-west-1";
private static final String TARGET_ACCOUNT = "111122223333";
private static final String ROLE_ARN = "arn:aws:iam::111122223333:role/eventbridge-role";
private static final String DEFINITION =
"{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}";
@Inject
EventBridgeService eventBridgeService;
@Inject
StepFunctionsService stepFunctionsService;
@Inject
ObjectMapper objectMapper;
private final List<String> ruleNames = new ArrayList<>();
private final List<String> stateMachineArns = new ArrayList<>();
@AfterEach
void cleanUp() {
for (String ruleName : ruleNames) {
List<String> targetIds = eventBridgeService.listTargetsByRule(ruleName, null, SOURCE_REGION)
.stream()
.map(Target::getId)
.toList();
eventBridgeService.removeTargets(ruleName, null, targetIds, SOURCE_REGION);
eventBridgeService.deleteRule(ruleName, null, SOURCE_REGION);
}
RequestScopes.runAs(TARGET_ACCOUNT, () -> {
for (String stateMachineArn : stateMachineArns) {
stepFunctionsService.deleteStateMachine(stateMachineArn);
}
});
}
@Test
void matchingRuleStartsStateMachinesWithEachInputMode() throws Exception {
String defaultArn = createStateMachine("default");
String explicitArn = createStateMachine("explicit");
String pathArn = createStateMachine("path");
String transformerArn = createStateMachine("transformer");
String ruleName = createRule("input-modes", "{\"source\":[\"orders.created\"]}");
Target defaultTarget = new Target("default", defaultArn, null, null);
Target explicitTarget = new Target("explicit", explicitArn, "{\"mode\":\"explicit\"}", null);
Target pathTarget = new Target("path", pathArn, null, "$.detail");
Target transformerTarget = new Target("transformer", transformerArn, null, null);
transformerTarget.setInputTransformer(new InputTransformer(
Map.of("orderId", "$.detail.orderId"), "{\"order\":\"<orderId>\"}"));
assertEquals(0, eventBridgeService.putTargets(
ruleName, null,
List.of(defaultTarget, explicitTarget, pathTarget, transformerTarget),
SOURCE_REGION));
EventBridgeService.PutEventsResult unmatched = eventBridgeService.putEvents(List.of(Map.of(
"Source", "orders.ignored",
"DetailType", "OrderCreated",
"Detail", "{\"orderId\":\"o-42\"}")), SOURCE_REGION);
assertEquals(0, unmatched.failedCount());
assertEquals(0, executions(defaultArn).size());
assertEquals(0, executions(explicitArn).size());
assertEquals(0, executions(pathArn).size());
assertEquals(0, executions(transformerArn).size());
EventBridgeService.PutEventsResult matched = eventBridgeService.putEvents(List.of(Map.of(
"Source", "orders.created",
"DetailType", "OrderCreated",
"Detail", "{\"orderId\":\"o-42\",\"total\":19}")), SOURCE_REGION);
assertEquals(0, matched.failedCount());
assertNotNull(matched.entries().getFirst().get("EventId"));
Execution defaultExecution = onlyExecution(defaultArn);
JsonNode defaultInput = objectMapper.readTree(defaultExecution.getInput());
assertEquals("orders.created", defaultInput.path("source").asText());
assertEquals("OrderCreated", defaultInput.path("detail-type").asText());
assertEquals("o-42", defaultInput.path("detail").path("orderId").asText());
assertEquals(SOURCE_REGION, defaultInput.path("region").asText());
assertEquals("{\"mode\":\"explicit\"}", onlyExecution(explicitArn).getInput());
assertEquals("{\"orderId\":\"o-42\",\"total\":19}", onlyExecution(pathArn).getInput());
assertEquals("{\"order\":\"o-42\"}", onlyExecution(transformerArn).getInput());
}
@Test
void startFailureDoesNotRejectAcceptedEvent() {
String ruleName = createRule("missing-target", "{\"source\":[\"orders.failed\"]}");
Target missingTarget = new Target(
"missing",
"arn:aws:states:eu-west-1:111122223333:stateMachine:missing",
"{}",
null);
assertEquals(0, eventBridgeService.putTargets(
ruleName, null, List.of(missingTarget), SOURCE_REGION));
EventBridgeService.PutEventsResult result = eventBridgeService.putEvents(List.of(Map.of(
"Source", "orders.failed",
"DetailType", "OrderCreated",
"Detail", "{}")), SOURCE_REGION);
assertEquals(0, result.failedCount());
assertNotNull(result.entries().getFirst().get("EventId"));
}
private String createStateMachine(String suffix) {
String name = "eventbridge-" + suffix + "-" + UUID.randomUUID();
StateMachine stateMachine = RequestScopes.callAs(TARGET_ACCOUNT,
() -> stepFunctionsService.createStateMachine(
name, DEFINITION, ROLE_ARN, "STANDARD", TARGET_REGION, Map.of()));
stateMachineArns.add(stateMachine.getStateMachineArn());
return stateMachine.getStateMachineArn();
}
private String createRule(String suffix, String eventPattern) {
String name = "eventbridge-sfn-" + suffix + "-" + UUID.randomUUID();
eventBridgeService.putRule(
name, null, eventPattern, null, RuleState.ENABLED, null, null, null, SOURCE_REGION);
ruleNames.add(name);
return name;
}
private List<Execution> executions(String stateMachineArn) {
return RequestScopes.callAs(
TARGET_ACCOUNT, () -> stepFunctionsService.listExecutions(stateMachineArn));
}
private Execution onlyExecution(String stateMachineArn) {
List<Execution> executions = executions(stateMachineArn);
assertEquals(1, executions.size());
return executions.getFirst();
}
}