|
| 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