Skip to content

Commit 341732b

Browse files
committed
Refs #16: Global property section and property to retain functions
You can now apply global properties by adding the `lambdaAtEdge` property to your `custom` section of your `serverless.yml`. **Note:** This section currently only supports the follow option: * **`retain`** (optional): a boolean, default is `false`, which sets the DeletionPolicy of the function resource to `Retain`. Can be used to avoid the currently inevitable CloudFormation stack deletion failure. There are at least two schools of though on how to handle this issue. Hopefully AWS will have this fixed soon. Use at your own discretion. feat(deletionPolicy): Add deletionPolicy setting fix(readme): Correct deletionPolicy replication del time fix(nit): Fix spacing nits fix(global): add function to global location
1 parent e2e480a commit 341732b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ 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 `custom` section of your `serverless.yml`. **Note:** This section currently only supports the follow option:
76+
77+
* **`retain`** (optional): a boolean, default is `false`, which
78+
sets the [DeletionPolicy][DeletionPolicy] of the function resource to `Retain`. Can be
79+
used to avoid the currently inevitable [CloudFormation stack deletion failure][ReplicaDeleteFail]. There are at least [two schools of though][HandlingCFNFailure] on how to handle this issue. Hopefully AWS will have this fixed soon. Use at your own discretion.
80+
7581
For example:
7682

7783
```yml
@@ -89,6 +95,10 @@ functions:
8995
Or:
9096

9197
```yml
98+
custom:
99+
lambdaAtEdge:
100+
retain: true
101+
92102
functions:
93103
someImageHandlingFunction:
94104
name: '${self:custom.objectPrefix}-image-handling'
@@ -242,3 +252,6 @@ details.
242252

243253
[contributing]: https://github.com/silvermine/silvermine-info#contributing
244254
[FnAssoc]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-cachebehavior.html#cfn-cloudfront-distribution-cachebehavior-lambdafunctionassociations
255+
[DeletionPolicy]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html
256+
[ReplicaDeleteFail]: https://forums.aws.amazon.com/thread.jspa?threadID=260242&tstart=0
257+
[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;
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)