Skip to content

Commit afe1f9f

Browse files
authored
Merge pull request #19 from iDVB/master
Deletion Policy property as a workaround to CloudFormation Stack Delete Fail
2 parents e586c8b + 531b0c8 commit afe1f9f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ objects, the objects all have the same fields, each of which is explained here:
7272
with a specific cache behavior. If the path pattern is not defined here, the function
7373
will be associated with the default cache behavior for the specified distribution.
7474

75+
You can also apply global properties by adding the `lambdaAtEdge` property to your
76+
`custom` section of your `serverless.yml`. **Note:** This section currently only supports
77+
the follow option:
78+
79+
* **`retain`** (optional): a boolean (default `false`). If you set this value to
80+
`true`, it will set the [DeletionPolicy][DeletionPolicy] of the function resource to
81+
`Retain`. This can be used to avoid the currently-inevitable [CloudFormation stack
82+
deletion failure][ReplicaDeleteFail]. There are at least [two schools of
83+
thought][HandlingCFNFailure] on how to handle this issue. Hopefully AWS will have
84+
this fixed soon. Use at your own discretion.
85+
7586
For example:
7687

7788
```yml
@@ -89,6 +100,10 @@ functions:
89100
Or:
90101

91102
```yml
103+
custom:
104+
lambdaAtEdge:
105+
retain: true
106+
92107
functions:
93108
someImageHandlingFunction:
94109
name: '${self:custom.objectPrefix}-image-handling'
@@ -244,3 +259,6 @@ details.
244259

245260
[contributing]: https://github.com/silvermine/silvermine-info#contributing
246261
[FnAssoc]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-cachebehavior.html#cfn-cloudfront-distribution-cachebehavior-lambdafunctionassociations
262+
[DeletionPolicy]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html
263+
[ReplicaDeleteFail]: https://forums.aws.amazon.com/thread.jspa?threadID=260242&tstart=0
264+
[HandlingCFNFailure]: https://github.com/silvermine/serverless-plugin-cloudfront-lambda-edge/pull/19

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = Class.extend({
1010
this._serverless = serverless;
1111
this._provider = serverless ? serverless.getProvider('aws') : null;
1212
this._opts = opts;
13+
this._custom = serverless.service ? serverless.service.custom : null;
1314

1415
if (!this._provider) {
1516
throw new Error('This plugin must be used with AWS');
@@ -90,10 +91,12 @@ module.exports = Class.extend({
9091
pathPattern = lambdaAtEdge.pathPattern,
9192
outputName = this._provider.naming.getLambdaVersionOutputLogicalId(fnName),
9293
distName = lambdaAtEdge.distribution,
94+
fnObj = template.Resources[fnLogicalName],
9395
fnProps = template.Resources[fnLogicalName].Properties,
9496
evtType = lambdaAtEdge.eventType,
9597
output = template.Outputs[outputName],
9698
dist = template.Resources[distName],
99+
retainFunctions = this._custom && this._custom.lambdaAtEdge && (this._custom.lambdaAtEdge.retain === true),
97100
distConfig, cacheBehavior, fnAssociations, versionLogicalID;
98101

99102
if (!_.contains(VALID_EVENT_TYPES, evtType)) {
@@ -130,6 +133,10 @@ module.exports = Class.extend({
130133
}
131134
}
132135

136+
if (retainFunctions) {
137+
fnObj.DeletionPolicy = 'Retain';
138+
}
139+
133140
distConfig = dist.Properties.DistributionConfig;
134141

135142
if (pathPattern) {

0 commit comments

Comments
 (0)