Skip to content

Commit 65b5356

Browse files
committed
first commit
0 parents  commit 65b5356

File tree

9 files changed

+9133
-0
lines changed

9 files changed

+9133
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.eslintrc.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
root: true,
3+
extends: 'prettier',
4+
plugins: ['import', 'prettier'],
5+
env: {
6+
es6: true,
7+
node: true,
8+
jest: true
9+
},
10+
parserOptions: {
11+
ecmaVersion: 2017,
12+
sourceType: 'module'
13+
},
14+
rules: {
15+
'array-bracket-spacing': [
16+
'error',
17+
'never',
18+
{
19+
objectsInArrays: false,
20+
arraysInArrays: false
21+
}
22+
],
23+
'arrow-parens': ['error', 'always'],
24+
'comma-dangle': ['error', 'never'],
25+
'func-names': 'off',
26+
'no-use-before-define': 'off',
27+
'prefer-destructuring': 'off',
28+
'no-console': 'error',
29+
'no-shadow': 'error',
30+
'no-undef': 'error',
31+
'object-curly-newline': 'off',
32+
'no-unused-vars': 'error',
33+
'semi': 'off',
34+
'object-shorthand': 'off',
35+
'prettier/prettier': 'error',
36+
'prefer-const': 'error'
37+
}
38+
}

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Logs
2+
*.log
3+
npm-debug.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
dist
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
.nyc_output
17+
18+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19+
.grunt
20+
21+
# node-waf configuration
22+
.lock-wscript
23+
24+
# Compiled binary addons (http://nodejs.org/api/addons.html)
25+
build/Release
26+
27+
# Dependency directory
28+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
29+
node_modules
30+
31+
# IDE stuff
32+
**/.idea
33+
34+
# OS stuff
35+
.DS_Store
36+
.tmp
37+
38+
# Serverless stuff
39+
admin.env
40+
.env
41+
tmp
42+
.coveralls.yml
43+
tmpdirs-serverless
44+
.eslintcache
45+
.serverless
46+
.vscode

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

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'] }

index.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict'
2+
3+
class ServerlessLambdaEdgePreExistingCloudFront {
4+
constructor(serverless, options) {
5+
this.serverless = serverless
6+
this.options = options || {}
7+
this.provider = this.serverless.getProvider('aws')
8+
this.service = this.serverless.service.service
9+
this.region = this.provider.getRegion()
10+
this.stage = this.provider.getStage()
11+
12+
this.hooks = {
13+
'after:aws:deploy:finalize:cleanup': async () => {
14+
this.serverless.service.getAllFunctions().forEach(async (functionName) => {
15+
const functionObj = this.serverless.service.getFunction(functionName)
16+
if (functionObj.events) {
17+
functionObj.events.forEach(async (event) => {
18+
if (event.preExistingCloudFront) {
19+
const config = await this.provider.request('CloudFront', 'getDistribution', {
20+
Id: event.preExistingCloudFront.distributionId
21+
})
22+
23+
if (event.preExistingCloudFront.pathPattern === '*') {
24+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
25+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
26+
event,
27+
functionObj.name
28+
)
29+
} else {
30+
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
31+
config.DistributionConfig.CacheBehaviors,
32+
event,
33+
functionObj.name
34+
)
35+
}
36+
37+
this.provider.request('CloudFront', 'updateDistribution', {
38+
Id: event.preExistingCloudFront.distributionId,
39+
IfMatch: config.ETag,
40+
DistributionConfig: config.DistributionConfig
41+
})
42+
}
43+
})
44+
}
45+
})
46+
}
47+
}
48+
}
49+
50+
async associateNonDefaultCacheBehaviors(cacheBehaviors, event, functionName) {
51+
for (let i = 0; i < cacheBehaviors.Items.length; i++) {
52+
if (event.preExistingCloudFront.pathPattern === cacheBehaviors.Items[i].PathPattern) {
53+
cacheBehaviors.Items[i].LambdaFunctionAssociations = await this.associateFunction(
54+
cacheBehaviors.Items[i].LambdaFunctionAssociations,
55+
event,
56+
functionName
57+
)
58+
}
59+
}
60+
return cacheBehaviors
61+
}
62+
63+
async associateFunction(lambdaFunctionAssociations, event, functionName) {
64+
const originals = lambdaFunctionAssociations.Items.filter(
65+
(x) => x.EventType !== event.preExistingCloudFront.eventType
66+
)
67+
lambdaFunctionAssociations.Items = originals
68+
lambdaFunctionAssociations.Items.push({
69+
LambdaFunctionARN: await this.getlatestVersionLambdaArn(functionName),
70+
IncludeBody: event.preExistingCloudFront.includeBody,
71+
EventType: event.preExistingCloudFront.eventType
72+
})
73+
lambdaFunctionAssociations.Quantity = lambdaFunctionAssociations.Items.length
74+
return lambdaFunctionAssociations
75+
}
76+
77+
async getlatestVersionLambdaArn(functionName, marker) {
78+
const args = {
79+
FunctionName: functionName,
80+
MaxItems: 50
81+
}
82+
83+
if (marker) {
84+
args['Marker'] = marker
85+
}
86+
87+
const versions = await this.provider.request('Lambda', 'listVersionsByFunction', args)
88+
89+
if (versions.NextMarker !== null) {
90+
return await this.getlatestVersion(functionName, versions.NextMarker)
91+
}
92+
let arn
93+
versions.Versions.forEach(async (functionObj) => {
94+
arn = functionObj.FunctionArn
95+
})
96+
return arn
97+
}
98+
}
99+
100+
module.exports = ServerlessLambdaEdgePreExistingCloudFront

0 commit comments

Comments
 (0)