-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2098 from guardian/aa/backup
feat(backup): Support backups provided by DevX
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
"@guardian/cdk": minor | ||
--- | ||
|
||
feat(backup): Support backups provided by DevX | ||
|
||
Adds a new property `withBackup` to `GuStack` to enable backups provided by DevX. | ||
|
||
When `true`, all supported resources in the stack will receive a new tag `devx-backup-enabled`. | ||
|
||
To opt in/out an individual resource, you can manually apply this tag. | ||
|
||
See https://github.com/guardian/aws-backup. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { App, Tags } from "aws-cdk-lib"; | ||
import { Vpc } from "aws-cdk-lib/aws-ec2"; | ||
import { DatabaseInstance, DatabaseInstanceEngine } from "aws-cdk-lib/aws-rds"; | ||
import { GuStack } from "../constructs/core"; | ||
import { GuTemplate } from "../utils/test"; | ||
|
||
describe("AwsBackupTag aspect", () => { | ||
it("should add the 'devx-backup-enabled' tag as 'true' when backups are enabled", () => { | ||
const app = new App(); | ||
const stack = new GuStack(app, "Test", { stack: "test", stage: "TEST", withBackup: true }); | ||
|
||
const vpc = new Vpc(stack, "TestVpc"); | ||
new DatabaseInstance(stack, "MyDatabase", { engine: DatabaseInstanceEngine.POSTGRES, vpc }); | ||
|
||
GuTemplate.fromStack(stack).hasGuTaggedResource("AWS::RDS::DBInstance", { | ||
additionalTags: [ | ||
{ | ||
Key: "devx-backup-enabled", | ||
Value: "true", | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it("should allow the 'devx-backup-enabled' tag to be overridden", () => { | ||
const app = new App(); | ||
const stack = new GuStack(app, "Test", { | ||
stack: "test", | ||
stage: "TEST", | ||
withBackup: true, // enable backups on all resources in this stack | ||
}); | ||
|
||
const vpc = new Vpc(stack, "TestVpc"); | ||
const database = new DatabaseInstance(stack, "MyDatabase", { engine: DatabaseInstanceEngine.POSTGRES, vpc }); | ||
|
||
// Disable backups on this one resource | ||
Tags.of(database).add("devx-backup-enabled", "false"); | ||
|
||
GuTemplate.fromStack(stack).hasGuTaggedResource("AWS::RDS::DBInstance", { | ||
additionalTags: [ | ||
{ | ||
Key: "devx-backup-enabled", | ||
Value: "false", | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { IAspect } from "aws-cdk-lib"; | ||
import { CfnResource, TagManager } from "aws-cdk-lib"; | ||
import type { IConstruct } from "constructs"; | ||
|
||
/** | ||
* Applies the tags that enable backups for supported resources. | ||
* | ||
* @see https://github.com/guardian/aws-backup | ||
*/ | ||
export class AwsBackupTag implements IAspect { | ||
/** | ||
* These resources are backed up by https://github.com/guardian/aws-backup. | ||
*/ | ||
static resourceTypes: string[] = ["AWS::RDS::DBInstance", "AWS::DynamoDB::Table"]; | ||
|
||
public visit(node: IConstruct): void { | ||
if (node instanceof CfnResource) { | ||
const { cfnResourceType } = node; | ||
|
||
if (AwsBackupTag.resourceTypes.includes(cfnResourceType) && TagManager.isTaggable(node)) { | ||
node.tags.setTag("devx-backup-enabled", "true"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters