Skip to content

Commit 110c938

Browse files
Merge pull request #4 from horike37/master
synch
2 parents 5f3ce46 + 1a7da96 commit 110c938

File tree

6 files changed

+10696
-2567
lines changed

6 files changed

+10696
-2567
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ 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+
Team: Atlantis
5153
alarms:
5254
topics:
5355
ok: arn:aws:sns:us-east-1:1234567890:NotifyMe
@@ -72,6 +74,8 @@ stepFunctions:
7274
- DynamoDBTable
7375
- KinesisStream
7476
- CUstomIamRole
77+
tags:
78+
Team: Atlantis
7579
activities:
7680
- myTask
7781
- yourTask
@@ -707,8 +711,12 @@ stepFunctions:
707711
...
708712
```
709713

714+
## Tags
715+
716+
You can specify tags on each state machine. Additionally any global tags (specified under `provider` section in your `serverless.yml`) would be merged in as well.
710717

711718
## Command
719+
712720
### deploy
713721
Run `sls deploy`, the defined Stepfunctions are deployed.
714722

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] };

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ function isIntrinsic(obj) {
77
return isObject && Object.keys(obj).some((k) => k.startsWith('Fn::') || k.startsWith('Ref'));
88
}
99

10+
function toTags(obj, serverless) {
11+
const tags = [];
12+
13+
if (!obj) {
14+
return tags;
15+
}
16+
17+
if (_.isPlainObject(obj)) {
18+
_.forEach(
19+
obj,
20+
(Value, Key) => tags.push({ Key, Value: Value.toString() }));
21+
} else {
22+
throw new serverless.classes
23+
.Error('Unable to parse tags, it should be an object.');
24+
}
25+
26+
return tags;
27+
}
28+
1029
module.exports = {
1130
isIntrinsic,
1231
compileStateMachines() {
@@ -16,6 +35,7 @@ module.exports = {
1635
let DefinitionString;
1736
let RoleArn;
1837
let DependsOn = [];
38+
const Tags = toTags(this.serverless.service.provider.tags, this.serverless);
1939

2040
if (stateMachineObj.definition) {
2141
if (typeof stateMachineObj.definition === 'string') {
@@ -83,6 +103,11 @@ module.exports = {
83103
}
84104
}
85105

106+
if (stateMachineObj.tags) {
107+
const stateMachineTags = toTags(stateMachineObj.tags, this.serverless);
108+
_.forEach(stateMachineTags, tag => Tags.push(tag));
109+
}
110+
86111
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,
87112
stateMachineObj);
88113
const stateMachineOutputLogicalId = this
@@ -92,6 +117,7 @@ module.exports = {
92117
Properties: {
93118
DefinitionString,
94119
RoleArn,
120+
Tags,
95121
},
96122
DependsOn,
97123
};

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,128 @@ 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+
team: 'core',
495+
score: 42,
496+
},
497+
},
498+
myStateMachine2: {
499+
definition: 'definition2',
500+
name: 'stateMachineBeta2',
501+
tags: {
502+
team: 'core',
503+
score: 42,
504+
},
505+
},
506+
},
507+
};
508+
509+
serverlessStepFunctions.compileStateMachines();
510+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
511+
.provider.compiledCloudFormationTemplate.Resources
512+
.StateMachineBeta1;
513+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
514+
.provider.compiledCloudFormationTemplate.Resources
515+
.StateMachineBeta2;
516+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
517+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
518+
expect(stateMachineBeta1.Properties.Tags)
519+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
520+
expect(stateMachineBeta2.Properties.Tags)
521+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
522+
});
523+
524+
it('should add global tags', () => {
525+
serverless.service.provider.tags = {
526+
team: 'core',
527+
score: 42,
528+
};
529+
530+
serverless.service.stepFunctions = {
531+
stateMachines: {
532+
myStateMachine1: {
533+
definition: 'definition1',
534+
name: 'stateMachineBeta1',
535+
},
536+
myStateMachine2: {
537+
definition: 'definition2',
538+
name: 'stateMachineBeta2',
539+
},
540+
},
541+
};
542+
543+
serverlessStepFunctions.compileStateMachines();
544+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
545+
.provider.compiledCloudFormationTemplate.Resources
546+
.StateMachineBeta1;
547+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
548+
.provider.compiledCloudFormationTemplate.Resources
549+
.StateMachineBeta2;
550+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
551+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
552+
expect(stateMachineBeta1.Properties.Tags)
553+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
554+
expect(stateMachineBeta2.Properties.Tags)
555+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
556+
});
557+
558+
it('should merge global and state machine tags', () => {
559+
serverless.service.provider.tags = {
560+
team: 'core',
561+
};
562+
563+
serverless.service.stepFunctions = {
564+
stateMachines: {
565+
myStateMachine1: {
566+
definition: 'definition1',
567+
name: 'stateMachineBeta1',
568+
tags: {
569+
score: 42,
570+
},
571+
},
572+
myStateMachine2: {
573+
definition: 'definition2',
574+
name: 'stateMachineBeta2',
575+
tags: {
576+
score: 42,
577+
},
578+
},
579+
},
580+
};
581+
582+
serverlessStepFunctions.compileStateMachines();
583+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
584+
.provider.compiledCloudFormationTemplate.Resources
585+
.StateMachineBeta1;
586+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
587+
.provider.compiledCloudFormationTemplate.Resources
588+
.StateMachineBeta2;
589+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
590+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
591+
expect(stateMachineBeta1.Properties.Tags)
592+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
593+
expect(stateMachineBeta2.Properties.Tags)
594+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
595+
});
596+
597+
it('should throw error when tags property contains malformed tags', () => {
598+
serverless.service.stepFunctions = {
599+
stateMachines: {
600+
myStateMachine1: {
601+
definition: 'definition1',
602+
name: 'stateMachineBeta1',
603+
tags: ['team:core'],
604+
},
605+
},
606+
};
607+
608+
expect(() => serverlessStepFunctions.compileStateMachines()).to.throw(Error);
609+
});
486610
});

0 commit comments

Comments
 (0)