Skip to content

Commit ed639ec

Browse files
authored
Merge pull request #20 from unleashlive/master
Support Fn::ImportValue as distribution id
2 parents 279a691 + 3a8fee0 commit ed639ec

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

index.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ class ServerlessLambdaEdgePreExistingCloudFront {
2323
const events = functionObj.events.filter(
2424
(event) => event.preExistingCloudFront && this.checkAllowedDeployStage()
2525
)
26-
2726
for (let idx = 0; idx < events.length; idx += 1) {
2827
const event = events[idx]
2928

3029
if (event.preExistingCloudFront.stage !== undefined &&
3130
event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue }
3231

3332
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
34-
33+
const resolvedDistributionId = await (event.preExistingCloudFront.distributionId['Fn::ImportValue']
34+
? this.resolveCfImportValue(this.provider, event.preExistingCloudFront.distributionId['Fn::ImportValue'])
35+
: event.preExistingCloudFront.distributionId
36+
)
3537
this.serverless.cli.consoleLog(
36-
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
38+
`${functionArn} (Event: ${event.preExistingCloudFront.eventType}, pathPattern: ${event.preExistingCloudFront.pathPattern}) is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.`
3739
)
3840

3941
let retryCount = 5
4042

4143
const updateDistribution = async () => {
4244
const config = await this.provider.request('CloudFront', 'getDistribution', {
43-
Id: event.preExistingCloudFront.distributionId
45+
Id: resolvedDistributionId
4446
})
4547

4648
if (event.preExistingCloudFront.pathPattern === '*') {
@@ -61,7 +63,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
6163

6264
await this.provider
6365
.request('CloudFront', 'updateDistribution', {
64-
Id: event.preExistingCloudFront.distributionId,
66+
Id: resolvedDistributionId,
6567
IfMatch: config.ETag,
6668
DistributionConfig: config.DistributionConfig
6769
})
@@ -106,7 +108,9 @@ class ServerlessLambdaEdgePreExistingCloudFront {
106108
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'preExistingCloudFront', {
107109
type: 'object',
108110
properties: {
109-
distributionId: { type: 'string' },
111+
distributionId: {
112+
anyOf: [{ type: 'string' }, { type: 'object' }],
113+
},
110114
eventType: { type: 'string' },
111115
pathPattern: { type: 'string' },
112116
includeBody: { type: 'boolean' },
@@ -183,5 +187,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
183187
})
184188
return arn
185189
}
190+
191+
resolveCfImportValue(provider, name, sdkParams = {}) {
192+
return provider.request('CloudFormation', 'listExports', sdkParams).then(result => {
193+
const targetExportMeta = result.Exports.find(exportMeta => exportMeta.Name === name);
194+
if (targetExportMeta) return targetExportMeta.Value;
195+
if (result.NextToken) {
196+
return this.resolveCfImportValue(provider, name, { NextToken: result.NextToken });
197+
}
198+
199+
throw new Error(
200+
`Could not resolve Fn::ImportValue with name ${name}. Are you sure this value is exported ?`
201+
);
202+
});
203+
}
186204
}
187205
module.exports = ServerlessLambdaEdgePreExistingCloudFront

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-lambda-edge-pre-existing-cloudfront",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "The Serverless Framework plugin which creates Lambda@Edge against pre-existing CloudFront.",
55
"main": "index.js",
66
"author": "serverless-operations",

0 commit comments

Comments
 (0)