Skip to content

Commit 0db1863

Browse files
tnoletDeviaVir
authored andcommitted
adds tagging on new and updated functions (#508)
* adds tagging on new and updated functions * fixes return value and associated test when no tags are given * adds tagging on new and updated functions * fixes return value and associated test when no tags are given
1 parent 8387fe6 commit 0db1863

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ DOCKER_IMAGE // (default: '')
9898
DEPLOY_ZIPFILE // (default: '')
9999
DEPLOY_USE_S3 // (default: false)
100100
AWS_DLQ_TARGET_ARN // (default: not set)
101+
AWS_TAGS // (default: '')
101102
```
102103

103104
#### run
@@ -196,6 +197,7 @@ Options:
196197
-z, --deployZipfile [] Deploy zipfile
197198
-B, --deployUseS3 [] Use S3 to deploy.
198199
-y, --proxy [] Proxy server
200+
-A, --tags [] 'Tags as key value pairs (e.g. "tagname1=tagvalue1,tagname2=tagvalue2") (default: "")
199201
```
200202

201203
If you are deploying to a custom endpoint you may also need to pass in an access key/secret. For localstack these can be anything, but cannot be blank:

bin/node-lambda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const PROXY = process.env.PROXY || process.env.http_proxy || ''
5959
const ENABLE_RUN_MULTIPLE_EVENTS = true
6060
const KEEP_NODE_MODULES = process.env.KEEP_NODE_MODULES || false
6161
const DOCKER_VOLUMES = process.env.DOCKER_VOLUMES || ''
62+
const AWS_TAGS = process.env.AWS_TAGS || ''
6263

6364
program
6465
.command('deploy')
@@ -105,6 +106,7 @@ program
105106
.option('-z, --deployZipfile [DEPLOY_ZIPFILE]', 'Deploy zipfile', DEPLOY_ZIPFILE)
106107
.option('-B, --deployUseS3 [DEPLOY_USE_S3]', 'Use S3 to deploy.', DEPLOY_USE_S3)
107108
.option('-y, --proxy [PROXY]', 'Proxy server', PROXY)
109+
.option('-A, --tags [AWS_TAGS]', 'Tags as key value pairs (e.g. "tagname1=tagvalue1,tagname2=tagvalue2)"', AWS_TAGS)
108110
.action((prg) => lambda.deploy(prg))
109111

110112
program

lib/main.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ so you can easily test run multiple events.
215215
TracingConfig: {
216216
Mode: null
217217
},
218-
Layers: []
218+
Layers: [],
219+
Tags: {}
219220
}
220221

221222
if (this._isUseS3(program)) {
@@ -255,6 +256,15 @@ so you can easily test run multiple events.
255256
if (program.layers) {
256257
params.Layers = program.layers.split(',')
257258
}
259+
if (program.tags) {
260+
const tags = program.tags.split(',')
261+
for (const tag of tags) {
262+
const kvPair = tag.split('=')
263+
if (kvPair && kvPair.length === 2) {
264+
params.Tags[kvPair[0].toString()] = kvPair[1].toString()
265+
}
266+
}
267+
}
258268

259269
return params
260270
}
@@ -752,6 +762,23 @@ they may not work as expected in the Lambda environment.
752762
}))
753763
}
754764

765+
_updateTags (lambda, functionArn, tags) {
766+
if (!tags || Object.keys(tags).length <= 0) {
767+
return Promise.resolve([])
768+
} else {
769+
return lambda.listTags({ Resource: functionArn }).promise()
770+
.then(data => {
771+
const keys = Object.keys(data.Tags)
772+
return keys && keys.length > 0
773+
? lambda.untagResource({ Resource: functionArn, TagKeys: keys }).promise()
774+
: Promise.resolve()
775+
})
776+
.then(() => {
777+
return lambda.tagResource({ Resource: functionArn, Tags: tags }).promise()
778+
})
779+
}
780+
}
781+
755782
_updateScheduleEvents (scheduleEvents, functionArn, scheduleList) {
756783
if (scheduleList == null) {
757784
return Promise.resolve([])
@@ -889,7 +916,11 @@ they may not work as expected in the Lambda environment.
889916
s3Events,
890917
results.FunctionArn,
891918
eventSourceList.S3Events
892-
)
919+
),
920+
this._updateTags(
921+
lambda,
922+
results.FunctionArn,
923+
params.Tags)
893924
])
894925
}),
895926
this._updateEventSources(

test/main.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ const lambdaMockSettings = {
8383
deleteEventSourceMapping: {
8484
EventSourceArn: 'Lambda.deleteEventSourceMapping.mock.EventSourceArn',
8585
FunctionName: 'Lambda.deleteEventSourceMapping.mock.EventSourceArn'
86-
}
86+
},
87+
listTags: {
88+
Tags: { tag1: 'key1' }
89+
},
90+
untagResource: {},
91+
tagResource: {}
8792
}
93+
8894
const _mockSetting = () => {
8995
awsMock.mock('CloudWatchEvents', 'putRule', (params, callback) => {
9096
callback(null, {})
@@ -1317,7 +1323,7 @@ describe('lib/main', function () {
13171323
assert.deepEqual(
13181324
result,
13191325
[
1320-
[[], []],
1326+
[[], [], []],
13211327
[],
13221328
{ retentionInDays: 30 }
13231329
]
@@ -1334,4 +1340,17 @@ describe('lib/main', function () {
13341340
})
13351341
})
13361342
})
1343+
1344+
describe('Lambda.prototype._updateTags()', () => {
1345+
it('simple test with mock', () => {
1346+
return lambda._updateTags(
1347+
awsLambda,
1348+
'arn:aws:lambda:eu-central-1:1234567:function:test',
1349+
{ tagKey: 'tagValue' }).then((result) => {
1350+
assert.deepEqual(
1351+
result, {}
1352+
)
1353+
})
1354+
})
1355+
})
13371356
})

0 commit comments

Comments
 (0)