From e3125598d62e38e99d7b3d27f7f0b696f37cda0d Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:19:10 +1000 Subject: [PATCH 1/5] Add tags to AppStack resources --- .../infra/__snapshots__/appStack.test.ts.snap | 364 ++++++++++++++++++ .../infra/appStack.test.ts | 4 + .../lambda-sqs-worker-cdk/infra/appStack.ts | 8 + template/lambda-sqs-worker-cdk/infra/tags.ts | 15 + 4 files changed, 391 insertions(+) create mode 100644 template/lambda-sqs-worker-cdk/infra/tags.ts diff --git a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap index 29c4063f5..a5d8ddf82 100644 --- a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap +++ b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap @@ -294,6 +294,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], "Version": "2012-10-17", }, + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::KMS::Key", "UpdateReplacePolicy": "Retain", @@ -311,6 +329,26 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` "Type": "AWS::KMS::Alias", }, "sourcetopic7C3DC892": { + "Properties": { + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], + }, "Type": "AWS::SNS::Topic", }, "worker28EA3E30": { @@ -354,6 +392,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -461,6 +517,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -548,6 +622,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -580,6 +672,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -702,6 +812,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 120, }, "Type": "AWS::Lambda::Function", @@ -734,6 +862,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -829,6 +975,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` }, "maxReceiveCount": 3, }, + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -896,6 +1060,24 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "QueueName": "serviceName-dead-letters", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "dev", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -1225,6 +1407,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], "Version": "2012-10-17", }, + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::KMS::Key", "UpdateReplacePolicy": "Retain", @@ -1242,6 +1442,26 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` "Type": "AWS::KMS::Alias", }, "sourcetopic7C3DC892": { + "Properties": { + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], + }, "Type": "AWS::SNS::Topic", }, "worker28EA3E30": { @@ -1285,6 +1505,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -1392,6 +1630,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -1479,6 +1735,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -1511,6 +1785,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -1633,6 +1925,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], "Timeout": 120, }, "Type": "AWS::Lambda::Function", @@ -1665,6 +1975,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::IAM::Role", }, @@ -1760,6 +2088,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` }, "maxReceiveCount": 3, }, + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -1827,6 +2173,24 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "QueueName": "serviceName-dead-letters", + "Tags": [ + { + "Key": "seek:env:label", + "Value": "prod", + }, + { + "Key": "seek:source:sha", + "Value": "*", + }, + { + "Key": "seek:source:url", + "Value": "ToDo: add source URL", + }, + { + "Key": "seek:system:name", + "Value": "ToDo: add system name", + }, + ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", diff --git a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts index 42ddb7acb..f9f77151c 100644 --- a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts +++ b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts @@ -50,6 +50,10 @@ it.each(['dev', 'prod'])( .replaceAll( /workerCurrentVersion([0-9a-zA-Z]+)"/g, (_, hash) => `workerCurrentVersion${'x'.repeat(hash.length)}"`, + ) + .replace( + /"Key":\s*"seek:source:sha",\s*"Value":\s*".*?"/g, + '"Key": "seek:source:sha", "Value": "*"', ); expect(JSON.parse(json)).toMatchSnapshot(); diff --git a/template/lambda-sqs-worker-cdk/infra/appStack.ts b/template/lambda-sqs-worker-cdk/infra/appStack.ts index eeec477f1..c8ce6eb74 100644 --- a/template/lambda-sqs-worker-cdk/infra/appStack.ts +++ b/template/lambda-sqs-worker-cdk/infra/appStack.ts @@ -16,6 +16,7 @@ import { import type { Construct } from 'constructs'; import { config } from './config'; +import { applyTags } from './tags'; export class AppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { @@ -29,6 +30,7 @@ export class AppStack extends Stack { admins: [accountPrincipal], alias: 'seek/self/<%- serviceName %>', }); + applyTags(kmsKey); kmsKey.grantEncrypt(accountPrincipal); @@ -40,6 +42,7 @@ export class AppStack extends Stack { encryptionMasterKey: kmsKey, }, ); + applyTags(deadLetterQueue); const queue = new aws_sqs.Queue(this, 'worker-queue', { queueName: '<%- serviceName %>', @@ -49,12 +52,14 @@ export class AppStack extends Stack { }, encryptionMasterKey: kmsKey, }); + applyTags(queue); const topic = aws_sns.Topic.fromTopicArn( this, 'source-topic', config.sourceSnsTopicArn, ); + applyTags(topic); topic.addSubscription(new aws_sns_subscriptions.SqsSubscription(queue)); @@ -98,6 +103,7 @@ export class AppStack extends Stack { description: `Updated at ${new Date().toISOString()}`, reservedConcurrentExecutions: config.workerLambda.reservedConcurrency, }); + applyTags(worker); const alias = worker.addAlias('live', { description: 'The Lambda version currently receiving traffic', @@ -125,6 +131,7 @@ export class AppStack extends Stack { }, }, ); + applyTags(preHook); worker.grantInvoke(preHook); @@ -144,6 +151,7 @@ export class AppStack extends Stack { }, }, ); + applyTags(postHook); const prunePermissions = new aws_iam.PolicyStatement({ actions: [ diff --git a/template/lambda-sqs-worker-cdk/infra/tags.ts b/template/lambda-sqs-worker-cdk/infra/tags.ts new file mode 100644 index 000000000..0097da4ae --- /dev/null +++ b/template/lambda-sqs-worker-cdk/infra/tags.ts @@ -0,0 +1,15 @@ +import { Tags } from 'aws-cdk-lib'; +import type { Construct } from 'constructs'; + +const tags: Record = { + 'seek:env:label': process.env.ENVIRONMENT ?? '', + 'seek:source:sha': process.env.BUILDKITE_COMMIT ?? '', + 'seek:source:url': 'ToDo: add source URL', + 'seek:system:name': 'ToDo: add system name', +}; + +export const applyTags = (construct: Construct) => { + Object.entries(tags).forEach(([key, value]) => { + Tags.of(construct).add(key, value); + }); +}; From 3a191012635470c57b4195fbab75bfff4024dbc8 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:38:39 +1000 Subject: [PATCH 2/5] Apply tags at stack level --- .../infra/__snapshots__/appStack.test.ts.snap | 364 ------------------ .../infra/appStack.test.ts | 4 - .../lambda-sqs-worker-cdk/infra/appStack.ts | 8 - template/lambda-sqs-worker-cdk/infra/index.ts | 6 + template/lambda-sqs-worker-cdk/infra/tags.ts | 15 - 5 files changed, 6 insertions(+), 391 deletions(-) delete mode 100644 template/lambda-sqs-worker-cdk/infra/tags.ts diff --git a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap index a5d8ddf82..29c4063f5 100644 --- a/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap +++ b/template/lambda-sqs-worker-cdk/infra/__snapshots__/appStack.test.ts.snap @@ -294,24 +294,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], "Version": "2012-10-17", }, - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::KMS::Key", "UpdateReplacePolicy": "Retain", @@ -329,26 +311,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` "Type": "AWS::KMS::Alias", }, "sourcetopic7C3DC892": { - "Properties": { - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], - }, "Type": "AWS::SNS::Topic", }, "worker28EA3E30": { @@ -392,24 +354,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -517,24 +461,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -622,24 +548,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -672,24 +580,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -812,24 +702,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 120, }, "Type": "AWS::Lambda::Function", @@ -862,24 +734,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -975,24 +829,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` }, "maxReceiveCount": 3, }, - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -1060,24 +896,6 @@ exports[`returns expected CloudFormation stack for dev 1`] = ` ], }, "QueueName": "serviceName-dead-letters", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "dev", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -1407,24 +1225,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], "Version": "2012-10-17", }, - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::KMS::Key", "UpdateReplacePolicy": "Retain", @@ -1442,26 +1242,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` "Type": "AWS::KMS::Alias", }, "sourcetopic7C3DC892": { - "Properties": { - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], - }, "Type": "AWS::SNS::Topic", }, "worker28EA3E30": { @@ -1505,24 +1285,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -1630,24 +1392,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -1735,24 +1479,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 30, }, "Type": "AWS::Lambda::Function", @@ -1785,24 +1511,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -1925,24 +1633,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "Runtime": "nodejs20.x", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], "Timeout": 120, }, "Type": "AWS::Lambda::Function", @@ -1975,24 +1665,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, ], - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::IAM::Role", }, @@ -2088,24 +1760,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` }, "maxReceiveCount": 3, }, - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", @@ -2173,24 +1827,6 @@ exports[`returns expected CloudFormation stack for prod 1`] = ` ], }, "QueueName": "serviceName-dead-letters", - "Tags": [ - { - "Key": "seek:env:label", - "Value": "prod", - }, - { - "Key": "seek:source:sha", - "Value": "*", - }, - { - "Key": "seek:source:url", - "Value": "ToDo: add source URL", - }, - { - "Key": "seek:system:name", - "Value": "ToDo: add system name", - }, - ], }, "Type": "AWS::SQS::Queue", "UpdateReplacePolicy": "Delete", diff --git a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts index f9f77151c..42ddb7acb 100644 --- a/template/lambda-sqs-worker-cdk/infra/appStack.test.ts +++ b/template/lambda-sqs-worker-cdk/infra/appStack.test.ts @@ -50,10 +50,6 @@ it.each(['dev', 'prod'])( .replaceAll( /workerCurrentVersion([0-9a-zA-Z]+)"/g, (_, hash) => `workerCurrentVersion${'x'.repeat(hash.length)}"`, - ) - .replace( - /"Key":\s*"seek:source:sha",\s*"Value":\s*".*?"/g, - '"Key": "seek:source:sha", "Value": "*"', ); expect(JSON.parse(json)).toMatchSnapshot(); diff --git a/template/lambda-sqs-worker-cdk/infra/appStack.ts b/template/lambda-sqs-worker-cdk/infra/appStack.ts index c8ce6eb74..eeec477f1 100644 --- a/template/lambda-sqs-worker-cdk/infra/appStack.ts +++ b/template/lambda-sqs-worker-cdk/infra/appStack.ts @@ -16,7 +16,6 @@ import { import type { Construct } from 'constructs'; import { config } from './config'; -import { applyTags } from './tags'; export class AppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { @@ -30,7 +29,6 @@ export class AppStack extends Stack { admins: [accountPrincipal], alias: 'seek/self/<%- serviceName %>', }); - applyTags(kmsKey); kmsKey.grantEncrypt(accountPrincipal); @@ -42,7 +40,6 @@ export class AppStack extends Stack { encryptionMasterKey: kmsKey, }, ); - applyTags(deadLetterQueue); const queue = new aws_sqs.Queue(this, 'worker-queue', { queueName: '<%- serviceName %>', @@ -52,14 +49,12 @@ export class AppStack extends Stack { }, encryptionMasterKey: kmsKey, }); - applyTags(queue); const topic = aws_sns.Topic.fromTopicArn( this, 'source-topic', config.sourceSnsTopicArn, ); - applyTags(topic); topic.addSubscription(new aws_sns_subscriptions.SqsSubscription(queue)); @@ -103,7 +98,6 @@ export class AppStack extends Stack { description: `Updated at ${new Date().toISOString()}`, reservedConcurrentExecutions: config.workerLambda.reservedConcurrency, }); - applyTags(worker); const alias = worker.addAlias('live', { description: 'The Lambda version currently receiving traffic', @@ -131,7 +125,6 @@ export class AppStack extends Stack { }, }, ); - applyTags(preHook); worker.grantInvoke(preHook); @@ -151,7 +144,6 @@ export class AppStack extends Stack { }, }, ); - applyTags(postHook); const prunePermissions = new aws_iam.PolicyStatement({ actions: [ diff --git a/template/lambda-sqs-worker-cdk/infra/index.ts b/template/lambda-sqs-worker-cdk/infra/index.ts index bc43ca2f9..3c489cdbd 100644 --- a/template/lambda-sqs-worker-cdk/infra/index.ts +++ b/template/lambda-sqs-worker-cdk/infra/index.ts @@ -8,4 +8,10 @@ const app = new App(); // eslint-disable-next-line no-new new AppStack(app, 'appStack', { stackName: config.appName, + tags: { + 'seek:env:label': config.workerLambda.environment.ENVIRONMENT, + 'seek:source:sha': process.env.BUILDKITE_COMMIT ?? 'na', + 'seek:source:url': 'ToDo: add source URL', + 'seek:system:name': 'ToDo: add system name', + }, }); diff --git a/template/lambda-sqs-worker-cdk/infra/tags.ts b/template/lambda-sqs-worker-cdk/infra/tags.ts deleted file mode 100644 index 0097da4ae..000000000 --- a/template/lambda-sqs-worker-cdk/infra/tags.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Tags } from 'aws-cdk-lib'; -import type { Construct } from 'constructs'; - -const tags: Record = { - 'seek:env:label': process.env.ENVIRONMENT ?? '', - 'seek:source:sha': process.env.BUILDKITE_COMMIT ?? '', - 'seek:source:url': 'ToDo: add source URL', - 'seek:system:name': 'ToDo: add system name', -}; - -export const applyTags = (construct: Construct) => { - Object.entries(tags).forEach(([key, value]) => { - Tags.of(construct).add(key, value); - }); -}; From 5bc32603a2ba9b1749f71f577e4be9e28ce88e10 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:49:18 +1000 Subject: [PATCH 3/5] Suggestions --- template/lambda-sqs-worker-cdk/infra/config.ts | 2 +- template/lambda-sqs-worker-cdk/infra/index.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/template/lambda-sqs-worker-cdk/infra/config.ts b/template/lambda-sqs-worker-cdk/infra/config.ts index aeb7aac0e..73f7dabe7 100644 --- a/template/lambda-sqs-worker-cdk/infra/config.ts +++ b/template/lambda-sqs-worker-cdk/infra/config.ts @@ -4,7 +4,7 @@ const ENVIRONMENTS = ['dev', 'prod'] as const; type Environment = (typeof ENVIRONMENTS)[number]; -const environment = Env.oneOf(ENVIRONMENTS)('ENVIRONMENT'); +export const environment = Env.oneOf(ENVIRONMENTS)('ENVIRONMENT'); interface Config { appName: string; diff --git a/template/lambda-sqs-worker-cdk/infra/index.ts b/template/lambda-sqs-worker-cdk/infra/index.ts index 3c489cdbd..5c7fa123f 100644 --- a/template/lambda-sqs-worker-cdk/infra/index.ts +++ b/template/lambda-sqs-worker-cdk/infra/index.ts @@ -1,7 +1,7 @@ import { App } from 'aws-cdk-lib'; import { AppStack } from './appStack'; -import { config } from './config'; +import { config, environment } from './config'; const app = new App(); @@ -9,9 +9,9 @@ const app = new App(); new AppStack(app, 'appStack', { stackName: config.appName, tags: { - 'seek:env:label': config.workerLambda.environment.ENVIRONMENT, + 'seek:env:label': environment, 'seek:source:sha': process.env.BUILDKITE_COMMIT ?? 'na', - 'seek:source:url': 'ToDo: add source URL', - 'seek:system:name': 'ToDo: add system name', + // 'seek:source:url': 'TODO: add source URL', + // 'seek:system:name': 'TODO: add system name', }, }); From 71fe40570da43bb370c601362af8bd750f95e091 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:49:51 +1000 Subject: [PATCH 4/5] Changeset --- .changeset/hungry-keys-cover.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-keys-cover.md diff --git a/.changeset/hungry-keys-cover.md b/.changeset/hungry-keys-cover.md new file mode 100644 index 000000000..919e4628c --- /dev/null +++ b/.changeset/hungry-keys-cover.md @@ -0,0 +1,5 @@ +--- +'skuba': patch +--- + +Add required tags to `lambda-sqs-worker-cdk` template From a0da720b729582d803cc3ca22eb5a27983f8c025 Mon Sep 17 00:00:00 2001 From: Zac Brydon <52645024+zbrydon@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:50:57 +1000 Subject: [PATCH 5/5] Add scope to changelog --- .changeset/hungry-keys-cover.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/hungry-keys-cover.md b/.changeset/hungry-keys-cover.md index 919e4628c..4ff01c6b1 100644 --- a/.changeset/hungry-keys-cover.md +++ b/.changeset/hungry-keys-cover.md @@ -2,4 +2,4 @@ 'skuba': patch --- -Add required tags to `lambda-sqs-worker-cdk` template +template: Add required tags to `lambda-sqs-worker-cdk` template