feat(stepfunctions): add UpdateStateMachine and update in place on stack updates (#1867)

* feat(stepfunctions): add UpdateStateMachine and update in place on stack updates

* test(dynamodb): retain Quarkus port for cleanup

* chore: rerun CI after download outage

* fix(cloudformation): preserve log group on failed rename

* fix(cloudformation): scope update rollback to attempted resources

* fix(cloudformation): delete resources after stack updates

* test(cloudformation): wire cloudfront fixture dependency

* fix: include Step Functions update date in describe response

* fix: preserve Step Functions describe response shape

* fix: preserve generated Step Functions names
This commit is contained in:
Alex Banna authored and GitHub committed 2026-08-13 22:36:15 -04:00
1 parent da5ff910b4
commit 97ac3cabb5
15 files changed
+4569 -119

No files matched your search

@@ -0,0 +1,162 @@
package com.floci.test;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.DescribeStackResourcesRequest;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.cloudformation.model.StackResource;
import software.amazon.awssdk.services.sfn.SfnClient;
import software.amazon.awssdk.services.sfn.model.DescribeStateMachineResponse;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("CloudFormation Step Functions update")
class CloudFormationStepFunctionsUpdateTest {
private static final Logger LOGGER =
Logger.getLogger(CloudFormationStepFunctionsUpdateTest.class.getName());
private static final String ROLE_ARN =
"arn:aws:iam::000000000000:role/cfn-step-functions-update-role";
private static CloudFormationClient cloudFormation;
private static SfnClient stepFunctions;
private static String stackName;
@BeforeAll
static void setup() {
cloudFormation = TestFixtures.cloudFormationClient();
stepFunctions = TestFixtures.sfnClient();
stackName = TestFixtures.uniqueName("cfn-sfn-update");
}
@AfterAll
static void cleanup() {
if (cloudFormation != null) {
if (stackName != null) {
try {
cloudFormation.deleteStack(request -> request.stackName(stackName));
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to clean up CloudFormation update test", e);
}
}
cloudFormation.close();
}
if (stepFunctions != null) {
stepFunctions.close();
}
}
@Test
void sdkUpdatesStateMachineInPlace() throws InterruptedException {
String stateMachineName = TestFixtures.uniqueName("cfn-managed-sfn");
String initialDefinition =
"{\"StartAt\":\"Initial\",\"States\":{\"Initial\":"
+ "{\"Type\":\"Pass\",\"End\":true}}}";
String updatedDefinition =
"{\"StartAt\":\"Updated\",\"States\":{\"Updated\":"
+ "{\"Type\":\"Pass\",\"End\":true}}}";
cloudFormation.createStack(request -> request
.stackName(stackName)
.templateBody(template(
stateMachineName, initialDefinition, "initial")));
assertThat(waitForTerminalStatus(stackName, 30))
.isEqualTo("CREATE_COMPLETE");
StackResource initialResource = stateMachineResource(stackName);
String stateMachineArn = initialResource.physicalResourceId();
DescribeStateMachineResponse initial =
stepFunctions.describeStateMachine(
request -> request.stateMachineArn(stateMachineArn));
cloudFormation.updateStack(request -> request
.stackName(stackName)
.templateBody(template(
stateMachineName, updatedDefinition, "updated")));
assertThat(waitForTerminalStatus(stackName, 30))
.isEqualTo("UPDATE_COMPLETE");
StackResource updatedResource = stateMachineResource(stackName);
assertThat(updatedResource.physicalResourceId())
.isEqualTo(stateMachineArn);
assertThat(updatedResource.resourceStatusAsString())
.isEqualTo("UPDATE_COMPLETE");
DescribeStateMachineResponse updated =
stepFunctions.describeStateMachine(
request -> request.stateMachineArn(stateMachineArn));
assertThat(updated.definition()).isEqualTo(updatedDefinition);
assertThat(updated.revisionId()).isNotEqualTo(initial.revisionId());
}
private static StackResource stateMachineResource(String name) {
List<StackResource> resources = cloudFormation.describeStackResources(
DescribeStackResourcesRequest.builder()
.stackName(name)
.build()).stackResources();
return resources.stream()
.filter(resource -> "AWS::StepFunctions::StateMachine"
.equals(resource.resourceType()))
.findFirst()
.orElseThrow();
}
private static String waitForTerminalStatus(
String name, int maxSeconds) throws InterruptedException {
long deadline = System.currentTimeMillis() + maxSeconds * 1000L;
while (System.currentTimeMillis() < deadline) {
List<Stack> stacks = cloudFormation.describeStacks(
DescribeStacksRequest.builder()
.stackName(name)
.build()).stacks();
if (!stacks.isEmpty()) {
String status = stacks.get(0).stackStatusAsString();
if (!status.endsWith("_IN_PROGRESS")) {
return status;
}
}
Thread.sleep(250);
}
throw new AssertionError(
"Stack " + name
+ " did not reach a terminal state within "
+ maxSeconds + " seconds");
}
private static String template(
String stateMachineName,
String definition,
String marker) {
String escapedDefinition = definition.replace("\\", "\\\\")
.replace("\"", "\\\"");
return """
{
"Resources": {
"StateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"StateMachineName": "%s",
"RoleArn": "%s",
"DefinitionString": "%s"
}
}
},
"Outputs": {
"Marker": {"Value": "%s"}
}
}
""".formatted(
stateMachineName,
ROLE_ARN,
escapedDefinition,
marker);
}
}
@@ -0,0 +1,92 @@
package com.floci.test;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.sfn.SfnClient;
import software.amazon.awssdk.services.sfn.model.DescribeStateMachineResponse;
import software.amazon.awssdk.services.sfn.model.IncludedData;
import software.amazon.awssdk.services.sfn.model.LogLevel;
import software.amazon.awssdk.services.sfn.model.UpdateStateMachineResponse;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("SFN UpdateStateMachine")
class StepFunctionsUpdateStateMachineTest {
private static final Logger LOGGER = Logger.getLogger(StepFunctionsUpdateStateMachineTest.class.getName());
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/service-role/test-role";
private static final String INITIAL_DEFINITION = """
{"StartAt":"Initial","States":{"Initial":{"Type":"Pass","End":true}}}
""".strip();
private static final String UPDATED_DEFINITION = """
{"StartAt":"Updated","States":{"Updated":{"Type":"Pass","End":true}}}
""".strip();
private static SfnClient sfn;
private static String stateMachineArn;
@BeforeAll
static void setup() {
sfn = TestFixtures.sfnClient();
stateMachineArn = sfn.createStateMachine(b -> b
.name(TestFixtures.uniqueName("update-sm"))
.definition(INITIAL_DEFINITION)
.roleArn(ROLE_ARN))
.stateMachineArn();
}
@AfterAll
static void cleanup() {
if (sfn != null) {
if (stateMachineArn != null) {
try {
sfn.deleteStateMachine(b -> b.stateMachineArn(stateMachineArn));
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to clean up Step Functions update compatibility test", e);
}
}
sfn.close();
}
}
@Test
void sdkUpdatesAndDescribesStateMachine() {
UpdateStateMachineResponse update = sfn.updateStateMachine(b -> b
.stateMachineArn(stateMachineArn)
.definition(UPDATED_DEFINITION)
.roleArn(ROLE_ARN)
.loggingConfiguration(logging -> logging
.level(LogLevel.OFF)
.includeExecutionData(false))
.tracingConfiguration(tracing -> tracing.enabled(true))
.publish(true)
.versionDescription("compatibility snapshot"));
assertThat(update.updateDate()).isNotNull();
assertThat(update.revisionId()).isNotBlank();
DescribeStateMachineResponse describe = sfn.describeStateMachine(b -> b
.stateMachineArn(stateMachineArn));
assertThat(describe.definition()).isEqualTo(UPDATED_DEFINITION);
assertThat(describe.roleArn()).isEqualTo(ROLE_ARN);
assertThat(describe.revisionId()).isEqualTo(update.revisionId());
assertThat(describe.loggingConfiguration().level()).isEqualTo(LogLevel.OFF);
assertThat(describe.tracingConfiguration().enabled()).isTrue();
DescribeStateMachineResponse version = sfn.describeStateMachine(b -> b
.stateMachineArn(update.stateMachineVersionArn()));
assertThat(version.stateMachineArn()).isEqualTo(update.stateMachineVersionArn());
assertThat(version.definition()).isEqualTo(UPDATED_DEFINITION);
assertThat(version.description()).isEqualTo("compatibility snapshot");
DescribeStateMachineResponse metadata = sfn.describeStateMachine(b -> b
.stateMachineArn(update.stateMachineVersionArn())
.includedData(IncludedData.METADATA_ONLY));
assertThat(metadata.definition()).isEqualTo("{}");
}
}