From bded2772c71574f6e494fb53acf632b73e0a7215 Mon Sep 17 00:00:00 2001 From: bzsurbhi <115104450+bzsurbhi@users.noreply.github.com> Date: Tue, 6 Feb 2024 10:15:13 -0800 Subject: [PATCH] feat: added tags to the stack resources (#985) * feat: added tags to the stack resources * add changeset --- .changeset/tricky-socks-wash.md | 5 ++ .../backend/src/default_stack_factory.test.ts | 53 ++++++++++++++++++- .../project_environment_main_stack_creator.ts | 12 ++++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 .changeset/tricky-socks-wash.md diff --git a/.changeset/tricky-socks-wash.md b/.changeset/tricky-socks-wash.md new file mode 100644 index 0000000000..e4ea4aa03d --- /dev/null +++ b/.changeset/tricky-socks-wash.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend': minor +--- + +Added tags to CFN resources diff --git a/packages/backend/src/default_stack_factory.test.ts b/packages/backend/src/default_stack_factory.test.ts index f26cab85ef..0824c0b034 100644 --- a/packages/backend/src/default_stack_factory.test.ts +++ b/packages/backend/src/default_stack_factory.test.ts @@ -1,8 +1,9 @@ import { describe, it } from 'node:test'; import { createDefaultStack } from './default_stack_factory.js'; -import { App } from 'aws-cdk-lib'; +import { App, aws_s3 } from 'aws-cdk-lib'; import assert from 'node:assert'; import { AmplifyStack } from './engine/amplify_stack.js'; +import { Template } from 'aws-cdk-lib/assertions'; void describe('createDefaultRootStack', () => { void it('creates AmplifyStack with backend ID and branch from CDK context', () => { @@ -58,4 +59,54 @@ void describe('createDefaultRootStack', () => { message: `No context value present for amplify-backend-type key`, }); }); + + void it('adds tags to the stack resources in case of branch deployment', () => { + const app = new App(); + app.node.setContext('amplify-backend-namespace', 'testBackendId'); + app.node.setContext('amplify-backend-name', 'testBranchName'); + app.node.setContext('amplify-backend-type', 'branch'); + const stack = createDefaultStack(app); + new aws_s3.Bucket(stack, 'test'); + Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', { + Tags: [ + { + Key: 'amplify:app-id', + Value: 'testBackendId', + }, + { + Key: 'amplify:branch-name', + Value: 'testBranchName', + }, + { + Key: 'amplify:deployment-type', + Value: 'branch', + }, + { + Key: 'created-by', + Value: 'amplify', + }, + ], + }); + }); + + void it('adds tags to the stack resources in case of sandbox deployment', () => { + const app = new App(); + app.node.setContext('amplify-backend-namespace', 'testProjectName'); + app.node.setContext('amplify-backend-name', 'testUser'); + app.node.setContext('amplify-backend-type', 'sandbox'); + const stack = createDefaultStack(app); + new aws_s3.Bucket(stack, 'test'); + Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', { + Tags: [ + { + Key: 'amplify:deployment-type', + Value: 'sandbox', + }, + { + Key: 'created-by', + Value: 'amplify', + }, + ], + }); + }); }); diff --git a/packages/backend/src/project_environment_main_stack_creator.ts b/packages/backend/src/project_environment_main_stack_creator.ts index 5674364c4b..fbadb59b57 100644 --- a/packages/backend/src/project_environment_main_stack_creator.ts +++ b/packages/backend/src/project_environment_main_stack_creator.ts @@ -1,6 +1,6 @@ import { BackendIdentifier, MainStackCreator } from '@aws-amplify/plugin-types'; import { Construct } from 'constructs'; -import { Stack } from 'aws-cdk-lib'; +import { Stack, Tags } from 'aws-cdk-lib'; import { AmplifyStack } from './engine/amplify_stack.js'; import { BackendIdentifierConversions } from '@aws-amplify/platform-core'; @@ -27,6 +27,16 @@ export class ProjectEnvironmentMainStackCreator implements MainStackCreator { BackendIdentifierConversions.toStackName(this.backendId) ); } + + const deploymentType = this.backendId.type; + Tags.of(this.mainStack).add('created-by', 'amplify'); + if (deploymentType === 'branch') { + Tags.of(this.mainStack).add('amplify:app-id', this.backendId.namespace); + Tags.of(this.mainStack).add('amplify:branch-name', this.backendId.name); + Tags.of(this.mainStack).add('amplify:deployment-type', 'branch'); + } else if (deploymentType === 'sandbox') { + Tags.of(this.mainStack).add('amplify:deployment-type', 'sandbox'); + } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.mainStack!; };