diff --git a/README.md b/README.md index d38f8da8..1baa027a 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ This is the Serverless Framework plugin for AWS Step Functions. - [Adding a custom name for a state machine](#adding-a-custom-name-for-a-statemachine) - [Adding a custom logical id for a stateMachine](#adding-a-custom-logical-id-for-a-statemachine) - [Depending on another logical id](#depending-on-another-logical-id) + - [Adding retain property for a state machine](#adding-retain-property-for-a-statemachine) - [CloudWatch Alarms](#cloudwatch-alarms) - [CloudWatch Notifications](#cloudwatch-notifications) - [Blue-Green deployments](#blue-green-deployment) @@ -219,6 +220,17 @@ stepFunctions: - myOtherDB - myStream ``` +### Adding retain property for a stateMachine +There are some practical cases when you would like to prevent state machine from deletion on stack delete or update. This can be achieved by adding `retain` property to the state machine section. + +```yaml +stepFunctions: + stateMachines: + myStateMachine: + retain: true +``` + +Configuring in such way adds `"DeletionPolicy" : "Retain"` to the state machine within CloudFormation template. ### CloudWatch Alarms diff --git a/lib/deploy/stepFunctions/compileStateMachines.js b/lib/deploy/stepFunctions/compileStateMachines.js index 3c2a0c3e..170df46b 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.js +++ b/lib/deploy/stepFunctions/compileStateMachines.js @@ -224,6 +224,10 @@ module.exports = { [stateMachineLogicalId]: stateMachineTemplate, }; + if (stateMachineObj.retain) { + newStateMachineObject[stateMachineLogicalId].DeletionPolicy = 'Retain'; + } + if (stateMachineObj.name) { newStateMachineObject[ stateMachineLogicalId].Properties.StateMachineName = stateMachineObj.name; diff --git a/lib/deploy/stepFunctions/compileStateMachines.schema.js b/lib/deploy/stepFunctions/compileStateMachines.schema.js index b95356a6..ecf3999d 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.schema.js +++ b/lib/deploy/stepFunctions/compileStateMachines.schema.js @@ -42,6 +42,7 @@ const alarms = Joi.object(); const notifications = Joi.object(); const useExactVersion = Joi.boolean().default(false); const type = Joi.string().valid('STANDARD', 'EXPRESS').default('STANDARD'); +const retain = Joi.boolean().default(false); const schema = Joi.object().keys({ id, @@ -55,6 +56,7 @@ const schema = Joi.object().keys({ alarms, notifications, type, + retain, loggingConfig, inheritGlobalTags, }); diff --git a/lib/deploy/stepFunctions/compileStateMachines.test.js b/lib/deploy/stepFunctions/compileStateMachines.test.js index 8c36bd03..6dd524ac 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.test.js +++ b/lib/deploy/stepFunctions/compileStateMachines.test.js @@ -1430,4 +1430,22 @@ describe('#compileStateMachines', () => { expect(Object.keys(serverlessStepFunctions.serverless.service.provider .compiledCloudFormationTemplate.Outputs).length).to.equal(0); }); + + it('should add DeletionPolicy when retain is true', () => { + serverless.service.stepFunctions = { + stateMachines: { + myStateMachine1: { + definition: 'definition1', + name: 'stateMachineBeta1', + retain: true, + }, + }, + }; + + serverlessStepFunctions.compileStateMachines(); + + expect(serverlessStepFunctions.serverless.service + .provider.compiledCloudFormationTemplate.Resources + .StateMachineBeta1.DeletionPolicy).to.equal('Retain'); + }); });