Skip to content

Commit 2fff9c0

Browse files
feat: add support for tags
1 parent 5f3ce46 commit 2fff9c0

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ stepFunctions:
4848
Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-hello
4949
End: true
5050
dependsOn: CustomIamRole
51+
tags:
52+
- Key: Team
53+
Value: Atlantis
5154
alarms:
5255
topics:
5356
ok: arn:aws:sns:us-east-1:1234567890:NotifyMe
@@ -72,6 +75,9 @@ stepFunctions:
7275
- DynamoDBTable
7376
- KinesisStream
7477
- CUstomIamRole
78+
tags:
79+
- Key: Team
80+
Value: Atlantis
7581
activities:
7682
- myTask
7783
- yourTask

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
let DefinitionString;
1717
let RoleArn;
1818
let DependsOn = [];
19+
let Tags = [];
1920

2021
if (stateMachineObj.definition) {
2122
if (typeof stateMachineObj.definition === 'string') {
@@ -83,6 +84,19 @@ module.exports = {
8384
}
8485
}
8586

87+
if (_.isArray(stateMachineObj.tags)) {
88+
const malformedTags =
89+
stateMachineObj.tags.filter(x => !_.isString(x.Key) || !_.isString(x.Value));
90+
if (!_.isEmpty(malformedTags)) {
91+
const errorMsg = `${malformedTags.length} tags are malformed. ` +
92+
'Expect tags to have shape {Key:string, Value: String}';
93+
throw new this.serverless.classes
94+
.Error(errorMsg);
95+
}
96+
97+
Tags = stateMachineObj.tags;
98+
}
99+
86100
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,
87101
stateMachineObj);
88102
const stateMachineOutputLogicalId = this
@@ -92,6 +106,7 @@ module.exports = {
92106
Properties: {
93107
DefinitionString,
94108
RoleArn,
109+
Tags,
95110
},
96111
DependsOn,
97112
};

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,61 @@ describe('#compileStateMachines', () => {
483483
};
484484
expect(() => serverlessStepFunctions.compileStateMachines()).to.throw(Error);
485485
});
486+
487+
it('should add tags', () => {
488+
serverless.service.stepFunctions = {
489+
stateMachines: {
490+
myStateMachine1: {
491+
definition: 'definition1',
492+
name: 'stateMachineBeta1',
493+
tags: [{
494+
Key: 'team',
495+
Value: 'core',
496+
}, {
497+
Key: 'feature',
498+
Value: 'tags',
499+
}],
500+
},
501+
myStateMachine2: {
502+
definition: 'definition2',
503+
name: 'stateMachineBeta2',
504+
tags: [{
505+
Key: 'team',
506+
Value: 'core',
507+
}, {
508+
Key: 'feature',
509+
Value: 'tags',
510+
}],
511+
},
512+
},
513+
};
514+
515+
serverlessStepFunctions.compileStateMachines();
516+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
517+
.provider.compiledCloudFormationTemplate.Resources
518+
.StateMachineBeta1;
519+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
520+
.provider.compiledCloudFormationTemplate.Resources
521+
.StateMachineBeta2;
522+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
523+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
524+
expect(stateMachineBeta1.Properties.Tags)
525+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'feature', Value: 'tags' }]);
526+
expect(stateMachineBeta2.Properties.Tags)
527+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'feature', Value: 'tags' }]);
528+
});
529+
530+
it('should throw error when tags property contains malformed tags', () => {
531+
serverless.service.stepFunctions = {
532+
stateMachines: {
533+
myStateMachine1: {
534+
definition: 'definition1',
535+
name: 'stateMachineBeta1',
536+
tags: ['team:core'],
537+
},
538+
},
539+
};
540+
541+
expect(() => serverlessStepFunctions.compileStateMachines()).to.throw(Error);
542+
});
486543
});

0 commit comments

Comments
 (0)