fix(stepfunctions): wrap optimized lambda:invoke result in the Invoke envelope (#2165)

A Task using the arn:aws:states:::lambda:invoke integration returned the
Lambda function's return value directly as the task result, with no
ExecutedVersion, Payload or StatusCode key. Real Step Functions nests the
function output inside the Invoke response metadata for this integration,
which is why $.Payload and the JSONata $states.result.Payload are the
documented ways to read a Lambda result.

Against the unwrapped result those references resolved to nothing, the
state still succeeded, and the execution finished SUCCEEDED carrying
silently-empty data — a pipeline accumulating results across stages looked
healthy while producing an empty result.

Both the direct-ARN branch and the optimized branch shared one block in
invokeResource(), so the parsed function output was returned raw for both.
Gate the wrap to the optimized branch: it now returns ExecutedVersion (from
the resolved function's version, matching what LambdaService.invoke already
reports), Payload (the parsed function output) and StatusCode (from the
invoke result). A directly specified function ARN keeps returning only the
function output, which is the documented behavior for that form.

SdkHttpMetadata and SdkResponseMetadata are deliberately not emitted. Their
contents are artifacts of a real HTTPS call to the Lambda service — Date,
X-Amzn-Trace-Id, RequestId, Content-Length — and floci invokes in process,
so any value it put there would be invented.

The .waitForTaskToken flow is unaffected: executeTaskState overwrites the
task result with the callback payload, so the envelope is discarded for
that pattern, as it should be.

Closes #2110
This commit is contained in:
soreavis authored and GitHub committed 2026-08-13 00:34:57 -04:00
1 parent 988696b057
commit 45811768b2
2 files changed
+252 -3

No files matched your search

@@ -501,12 +501,14 @@ public class AslExecutor {
// Support Lambda resources: direct ARN or optimized integration
String functionName = null;
JsonNode lambdaPayload = input;
boolean optimizedLambdaInvoke = false;
if (resource.contains(":lambda:") && resource.contains(":function:")) {
// Direct Lambda ARN: arn:aws:lambda:region:account:function:name[:qualifier]
functionName = extractLambdaFunctionName(resource);
} else if (resource.equals("arn:aws:states:::lambda:invoke")) {
// Optimized Lambda integration — function name and payload come from resolved input
optimizedLambdaInvoke = true;
String fnRef = input.path("FunctionName").asText(null);
if (fnRef != null) {
functionName = extractLambdaFunctionName(fnRef);
@@ -533,10 +535,20 @@ public class AslExecutor {
}
byte[] responseBytes = result.getPayload();
if (responseBytes != null && responseBytes.length > 0) {
return objectMapper.readTree(responseBytes);
JsonNode functionOutput = responseBytes != null && responseBytes.length > 0
? objectMapper.readTree(responseBytes)
: NullNode.getInstance();
// A direct function ARN yields only the function output, while the optimized
// integration nests it in the Invoke response metadata.
if (!optimizedLambdaInvoke) {
return functionOutput;
}
return NullNode.getInstance();
ObjectNode invokeResponse = objectMapper.createObjectNode();
invokeResponse.put("ExecutedVersion", fn.getVersion());
invokeResponse.set("Payload", functionOutput);
invokeResponse.put("StatusCode", result.getStatusCode());
return invokeResponse;
}
// DynamoDB optimized integrations (4 actions)
@@ -0,0 +1,237 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.mutiny.core.Vertx;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Regression tests for the shape of a Lambda Task result.
*
* AWS returns the Invoke response envelope (ExecutedVersion / Payload / StatusCode) for the
* optimized "arn:aws:states:::lambda:invoke" integration, but only the function output for a
* directly specified function ARN. Reading a Lambda result through $.Payload or
* $states.result.Payload — the documented way — depends on that distinction.
*/
@QuarkusTest
class AslExecutorLambdaInvokeResultTest {
private static final String REGION = "us-east-2";
private static final String ACCOUNT = "000000000000";
private static final String FUNCTION_NAME = "echo-lambda";
private static final String FUNCTION_ARN =
"arn:aws:lambda:%s:%s:function:%s".formatted(REGION, ACCOUNT, FUNCTION_NAME);
private final ObjectMapper objectMapper = new ObjectMapper();
private AslExecutor executor;
@Inject
Vertx vertx;
@BeforeEach
void setUp() throws Exception {
LambdaExecutorService lambdaExecutor = mock(LambdaExecutorService.class);
LambdaFunctionStore functionStore = mock(LambdaFunctionStore.class);
LambdaFunction function = new LambdaFunction();
function.setFunctionName(FUNCTION_NAME);
function.setFunctionArn(FUNCTION_ARN);
when(functionStore.get(REGION, FUNCTION_NAME)).thenReturn(Optional.of(function));
when(lambdaExecutor.invoke(eq(function), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
JsonNode event = objectMapper.readTree((byte[]) invocation.getArgument(1));
byte[] output = objectMapper.writeValueAsBytes(objectMapper.createObjectNode()
.put("marker", "RET")
.set("echo", event));
return new InvokeResult(200, null, output, null, "echo-request");
});
executor = new AslExecutor(
lambdaExecutor,
functionStore,
mock(DynamoDbService.class),
mock(DynamoDbJsonHandler.class),
mock(SqsJsonHandler.class),
mock(io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler.class),
mock(io.github.hectorvent.floci.services.ec2.Ec2Service.class),
mock(S3Service.class),
mock(io.github.hectorvent.floci.services.ecs.EcsService.class),
mock(io.github.hectorvent.floci.services.ecs.EcsJsonHandler.class),
objectMapper,
new JsonataEvaluator(objectMapper),
mock(Instance.class), mock(EmulatorConfig.class), vertx);
}
@Test
void optimizedInvokeNestsFunctionOutputInTheInvokeResponseEnvelope() throws Exception {
Execution execution = run("""
{
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {"FunctionName": "%s", "Payload": {"in": 1}},
"End": true
}
}
}
""".formatted(FUNCTION_ARN));
assertEquals("SUCCEEDED", execution.getStatus());
JsonNode output = objectMapper.readTree(execution.getOutput());
assertEquals("$LATEST", output.path("ExecutedVersion").asText());
assertTrue(output.path("StatusCode").isInt());
assertEquals(200, output.path("StatusCode").asInt());
assertEquals("RET", output.path("Payload").path("marker").asText());
assertEquals(1, output.path("Payload").path("echo").path("in").asInt());
assertFalse(output.has("marker"));
assertEquals(3, output.size());
}
@Test
void directFunctionArnReturnsOnlyTheFunctionOutput() throws Exception {
Execution execution = run("""
{
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "%s",
"End": true
}
}
}
""".formatted(FUNCTION_ARN));
assertEquals("SUCCEEDED", execution.getStatus());
JsonNode output = objectMapper.readTree(execution.getOutput());
assertEquals("RET", output.path("marker").asText());
assertEquals(1, output.path("echo").path("in").asInt());
assertFalse(output.has("Payload"));
assertFalse(output.has("StatusCode"));
assertFalse(output.has("ExecutedVersion"));
}
@Test
void resultSelectorReadsPayloadFromOptimizedInvokeResult() throws Exception {
Execution execution = run("""
{
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {"FunctionName": "%s", "Payload": {"in": 1}},
"ResultSelector": {"marker.$": "$.Payload.marker", "status.$": "$.StatusCode"},
"ResultPath": "$.lambda",
"End": true
}
}
}
""".formatted(FUNCTION_ARN));
assertEquals("SUCCEEDED", execution.getStatus());
JsonNode output = objectMapper.readTree(execution.getOutput());
assertEquals("RET", output.path("lambda").path("marker").asText());
assertEquals(200, output.path("lambda").path("status").asInt());
assertEquals(1, output.path("in").asInt());
}
@Test
void outputPathPayloadUnwrapsTheOptimizedInvokeResult() throws Exception {
Execution execution = run("""
{
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {"Payload.$": "$", "FunctionName": "%s"},
"End": true
}
}
}
""".formatted(FUNCTION_ARN));
assertEquals("SUCCEEDED", execution.getStatus());
JsonNode output = objectMapper.readTree(execution.getOutput());
assertEquals("RET", output.path("marker").asText());
assertEquals(1, output.path("echo").path("in").asInt());
}
@Test
void jsonataOutputResolvesStatesResultPayload() throws Exception {
Execution execution = run("""
{
"QueryLanguage": "JSONata",
"StartAt": "Call",
"States": {
"Call": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Arguments": {"FunctionName": "%s", "Payload": {"in": 1}},
"Output": "{%% $states.result.Payload %%}",
"End": true
}
}
}
""".formatted(FUNCTION_ARN));
assertEquals("SUCCEEDED", execution.getStatus());
JsonNode output = objectMapper.readTree(execution.getOutput());
assertEquals("RET", output.path("marker").asText());
assertEquals(1, output.path("echo").path("in").asInt());
}
private Execution run(String definition) {
StateMachine stateMachine = new StateMachine();
stateMachine.setName("lambda-invoke-result");
stateMachine.setStateMachineArn(
"arn:aws:states:%s:%s:stateMachine:lambda-invoke-result".formatted(REGION, ACCOUNT));
stateMachine.setRoleArn("arn:aws:iam::%s:role/test-role".formatted(ACCOUNT));
stateMachine.setDefinition(definition);
Execution execution = new Execution();
execution.setName("lambda-invoke-result-execution");
execution.setExecutionArn("arn:aws:states:%s:%s:execution:lambda-invoke-result:run-1"
.formatted(REGION, ACCOUNT));
execution.setStateMachineArn(stateMachine.getStateMachineArn());
execution.setInput("{\"in\":1}");
List<HistoryEvent> history = new ArrayList<>();
executor.executeSync(stateMachine, execution, history, (updated, events) -> {
});
return execution;
}
}