mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): update nested stacks in place (#3859)
This commit is contained in:
1 parent
29b601834b
commit
fbc0d4d3f2
2 files changed
+79
-6
No files matched your search
+16
-6
@@ -1206,7 +1206,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
if ("AWS::CloudFormation::Stack".equals(type)) {
|
||||
resource = executeNestedStack(stack, logicalId,
|
||||
props.isMissingNode() ? null : props,
|
||||
engine, region, accountId, isCreate);
|
||||
engine, region, accountId, isCreate, previousResource);
|
||||
} else {
|
||||
resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props,
|
||||
engine, region, accountId, stack.getStackName(),
|
||||
@@ -2293,7 +2293,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
|
||||
private StackResource executeNestedStack(Stack parentStack, String logicalId, JsonNode props,
|
||||
CloudFormationTemplateEngine engine, String region,
|
||||
String accountId, boolean isCreate) {
|
||||
String accountId, boolean isCreate,
|
||||
StackResource previousResource) {
|
||||
StackResource resource = new StackResource();
|
||||
resource.setLogicalId(logicalId);
|
||||
resource.setResourceType("AWS::CloudFormation::Stack");
|
||||
@@ -2308,9 +2309,18 @@ public class CloudFormationService implements ResourceProvider {
|
||||
String childTemplate = fetchTemplateFromS3(templateUrl);
|
||||
String childStackName = parentStack.getStackName() + "-" + logicalId;
|
||||
|
||||
Stack childStack = newStack(childStackName, region, accountId);
|
||||
childStack.setStatus("CREATE_IN_PROGRESS");
|
||||
stacks.put(stackKey(accountId, childStackName, region), childStack);
|
||||
Stack childStack = null;
|
||||
boolean childCreate = isCreate || previousResource == null
|
||||
|| previousResource.getPhysicalId() == null;
|
||||
if (!childCreate) {
|
||||
childStack = resolveStack(previousResource.getPhysicalId(), region, accountId);
|
||||
childCreate = childStack == null;
|
||||
}
|
||||
if (childCreate) {
|
||||
childStack = newStack(childStackName, region, accountId);
|
||||
childStack.setStatus("CREATE_IN_PROGRESS");
|
||||
stacks.put(stackKey(accountId, childStackName, region), childStack);
|
||||
}
|
||||
|
||||
Map<String, String> childParams = new LinkedHashMap<>();
|
||||
if (props != null && props.has("Parameters") && props.get("Parameters").isObject()) {
|
||||
@@ -2318,7 +2328,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
childParams.put(e.getKey(), engine.resolve(e.getValue())));
|
||||
}
|
||||
|
||||
executeTemplate(childStack, childTemplate, childParams, isCreate, region, accountId);
|
||||
executeTemplate(childStack, childTemplate, childParams, childCreate, region, accountId);
|
||||
|
||||
resource.setPhysicalId(childStack.getStackId());
|
||||
resource.getAttributes().put("Arn", childStack.getStackId());
|
||||
|
||||
+63
@@ -366,6 +366,50 @@ class CloudFormationDeletionPolicyIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateNestedStackReusesChildStackAndUpdatesNamedLambda() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String functionName = "cfn-nested-update-func-" + suffix;
|
||||
String bucketName = "nested-stack-templates-" + suffix;
|
||||
String templateUrl = bucketName + "/child-lambda-update.json";
|
||||
String parentTemplate = """
|
||||
{
|
||||
"Resources": {
|
||||
"ChildStack": {
|
||||
"Type": "AWS::CloudFormation::Stack",
|
||||
"Properties": { "TemplateURL": "http://localhost/%s" }
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(templateUrl);
|
||||
String stackName = "parent-nested-lambda-update-" + suffix;
|
||||
|
||||
given().header("Authorization", CUSTOM_AUTH)
|
||||
.when().put("/" + bucketName).then().statusCode(200);
|
||||
given().header("Authorization", CUSTOM_AUTH)
|
||||
.contentType("application/json").body(nestedLambdaTemplate(functionName, 3))
|
||||
.when().put("/" + templateUrl).then().statusCode(200);
|
||||
String parentStackId = createStack(stackName, parentTemplate, CUSTOM_AUTH);
|
||||
try {
|
||||
awaitStackStatus(parentStackId, "CREATE_COMPLETE", CUSTOM_AUTH);
|
||||
String childStackId = getNestedStackId(parentStackId, "ChildStack", CUSTOM_AUTH);
|
||||
|
||||
given().header("Authorization", CUSTOM_AUTH)
|
||||
.contentType("application/json").body(nestedLambdaTemplate(functionName, 9))
|
||||
.when().put("/" + templateUrl).then().statusCode(200);
|
||||
updateStack(stackName, parentTemplate, CUSTOM_AUTH);
|
||||
awaitStackStatus(parentStackId, "UPDATE_COMPLETE", CUSTOM_AUTH);
|
||||
|
||||
assertThat(getNestedStackId(parentStackId, "ChildStack", CUSTOM_AUTH), equalTo(childStackId));
|
||||
given().header("Authorization", CUSTOM_AUTH)
|
||||
.when().get("/2015-03-31/functions/" + functionName)
|
||||
.then().statusCode(200)
|
||||
.body("Configuration.Timeout", equalTo(9));
|
||||
} finally {
|
||||
deleteStack(stackName);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nestedStackWithNonEmptyBucketFailsDeletionOnUpdate_leavesChildAsDeleteFailedAndTracksInParent() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
@@ -967,6 +1011,25 @@ class CloudFormationDeletionPolicyIntegrationTest {
|
||||
return xml.substring(physStart, physEnd);
|
||||
}
|
||||
|
||||
private static String nestedLambdaTemplate(String functionName, int timeout) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Function": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"FunctionName": "%s",
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"Timeout": %d,
|
||||
"Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(functionName, timeout);
|
||||
}
|
||||
|
||||
private static String createStack(String stackName, String template) {
|
||||
return createStack(stackName, template, null);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user