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

Commit a46f0cf

Browse files
committed
fixes
1 parent be5155f commit a46f0cf

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

packages/serverless-components/aws-lambda/__mocks__/aws-sdk.mock.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const mockGetCallerIdentityMappingPromise = promisifyMock(
5050
mockGetCallerIdentityMapping
5151
);
5252

53+
const mockListTags = jest.fn();
54+
const mockListTagsPromise = promisifyMock(mockListTags);
55+
const mockTagResource = jest.fn();
56+
const mockTagResourcePromise = promisifyMock(mockTagResource);
57+
const mockUntagResource = jest.fn();
58+
const mockUntagResourcePromise = promisifyMock(mockUntagResource);
59+
5360
module.exports = {
5461
mockCreateQueuePromise,
5562
mockGetQueueAttributesPromise,
@@ -80,6 +87,12 @@ module.exports = {
8087
mockUpdateFunctionCodePromise,
8188
mockUpdateFunctionConfiguration,
8289
mockUpdateFunctionConfigurationPromise,
90+
mockListTags,
91+
mockListTagsPromise,
92+
mockTagResource,
93+
mockTagResourcePromise,
94+
mockUntagResource,
95+
mockUntagResourcePromise,
8396

8497
Lambda: jest.fn(() => ({
8598
listEventSourceMappings: mockListEventSourceMappings,
@@ -88,6 +101,9 @@ module.exports = {
88101
publishVersion: mockPublishVersion,
89102
getFunctionConfiguration: mockGetFunctionConfiguration,
90103
updateFunctionCode: mockUpdateFunctionCode,
91-
updateFunctionConfiguration: mockUpdateFunctionConfiguration
104+
updateFunctionConfiguration: mockUpdateFunctionConfiguration,
105+
listTags: mockListTags,
106+
tagResource: mockTagResource,
107+
untagResource: mockUntagResource
92108
}))
93109
};

packages/serverless-components/aws-lambda/__tests__/publishVersion.test.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
const { createComponent, createTmpDir } = require("../test-utils");
22

33
const {
4+
mockCreateFunction,
45
mockCreateFunctionPromise,
56
mockPublishVersion,
67
mockPublishVersionPromise,
78
mockGetFunctionConfigurationPromise,
89
mockUpdateFunctionCodePromise,
9-
mockUpdateFunctionConfigurationPromise
10+
mockUpdateFunctionConfigurationPromise,
11+
mockListTags,
12+
mockListTagsPromise,
13+
mockTagResource,
14+
mockUntagResource
1015
} = require("aws-sdk");
1116

1217
jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock"));
@@ -47,7 +52,8 @@ describe("publishVersion", () => {
4752
const tmpFolder = await createTmpDir();
4853

4954
await component.default({
50-
code: tmpFolder
55+
code: tmpFolder,
56+
tags: { new: "tag" }
5157
});
5258

5359
const versionResult = await component.publishVersion();
@@ -60,6 +66,12 @@ describe("publishVersion", () => {
6066
expect(versionResult).toEqual({
6167
version: "v2"
6268
});
69+
70+
expect(mockCreateFunction).toBeCalledWith(
71+
expect.objectContaining({
72+
Tags: { new: "tag" }
73+
})
74+
);
6375
});
6476

6577
it("publishes new version of lambda that was updated", async () => {
@@ -79,7 +91,11 @@ describe("publishVersion", () => {
7991
CodeSha256: "LQT0VA="
8092
});
8193
mockUpdateFunctionConfigurationPromise.mockResolvedValueOnce({
82-
CodeSha256: "XYZ0VA="
94+
CodeSha256: "XYZ0VA=",
95+
FunctionArn: "arn:aws:lambda:us-east-1:123456789012:function:my-func"
96+
});
97+
mockListTagsPromise.mockResolvedValueOnce({
98+
Tags: { foo: "bar" }
8399
});
84100

85101
const tmpFolder = await createTmpDir();
@@ -89,7 +105,8 @@ describe("publishVersion", () => {
89105
});
90106

91107
await component.default({
92-
code: tmpFolder
108+
code: tmpFolder,
109+
tags: { new: "tag" }
93110
});
94111

95112
const versionResult = await component.publishVersion();
@@ -99,6 +116,20 @@ describe("publishVersion", () => {
99116
CodeSha256: "XYZ0VA=" // compare against the hash received from the function update, *not* create
100117
});
101118

119+
expect(mockListTags).toBeCalledWith({
120+
Resource: "arn:aws:lambda:us-east-1:123456789012:function:my-func"
121+
});
122+
123+
expect(mockUntagResource).toBeCalledWith({
124+
Resource: "arn:aws:lambda:us-east-1:123456789012:function:my-func",
125+
Tags: ["foo"]
126+
});
127+
128+
expect(mockTagResource).toBeCalledWith({
129+
Resource: "arn:aws:lambda:us-east-1:123456789012:function:my-func",
130+
Tags: { new: "tag" }
131+
});
132+
102133
expect(versionResult).toEqual({
103134
version: "v2"
104135
});

packages/serverless-components/aws-lambda/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"archiver": "^5.3.0",
2525
"fs-extra": "^9.1.0",
2626
"globby": "^11.0.1",
27+
"lodash": "^4.17.21",
2728
"ramda": "^0.27.0"
2829
},
2930
"peerDependencies": {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const outputsList = [
2626
"env",
2727
"role",
2828
"arn",
29-
"region"
29+
"region",
30+
"tags"
3031
];
3132

3233
const defaults = {
@@ -50,6 +51,7 @@ class AwsLambda extends Component {
5051
const config = mergeDeepRight(defaults, inputs);
5152

5253
config.name = inputs.name || this.state.name || this.context.resourceId();
54+
config.tags = inputs.tags || this.state.tags;
5355

5456
this.context.debug(
5557
`Starting deployment of lambda ${config.name} to the ${config.region} region.`

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const globby = require("globby");
55
const { contains, isNil, last, split, equals, not, pick } = require("ramda");
66
const { readFile, createReadStream, createWriteStream } = require("fs-extra");
77
const { utils } = require("@serverless/core");
8+
const _ = require("lodash");
89

910
const VALID_FORMATS = ["zip", "tar"];
1011
const isValidFormat = (format) => contains(format, VALID_FORMATS);
@@ -146,8 +147,7 @@ const updateLambdaConfig = async ({
146147
Timeout: timeout,
147148
Environment: {
148149
Variables: env
149-
},
150-
Tags: tags
150+
}
151151
};
152152

153153
if (layer && layer.arn) {
@@ -158,6 +158,30 @@ const updateLambdaConfig = async ({
158158
.updateFunctionConfiguration(functionConfigParams)
159159
.promise();
160160

161+
// Get and update Lambda tags
162+
const listTagsResponse = await lambda
163+
.listTags({ Resource: res.FunctionArn })
164+
.promise();
165+
const currentTags = listTagsResponse.Tags;
166+
167+
// If tags are not the same then update them
168+
if (!_.isEqual(currentTags, tags)) {
169+
await lambda
170+
.untagResource({
171+
Resource: res.FunctionArn,
172+
Tags: Object.keys(currentTags)
173+
})
174+
.promise();
175+
176+
if (tags && Object.keys(tags).length > 0)
177+
await lambda
178+
.tagResource({
179+
Resource: res.FunctionArn,
180+
Tags: tags
181+
})
182+
.promise();
183+
}
184+
161185
return { arn: res.FunctionArn, hash: res.CodeSha256 };
162186
};
163187

packages/serverless-components/aws-lambda/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ lodash.union@^4.6.0:
10971097
resolved "https://registry.npmjs.org/lodash.union/-/lodash.union-4.6.0.tgz#48bb5088409f16f1821666641c44dd1aaae3cd88"
10981098
integrity sha1-SLtQiECfFvGCFmZkHETdGqrjzYg=
10991099

1100-
lodash@^4.17.14:
1100+
lodash@^4.17.14, lodash@^4.17.21:
11011101
version "4.17.21"
11021102
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
11031103
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)