Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-cdk): display status reason on failure #609

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})` : '');
}
}