Skip to content

Commit 89668fb

Browse files
author
Allegra Chen
committed
Add the stage property and stage checking for each cloudfront distribution
1 parent 305fd32 commit 89668fb

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

index.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,39 @@ class ServerlessLambdaEdgePreExistingCloudFront {
2626

2727
for (let idx = 0; idx < events.length; idx += 1) {
2828
const event = events[idx]
29-
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
30-
const config = await this.provider.request('CloudFront', 'getDistribution', {
31-
Id: event.preExistingCloudFront.distributionId
32-
})
3329

34-
if (event.preExistingCloudFront.pathPattern === '*') {
35-
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
36-
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
37-
event,
38-
functionObj.name,
39-
functionArn
40-
)
41-
} else {
42-
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
43-
config.DistributionConfig.CacheBehaviors,
44-
event,
45-
functionObj.name,
46-
functionArn
30+
if (event.preExistingCloudFront.stage == `${serverless.service.provider.stage}`) {
31+
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
32+
const config = await this.provider.request('CloudFront', 'getDistribution', {
33+
Id: event.preExistingCloudFront.distributionId
34+
})
35+
36+
if (event.preExistingCloudFront.pathPattern === '*') {
37+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
38+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
39+
event,
40+
functionObj.name,
41+
functionArn
42+
)
43+
} else {
44+
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
45+
config.DistributionConfig.CacheBehaviors,
46+
event,
47+
functionObj.name,
48+
functionArn
49+
)
50+
}
51+
52+
this.serverless.cli.consoleLog(
53+
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
4754
)
55+
56+
await this.provider.request('CloudFront', 'updateDistribution', {
57+
Id: event.preExistingCloudFront.distributionId,
58+
IfMatch: config.ETag,
59+
DistributionConfig: config.DistributionConfig
60+
})
4861
}
49-
50-
this.serverless.cli.consoleLog(
51-
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
52-
)
53-
54-
await this.provider.request('CloudFront', 'updateDistribution', {
55-
Id: event.preExistingCloudFront.distributionId,
56-
IfMatch: config.ETag,
57-
DistributionConfig: config.DistributionConfig
58-
})
5962
}
6063
})
6164
}, Promise.resolve())
@@ -85,9 +88,10 @@ class ServerlessLambdaEdgePreExistingCloudFront {
8588
distributionId: { type: 'string' },
8689
eventType: { type: 'string' },
8790
pathPattern: { type: 'string' },
88-
includeBody: { type: 'boolean' }
91+
includeBody: { type: 'boolean' },
92+
stage: { type: 'string' }
8993
},
90-
required: ['distributionId', 'eventType', 'pathPattern', 'includeBody']
94+
required: ['distributionId', 'eventType', 'pathPattern', 'includeBody', 'stage']
9195
})
9296
}
9397
}

0 commit comments

Comments
 (0)