Skip to content

Commit

Permalink
feat(aws-cdk): display status reason on failure (#609)
Browse files Browse the repository at this point in the history
Make the toolkit show the StackStatusReason if deploying
or destroying failed.

Fixes #604.
  • Loading branch information
rix0rrr authored Aug 22, 2018
1 parent 0056f35 commit 128d55d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Mode } from './aws-auth/credentials';
import { ToolkitInfo } from './toolkit-info';
import { describeStack, stackExists, waitForChangeSet, waitForStack } from './util/cloudformation';
import { StackActivityMonitor } from './util/cloudformation/stack-activity-monitor';
import { StackStatus } from './util/cloudformation/stack-status';
import { SDK } from './util/sdk';

type TemplateBodyParameter = {
Expand Down Expand Up @@ -149,7 +150,8 @@ export async function destroyStack(stack: cxapi.StackInfo, sdk: SDK, deployName?
const destroyedStack = await waitForStack(cfn, deployName, false);
if (monitor) { monitor.stop(); }
if (destroyedStack && destroyedStack.StackStatus !== 'DELETE_COMPLETE') {
throw new Error(`Failed to destroy ${deployName} (current state: ${destroyedStack.StackStatus})!`);
const status = StackStatus.fromStackDescription(destroyedStack);
throw new Error(`Failed to destroy ${deployName}: ${status}`);
}
return;
}
8 changes: 4 additions & 4 deletions packages/aws-cdk/lib/api/util/cloudformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ export async function waitForStack(cfn: CloudFormation,
debug('Stack %s does not exist', stackName);
return null;
}
const status = new StackStatus(description.StackStatus);
const status = StackStatus.fromStackDescription(description);
if (!status.isStable) {
debug('Stack %s is still not stable (%s)', stackName, status.name);
debug('Stack %s is still not stable (%s)', stackName, status);
return undefined;
}
if (status.isCreationFailure) {
throw new Error(`The stack named ${stackName} failed creation, it may need to be manually deleted from the AWS console.`);
throw new Error(`The stack named ${stackName} failed creation, it may need to be manually deleted from the AWS console: ${status}`);
} else if (!status.isSuccess) {
throw new Error(`The stack named ${stackName} is in a failed state: ${status.name}`);
throw new Error(`The stack named ${stackName} is in a failed state: ${status}`);
} else if (status.isDeleted) {
if (failOnDeletedStack) { throw new Error(`The stack named ${stackName} was deleted`); }
return undefined;
Expand Down
12 changes: 11 additions & 1 deletion packages/aws-cdk/lib/api/util/cloudformation/stack-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import AWS = require('aws-sdk');

/**
* A utility class to inspect CloudFormation stack statuses.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html
*/
export class StackStatus {
constructor(readonly name: string) {}
public static fromStackDescription(description: AWS.CloudFormation.Stack) {
return new StackStatus(description.StackStatus, description.StackStatusReason);
}

constructor(public readonly name: string, public readonly reason?: string) {}

get isCreationFailure(): boolean {
return this.name === 'ROLLBACK_COMPLETE'
Expand All @@ -30,4 +36,8 @@ export class StackStatus {
get isSuccess(): boolean {
return !this.isRollback && !this.isFailure;
}

public toString(): string {
return this.name + (this.reason ? ` (${this.reason})` : '');
}
}

0 comments on commit 128d55d

Please sign in to comment.