diff --git a/index.js b/index.js index bfa283e..3dce0fc 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,6 @@ class ServerlessLambdaEdgePreExistingCloudFront { const events = functionObj.events.filter( (event) => event.preExistingCloudFront && this.checkAllowedDeployStage() ) - for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] @@ -31,16 +30,19 @@ class ServerlessLambdaEdgePreExistingCloudFront { event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue } const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) - + const resolvedDistributionId = await (event.preExistingCloudFront.distributionId['Fn::ImportValue'] + ? this.resolveCfImportValue(this.provider, event.preExistingCloudFront.distributionId['Fn::ImportValue']) + : event.preExistingCloudFront.distributionId + ) this.serverless.cli.consoleLog( - `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` + `${functionArn} (Event: ${event.preExistingCloudFront.eventType}, pathPattern: ${event.preExistingCloudFront.pathPattern}) is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` ) let retryCount = 5 const updateDistribution = async () => { const config = await this.provider.request('CloudFront', 'getDistribution', { - Id: event.preExistingCloudFront.distributionId + Id: resolvedDistributionId }) if (event.preExistingCloudFront.pathPattern === '*') { @@ -61,7 +63,7 @@ class ServerlessLambdaEdgePreExistingCloudFront { await this.provider .request('CloudFront', 'updateDistribution', { - Id: event.preExistingCloudFront.distributionId, + Id: resolvedDistributionId, IfMatch: config.ETag, DistributionConfig: config.DistributionConfig }) @@ -106,7 +108,9 @@ class ServerlessLambdaEdgePreExistingCloudFront { this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'preExistingCloudFront', { type: 'object', properties: { - distributionId: { type: 'string' }, + distributionId: { + anyOf: [{ type: 'string' }, { type: 'object' }], + }, eventType: { type: 'string' }, pathPattern: { type: 'string' }, includeBody: { type: 'boolean' }, @@ -183,5 +187,19 @@ class ServerlessLambdaEdgePreExistingCloudFront { }) return arn } + + resolveCfImportValue(provider, name, sdkParams = {}) { + return provider.request('CloudFormation', 'listExports', sdkParams).then(result => { + const targetExportMeta = result.Exports.find(exportMeta => exportMeta.Name === name); + if (targetExportMeta) return targetExportMeta.Value; + if (result.NextToken) { + return this.resolveCfImportValue(provider, name, { NextToken: result.NextToken }); + } + + throw new Error( + `Could not resolve Fn::ImportValue with name ${name}. Are you sure this value is exported ?` + ); + }); + } } module.exports = ServerlessLambdaEdgePreExistingCloudFront diff --git a/package-lock.json b/package-lock.json index 31faa12..1c3b900 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-lambda-edge-pre-existing-cloudfront", - "version": "1.1.5", + "version": "1.1.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b276e55..f7749cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-lambda-edge-pre-existing-cloudfront", - "version": "1.1.5", + "version": "1.1.6", "description": "The Serverless Framework plugin which creates Lambda@Edge against pre-existing CloudFront.", "main": "index.js", "author": "serverless-operations",