Skip to content

Commit

Permalink
Merge pull request #20 from unleashlive/master
Browse files Browse the repository at this point in the history
Support Fn::ImportValue as distribution id
  • Loading branch information
horike37 authored Sep 30, 2021
2 parents 279a691 + 3a8fee0 commit ed639ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ 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]

if (event.preExistingCloudFront.stage !== undefined &&
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 === '*') {
Expand All @@ -61,7 +63,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {

await this.provider
.request('CloudFront', 'updateDistribution', {
Id: event.preExistingCloudFront.distributionId,
Id: resolvedDistributionId,
IfMatch: config.ETag,
DistributionConfig: config.DistributionConfig
})
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ed639ec

Please sign in to comment.