Skip to content

Commit 2753413

Browse files
Merge pull request #288 from maafk/tags-optional
fix: #287 don't include Tags property if no tags provided
2 parents 7e84a8a + 858d826 commit 2753413

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,25 @@ stepFunctions:
980980

981981
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.
982982

983+
If you _don't_ want for global tags to be merged into your state machine, you can include the `inheritGlobalTags` property for your state machine.
984+
985+
```yaml
986+
provider:
987+
tags:
988+
app: myApp
989+
department: engineering
990+
stepFunctions:
991+
stateMachines:
992+
hellostepfunc1:
993+
name: myStateMachine
994+
inheritGlobalTags: false
995+
tags:
996+
score: 42
997+
definition: something
998+
```
999+
1000+
As a result, `hellostepfunc1` will only have the tag of `score: 42`, and _not_ the tags at the provider level
1001+
9831002
## Commands
9841003

9851004
### deploy

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ module.exports = {
9999
let RoleArn;
100100
let DependsOn = [];
101101
let LoggingConfiguration;
102-
const Tags = toTags(this.serverless.service.provider.tags);
102+
let Tags;
103+
if (stateMachineObj.inheritGlobalTags === false) {
104+
Tags = [];
105+
} else {
106+
Tags = toTags(this.serverless.service.provider.tags);
107+
}
103108

104109
const { error, value } = Joi.validate(stateMachineObj, schema, { allowUnknown: false });
105110
if (error) {
@@ -202,17 +207,20 @@ module.exports = {
202207

203208
const stateMachineOutputLogicalId = this
204209
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
210+
205211
const stateMachineTemplate = {
206212
Type: 'AWS::StepFunctions::StateMachine',
207213
Properties: {
208214
DefinitionString,
209215
RoleArn,
210-
Tags,
211216
StateMachineType: stateMachineObj.type,
212217
LoggingConfiguration,
213218
},
214219
DependsOn,
215220
};
221+
if (Tags.length > 0) {
222+
stateMachineTemplate.Properties.Tags = Tags;
223+
}
216224

217225
const newStateMachineObject = {
218226
[stateMachineLogicalId]: stateMachineTemplate,

lib/deploy/stepFunctions/compileStateMachines.schema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const definition = Joi.alternatives().try(
2121
Joi.object(),
2222
);
2323

24+
const inheritGlobalTags = Joi.boolean();
25+
2426
const dependsOn = Joi.alternatives().try(
2527
Joi.string(),
2628
Joi.array().items(Joi.string()),
@@ -54,6 +56,7 @@ const schema = Joi.object().keys({
5456
notifications,
5557
type,
5658
loggingConfig,
59+
inheritGlobalTags,
5760
});
5861

5962
module.exports = schema;

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,76 @@ describe('#compileStateMachines', () => {
546546
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
547547
});
548548

549+
it('should not add tags property to state machine if none are provided', () => {
550+
serverless.service.stepFunctions = {
551+
stateMachines: {
552+
myStateMachine1: {
553+
definition: 'definition1',
554+
name: 'stateMachineBeta1',
555+
},
556+
},
557+
};
558+
serverlessStepFunctions.compileStateMachines();
559+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
560+
.provider.compiledCloudFormationTemplate.Resources
561+
.StateMachineBeta1;
562+
expect(stateMachineBeta1.Properties).to.not.have.property('Tags');
563+
});
564+
565+
it('should not inherit global tags if inheritGlobalTags is set to false', () => {
566+
serverless.service.provider.tags = {
567+
team: 'core',
568+
};
569+
570+
serverless.service.stepFunctions = {
571+
stateMachines: {
572+
myStateMachine1: {
573+
definition: 'definition1',
574+
name: 'stateMachineBeta1',
575+
inheritGlobalTags: false,
576+
tags: {
577+
score: 42,
578+
},
579+
},
580+
myStateMachine2: {
581+
definition: 'definition2',
582+
name: 'stateMachineBeta2',
583+
tags: {
584+
score: 42,
585+
},
586+
},
587+
myStateMachine3: {
588+
definition: 'definition3',
589+
name: 'stateMachineBeta3',
590+
inheritGlobalTags: true,
591+
tags: {
592+
question: 'Meaning of life',
593+
answer: 42,
594+
},
595+
},
596+
},
597+
};
598+
serverlessStepFunctions.compileStateMachines();
599+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
600+
.provider.compiledCloudFormationTemplate.Resources
601+
.StateMachineBeta1;
602+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
603+
.provider.compiledCloudFormationTemplate.Resources
604+
.StateMachineBeta2;
605+
const stateMachineBeta3 = serverlessStepFunctions.serverless.service
606+
.provider.compiledCloudFormationTemplate.Resources
607+
.StateMachineBeta3;
608+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(1);
609+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
610+
expect(stateMachineBeta3.Properties.Tags).to.have.lengthOf(3);
611+
expect(stateMachineBeta1.Properties.Tags)
612+
.to.deep.eq([{ Key: 'score', Value: '42' }]);
613+
expect(stateMachineBeta2.Properties.Tags)
614+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
615+
expect(stateMachineBeta3.Properties.Tags)
616+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'question', Value: 'Meaning of life' }, { Key: 'answer', Value: '42' }]);
617+
});
618+
549619
it('should throw error when tags property contains malformed tags', () => {
550620
serverless.service.stepFunctions = {
551621
stateMachines: {

0 commit comments

Comments
 (0)