@@ -10,7 +10,7 @@ const AWS = require('aws-sdk')
10
10
* @returns {boolean }
11
11
*/
12
12
const validateConfig = ( config , requiredFields = [ ] , requiredTriggerFields = [ ] ) => {
13
- const validFields = [ 'dryRun' , 'awsRegion' , 'cfDistributionID' , 'autoIncrementVersion' , 'lambdaCodeS3Bucket' , 'lambdaCodeS3Bucket' , 'cfTriggers' ]
13
+ const validFields = [ 'dryRun' , 'awsRegion' , 'cfDistributionID' , 'autoIncrementVersion' , 'lambdaCodeS3Bucket' , 'lambdaCodeS3Bucket' , 'cfTriggers' , 'cacheBehavior' ]
14
14
const validTriggerFields = [ 'cfTriggerName' , 'lambdaFunctionName' , 'lambdaFunctionVersion' , 'lambdaCodeS3Key' , 'lambdaCodeFilePath' ]
15
15
16
16
// ensure all fields in the config are expected
@@ -123,9 +123,14 @@ const getCloudFrontDistributionConfig = async (distributionID, region) => {
123
123
* @param {CloudFront.EventType } triggerName The name of the trigger event ['viewer-request'|'origin-request'|'origin-response'|'viewer-response']
124
124
* @returns {* }
125
125
*/
126
- const changeCloudFrontDistributionLambdaARN = ( distributionConfig , lambdaARN , triggerName ) => {
126
+ const changeCloudFrontDistributionLambdaARN = ( distributionConfig , lambdaARN , triggerName , cacheBehavior ) => {
127
+
127
128
try {
128
- const lambdaFunction = distributionConfig . DistributionConfig . DefaultCacheBehavior . LambdaFunctionAssociations . Items . find ( item => item . EventType === triggerName )
129
+ const cacheBehaviors = cacheBehavior === 'default' ?
130
+ distributionConfig . DefaultCacheBehavior :
131
+ distributionConfig . CacheBehaviors . Items . find ( item => item . PathPattern == cacheBehavior )
132
+
133
+ const lambdaFunction = cacheBehaviors . LambdaFunctionAssociations . Items . find ( item => item . EventType === triggerName )
129
134
130
135
if ( lambdaFunction . LambdaFunctionARN !== lambdaARN ) {
131
136
lambdaFunction . LambdaFunctionARN = lambdaARN
@@ -295,7 +300,7 @@ const publishLambdas = async (config) => {
295
300
* @return {Promise<void> }
296
301
*/
297
302
const activateLambdas = async ( config ) => {
298
- if ( ! validateConfig ( config , [ 'cfDistributionID' ] , [ 'lambdaFunctionName' ] ) ) {
303
+ if ( ! validateConfig ( config , [ 'cfDistributionID' , 'cacheBehavior' ] , [ 'lambdaFunctionName' ] ) ) {
299
304
throw new Error ( 'Invalid config.' )
300
305
}
301
306
@@ -309,11 +314,11 @@ const activateLambdas = async (config) => {
309
314
} ) )
310
315
311
316
console . log ( 'Activating the following ARNs:' , lambdaARNs )
312
-
317
+ const cacheBehavior = config . cacheBehavior
313
318
// then, set the arns in the config (filter out missing arns)
314
319
const updatedConfig = Object . entries ( lambdaARNs )
315
320
. filter ( ( [ , arn ] ) => ! ! arn )
316
- . reduce ( ( config , [ triggerName , arn ] ) => changeCloudFrontDistributionLambdaARN ( config , arn , triggerName ) , distroConfig )
321
+ . reduce ( ( config , [ triggerName , arn ] ) => changeCloudFrontDistributionLambdaARN ( config , arn , triggerName , cacheBehavior ) , distroConfig )
317
322
318
323
// do not update if this is a dry run
319
324
if ( config . dryRun ) {
0 commit comments