fix(cloudformation): delete the HTTP API whose Body import fails on create (#6982)

AWS::ApiGatewayV2::Api creates the API before it turns its Body into
routes, integrations and authorizers. When the Body cannot be read or
materialized, the Body's own rollback removes what it generated, but the
API stays: the failed resource names it without the rollback-owned
marker, so neither the stack's rollback nor DeleteStack deletes it.

The Body is now read before the API is created or updated, so a document
that cannot be read changes nothing. The materialization after the
create runs through CfnRollback.guarded, as the REST API provisioner's
steps do: an API the provision created is deleted again, or left marked
for the stack's rollback when that delete fails. An API that an update
keeps is handled as before.

Deleting an Api now tolerates one already gone, as the REST API and VPC
link deletes do. Without that, an UpdateStack that adds such an Api
ended UPDATE_ROLLBACK_FAILED: the update rollback deletes the API the
provision had already deleted. It also lets an update remove an Api that
was deleted out of band, and makes Cloud Control DeleteResource answer
SUCCESS for a missing one, as it does for REST APIs.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-29 23:08:01 -04:00
1 parent 5cfb16a2f5
commit ec3bd3184f
3 files changed
+329 -9

No files matched your search

@@ -91,7 +91,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
@Override
public void delete(StackResource resource, String region) {
switch (resource.getResourceType()) {
case API -> apiGatewayV2Service.deleteApi(region, resource.getPhysicalId());
case API -> deleteApi(region, resource.getPhysicalId());
case AUTHORIZER -> {
// Deleting an authorizer needs the api id (a create-time attribute) as well as the
// authorizer id (the physical id), which the id-only delete path cannot supply.
@@ -120,6 +120,15 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
}
}
/**
* Deletes an api, and with it every child, tolerating one already gone: removed out of band, or
* deleted by a provision whose Body failed before the update rollback deletes the resource again.
*/
private void deleteApi(String region, String apiId) {
CfnDeletes.safeDelete("API Gateway V2 API", apiId, () -> apiGatewayV2Service.deleteApi(region, apiId),
"NotFoundException");
}
@Override
public boolean hasReplacementUpdate(StackResource resource) {
return ReplacementCleanup.hasReplacement(resource);
@@ -195,11 +204,15 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
req.put("corsConfiguration", cors);
}
// Read before anything changes, so a document that cannot be read leaves the API as it was.
JsonNode body = OpenApiDocuments.resolve(props, engine, s3Service, objectMapper);
boolean created = !ctx.isUpdate();
Api api;
if (r.getPhysicalId() == null) {
if (created) {
api = apiGatewayV2Service.createApi(region, req);
} else {
api = apiGatewayV2Service.updateApi(region, r.getPhysicalId(), req);
api = apiGatewayV2Service.updateApi(region, ctx.priorPhysicalId(), req);
}
r.setPhysicalId(api.getApiId());
r.getAttributes().put("ApiId", api.getApiId());
@@ -208,17 +221,23 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
// arn:<partition>:execute-api:<region>:<account>:<apiId> for this attribute.
r.getAttributes().put("ExecuteApiArn",
AwsArnUtils.Arn.of("execute-api", region, ctx.accountId(), api.getApiId()).toString());
reconcileBodyRoutes(r, region, api.getApiId(), props, engine);
// A Body that cannot be materialized fails the provision after the API exists. Its own
// rollback has already removed what it generated, or kept the ids of what it could not, and
// on a kept API put back the routes, integrations and authorizers it replaced; what
// UpdateApi changed stays. An API this provision created is then deleted too, with every
// child it still has, or left marked for the stack's rollback when that delete fails.
CfnRollback.guarded(r, ctx, created,
() -> deleteApi(region, api.getApiId()),
() -> { },
() -> reconcileBodyRoutes(r, region, api.getApiId(), body));
}
/**
* Reconciles the routes, integrations and authorizers materialized from an ApiGatewayV2 OpenAPI
* body. Only ids stored on this resource are removed, so separately declared V2 resources stay
* outside this generated-resource lifecycle.
* body, {@code null} when the resource declares none. Only ids stored on this resource are
* removed, so separately declared V2 resources stay outside this generated-resource lifecycle.
*/
private void reconcileBodyRoutes(StackResource r, String region, String apiId, JsonNode props,
CloudFormationTemplateEngine engine) {
JsonNode body = OpenApiDocuments.resolve(props, engine, s3Service, objectMapper);
private void reconcileBodyRoutes(StackResource r, String region, String apiId, JsonNode body) {
BodyResourceState previous = null;
try {
previous = snapshotBodyResources(r, region, apiId);