Skip to content

Commit

Permalink
feat: added tags to the stack resources (aws-amplify#985)
Browse files Browse the repository at this point in the history
* feat: added tags to the stack resources

* add changeset
  • Loading branch information
bzsurbhi authored Feb 6, 2024
1 parent a8bd451 commit bded277
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-socks-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend': minor
---

Added tags to CFN resources
53 changes: 52 additions & 1 deletion packages/backend/src/default_stack_factory.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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',
},
],
});
});
});
12 changes: 11 additions & 1 deletion packages/backend/src/project_environment_main_stack_creator.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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!;
};
Expand Down

0 comments on commit bded277

Please sign in to comment.