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: Log when any stateful resource is in stack #530

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions src/constants/stateful-resource-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* A list of resource types that should be considered stateful
* and care should be taken when updating them to ensure they
* are not accidentally replaced as this could lead to downtime.
*
* For example, if a load balancer is accidentally replaced,
* any CNAME DNS entry for it would now be invalid and downtime
* will be incurred for the TTL of the DNS entry.
*
* Currently, this list is used to generate warnings at synth time.
* Ideally we'd add a stack policy to stop the resource being deleted,
* however this isn't currently supported in CDK.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html
* @see https://github.com/aws/aws-cdk-rfcs/issues/72
*/
export const StatefulResourceTypes: string[] = [
Copy link
Member Author

@akash1810 akash1810 May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is in draft whilst we identify items in this list (and also discuss if this PR is a good idea, as I'm not entirely sold...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions:

AWS::SNS::Topic (when used as an API for other services)
AWS::SQS::Queue
AWS::Kinesis::Stream
AWS::RDS::DBInstance

There must also be an API Gateway resource which should be included in this list (i.e. the resource which maintains the stable URL which is part of the CNAME DNS entry), but I'm struggling to figure out which resource that is!

"AWS::CertificateManager::Certificate",
"AWS::DynamoDB::Table",
"AWS::ElasticLoadBalancing::LoadBalancer",
"AWS::ElasticLoadBalancingV2::LoadBalancer",
"AWS::S3::Bucket",
];
23 changes: 23 additions & 0 deletions src/constructs/core/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import "@aws-cdk/assert/jest";

import { SynthUtils } from "@aws-cdk/assert";
import { Role, ServicePrincipal } from "@aws-cdk/aws-iam";
import { Bucket } from "@aws-cdk/aws-s3";
import { App } from "@aws-cdk/core";
import { Stage, Stages } from "../../constants";
import { TrackingTag } from "../../constants/tracking-tag";
import { Logger } from "../../utils/logger";
import { alphabeticalTags, simpleGuStackForTesting } from "../../utils/test";
import type { SynthedStack } from "../../utils/test";
import { GuParameter } from "./parameters";
import { GuStack } from "./stack";

describe("The GuStack construct", () => {
const warn = jest.spyOn(Logger, "warn");

afterEach(() => {
warn.mockReset();
});

it("requires passing the stack value as props", function () {
const stack = simpleGuStackForTesting({ stack: "some-stack" });
expect(stack.stack).toEqual("some-stack");
Expand Down Expand Up @@ -71,4 +79,19 @@ describe("The GuStack construct", () => {
"Attempting to read parameter i-do-not-exist which does not exist"
);
});

it("During the synthesise process, should advise updating with caution when it contains a stateful resource", () => {
const stack = simpleGuStackForTesting();
const bucket = new Bucket(stack, "MyBucket");
SynthUtils.toCloudFormation(stack);

// `defaultChild can technically be `undefined`.
// We know a `Bucket` has a `defaultChild` so the coalescing is just appeasing the compiler.
const cfnBucketResourcePath = bucket.node.defaultChild?.node.path ?? "";

expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledWith(
`The resource '${cfnBucketResourcePath}' of type AWS::S3::Bucket is considered stateful by @guardian/cdk. Care should be taken when updating this resource to avoid accidental replacement as this could lead to downtime.`
);
});
});
28 changes: 26 additions & 2 deletions src/constructs/core/stack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { App, StackProps } from "@aws-cdk/core";
import { Stack, Tags } from "@aws-cdk/core";
import type { App, IConstruct, StackProps } from "@aws-cdk/core";
import { CfnResource, Stack, Tags } from "@aws-cdk/core";
import { Stage } from "../../constants";
import { StatefulResourceTypes } from "../../constants/stateful-resource-types";
import { TrackingTag } from "../../constants/tracking-tag";
import { Logger } from "../../utils/logger";
import type { StackStageIdentity } from "./identity";
import type { GuStageDependentValue } from "./mappings";
import { GuStageMapping } from "./mappings";
Expand Down Expand Up @@ -120,4 +122,26 @@ export class GuStack extends Stack implements StackStageIdentity, GuMigratingSta
this.addTag("Stack", this.stack);
this.addTag("Stage", this.stage);
}

protected prepare(): void {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs are a little scary, but we're only logging so should be ok?!

Perform final modifications before synthesis.

This method can be implemented by derived constructs in order to perform final changes before synthesis. prepare() will be called after child constructs have been prepared.

This is an advanced framework feature. Only use this if you understand the implications.

super.prepare();

/*
Log a message whenever a stateful resource is encountered in the stack.

Ideally we'd add a stack policy to stop the resource being deleted,
however this isn't currently supported in CDK.

See:
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html
- https://github.com/aws/aws-cdk-rfcs/issues/72
*/
this.node.findAll().forEach((construct: IConstruct) => {
if (CfnResource.isCfnResource(construct) && StatefulResourceTypes.includes(construct.cfnResourceType)) {
Logger.warn(
Copy link
Member Author

@akash1810 akash1810 May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could call addDeletionOverride here, as we do for the GuAutoScalingGroup.

`The resource '${construct.node.path}' of type ${construct.cfnResourceType} is considered stateful by @guardian/cdk. Care should be taken when updating this resource to avoid accidental replacement as this could lead to downtime.`
);
}
});
}
}