Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit be5155f

Browse files
committed
feat(nextjs-component, aws-lambda): support adding tags to lambdas
1 parent e824146 commit be5155f

File tree

7 files changed

+116
-13
lines changed

7 files changed

+116
-13
lines changed

packages/e2e-tests/next-app/serverless.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ next-app:
1010
api/*:
1111
forward:
1212
headers: [Authorization]
13+
tags:
14+
defaultLambda:
15+
tag1: val1
16+
apiLambda:
17+
tag2: val2
18+
imageLambda:
19+
tag3: val3
20+
regenerationLambda:
21+
tag4: val4

packages/serverless-components/aws-lambda/serverless.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const defaults = {
3939
handler: "handler.hello",
4040
runtime: "nodejs10.x",
4141
env: {},
42-
region: "us-east-1"
42+
region: "us-east-1",
43+
tags: {}
4344
};
4445

4546
class AwsLambda extends Component {

packages/serverless-components/aws-lambda/utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ const createLambda = async ({
8888
zipPath,
8989
bucket,
9090
role,
91-
layer
91+
layer,
92+
tags
9293
}) => {
9394
const params = {
9495
FunctionName: name,
@@ -102,7 +103,8 @@ const createLambda = async ({
102103
Timeout: timeout,
103104
Environment: {
104105
Variables: env
105-
}
106+
},
107+
Tags: tags
106108
};
107109

108110
if (layer && layer.arn) {
@@ -131,7 +133,8 @@ const updateLambdaConfig = async ({
131133
env,
132134
description,
133135
role,
134-
layer
136+
layer,
137+
tags
135138
}) => {
136139
const functionConfigParams = {
137140
FunctionName: name,
@@ -143,7 +146,8 @@ const updateLambdaConfig = async ({
143146
Timeout: timeout,
144147
Environment: {
145148
Variables: env
146-
}
149+
},
150+
Tags: tags
147151
};
148152

149153
if (layer && layer.arn) {

packages/serverless-components/nextjs-component/__tests__/custom-inputs.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { mockSQS } from "@sls-next/aws-sqs";
1010

1111
import NextjsComponent, { DeploymentResult } from "../src/component";
1212
import obtainDomains from "../src/lib/obtainDomains";
13-
import { DEFAULT_LAMBDA_CODE_DIR, API_LAMBDA_CODE_DIR } from "../src/constants";
13+
import {
14+
DEFAULT_LAMBDA_CODE_DIR,
15+
API_LAMBDA_CODE_DIR,
16+
IMAGE_LAMBDA_CODE_DIR,
17+
REGENERATION_LAMBDA_CODE_DIR
18+
} from "../src/constants";
1419
import { cleanupFixtureDirectory } from "../src/lib/test-utils";
1520

1621
// unfortunately can't use __mocks__ because aws-sdk is being mocked in other
@@ -414,6 +419,61 @@ describe("Custom inputs", () => {
414419
});
415420
});
416421

422+
describe.each([
423+
{
424+
defaultLambda: { tag1: "val1" },
425+
apiLambda: { tag2: "val2" },
426+
imageLambda: { tag3: "val3" }
427+
}
428+
])("Lambda tags input", (tags) => {
429+
const fixturePath = path.join(__dirname, "./fixtures/generic-fixture");
430+
let tmpCwd: string;
431+
432+
beforeEach(async () => {
433+
tmpCwd = process.cwd();
434+
process.chdir(fixturePath);
435+
436+
mockServerlessComponentDependencies({ expectedDomain: undefined });
437+
438+
const component = createNextComponent();
439+
440+
componentOutputs = await component.default({
441+
tags: tags
442+
});
443+
});
444+
445+
afterEach(() => {
446+
process.chdir(tmpCwd);
447+
return cleanupFixtureDirectory(fixturePath);
448+
});
449+
450+
it(`sets lambda tags to ${JSON.stringify(tags)}`, () => {
451+
// default Lambda
452+
expect(mockLambda).toBeCalledWith(
453+
expect.objectContaining({
454+
code: path.join(fixturePath, DEFAULT_LAMBDA_CODE_DIR),
455+
tags: tags.defaultLambda
456+
})
457+
);
458+
459+
// api Lambda
460+
expect(mockLambda).toBeCalledWith(
461+
expect.objectContaining({
462+
code: path.join(fixturePath, API_LAMBDA_CODE_DIR),
463+
tags: tags.apiLambda
464+
})
465+
);
466+
467+
// image lambda
468+
expect(mockLambda).toBeCalledWith(
469+
expect.objectContaining({
470+
code: path.join(fixturePath, IMAGE_LAMBDA_CODE_DIR),
471+
tags: tags.imageLambda
472+
})
473+
);
474+
});
475+
});
476+
417477
describe.each`
418478
inputTimeout | expectedTimeout
419479
${undefined} | ${{ defaultTimeout: 10, apiTimeout: 10 }}

packages/serverless-components/nextjs-component/__tests__/deploy.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ describe.each`
124124
runtime: "nodejs12.x",
125125
name: "bucket-xyz",
126126
region: "us-east-1",
127+
tags: {},
127128
role: {
128129
service: ["lambda.amazonaws.com"],
129130
policy: {
@@ -175,6 +176,7 @@ describe.each`
175176
memory: 512,
176177
timeout: 10,
177178
runtime: "nodejs12.x",
179+
tags: {},
178180
role: {
179181
service: ["lambda.amazonaws.com", "edgelambda.amazonaws.com"],
180182
policy: {
@@ -221,6 +223,7 @@ describe.each`
221223
memory: 512,
222224
timeout: 10,
223225
runtime: "nodejs12.x",
226+
tags: {},
224227
role: {
225228
service: ["lambda.amazonaws.com", "edgelambda.amazonaws.com"],
226229
policy: {
@@ -267,6 +270,7 @@ describe.each`
267270
memory: 512,
268271
timeout: 10,
269272
runtime: "nodejs12.x",
273+
tags: {},
270274
role: {
271275
service: ["lambda.amazonaws.com", "edgelambda.amazonaws.com"],
272276
policy: {

packages/serverless-components/nextjs-component/src/component.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ class NextjsComponent extends Component {
451451
);
452452

453453
const readLambdaInputValue = (
454-
inputKey: "memory" | "timeout" | "name" | "runtime" | "roleArn",
454+
inputKey: "memory" | "timeout" | "name" | "runtime" | "roleArn" | "tags",
455455
lambdaType: LambdaType,
456-
defaultValue: string | number | undefined
457-
): string | number | undefined => {
456+
defaultValue: string | number | Record<string, string> | undefined
457+
): string | number | Record<string, string> | undefined => {
458458
const inputValue = inputs[inputKey];
459459

460460
if (typeof inputValue === "string" || typeof inputValue === "number") {
@@ -571,7 +571,11 @@ class NextjsComponent extends Component {
571571
"regenerationLambda",
572572
"nodejs12.x"
573573
) as string,
574-
name: bucketOutputs.name
574+
name: bucketOutputs.name,
575+
tags: readLambdaInputValue("tags", "regenerationLambda", {}) as Record<
576+
string,
577+
string
578+
>
575579
};
576580

577581
const regenerationLambdaResult = await regenerationLambda(
@@ -610,7 +614,11 @@ class NextjsComponent extends Component {
610614
) as string,
611615
name: readLambdaInputValue("name", "apiLambda", undefined) as
612616
| string
613-
| undefined
617+
| undefined,
618+
tags: readLambdaInputValue("tags", "apiLambda", {}) as Record<
619+
string,
620+
string
621+
>
614622
};
615623

616624
const apiEdgeLambdaOutputs = await apiEdgeLambda(apiEdgeLambdaInput);
@@ -667,7 +675,11 @@ class NextjsComponent extends Component {
667675
) as string,
668676
name: readLambdaInputValue("name", "imageLambda", undefined) as
669677
| string
670-
| undefined
678+
| undefined,
679+
tags: readLambdaInputValue("tags", "imageLambda", {}) as Record<
680+
string,
681+
string
682+
>
671683
};
672684

673685
const imageEdgeLambdaOutputs = await imageEdgeLambda(
@@ -728,7 +740,11 @@ class NextjsComponent extends Component {
728740
) as string,
729741
name: readLambdaInputValue("name", "defaultLambda", undefined) as
730742
| string
731-
| undefined
743+
| undefined,
744+
tags: readLambdaInputValue("tags", "defaultLambda", {}) as Record<
745+
string,
746+
string
747+
>
732748
};
733749

734750
const defaultEdgeLambdaOutputs = await defaultEdgeLambda(

packages/serverless-components/nextjs-component/types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export type ServerlessComponentInputs = {
4949
imageLambda?: string;
5050
regenerationLambda?: string;
5151
};
52+
tags?:
53+
| string
54+
| {
55+
defaultLambda?: Record<string, string>;
56+
apiLambda?: Record<string, string>;
57+
imageLambda?: Record<string, string>;
58+
regenerationLambda?: Record<string, string>;
59+
};
5260
handler?: string;
5361
description?: string;
5462
policy?: string;
@@ -95,4 +103,5 @@ export type LambdaInput = {
95103
timeout: number;
96104
runtime: string;
97105
name?: string;
106+
tags: Record<string, string>;
98107
};

0 commit comments

Comments
 (0)