@@ -10,7 +10,7 @@ const AWS = require('aws-sdk')
1010 * @returns {boolean }
1111 */
1212const 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' ]
1414 const validTriggerFields = [ 'cfTriggerName' , 'lambdaFunctionName' , 'lambdaFunctionVersion' , 'lambdaCodeS3Key' , 'lambdaCodeFilePath' ]
1515
1616 // ensure all fields in the config are expected
@@ -123,9 +123,14 @@ const getCloudFrontDistributionConfig = async (distributionID, region) => {
123123 * @param {CloudFront.EventType } triggerName The name of the trigger event ['viewer-request'|'origin-request'|'origin-response'|'viewer-response']
124124 * @returns {* }
125125 */
126- const changeCloudFrontDistributionLambdaARN = ( distributionConfig , lambdaARN , triggerName ) => {
126+ const changeCloudFrontDistributionLambdaARN = ( distributionConfig , lambdaARN , triggerName , cacheBehavior ) => {
127+
127128 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 )
129134
130135 if ( lambdaFunction . LambdaFunctionARN !== lambdaARN ) {
131136 lambdaFunction . LambdaFunctionARN = lambdaARN
@@ -295,7 +300,7 @@ const publishLambdas = async (config) => {
295300 * @return {Promise<void> }
296301 */
297302const activateLambdas = async ( config ) => {
298- if ( ! validateConfig ( config , [ 'cfDistributionID' ] , [ 'lambdaFunctionName' ] ) ) {
303+ if ( ! validateConfig ( config , [ 'cfDistributionID' , 'cacheBehavior' ] , [ 'lambdaFunctionName' ] ) ) {
299304 throw new Error ( 'Invalid config.' )
300305 }
301306
@@ -309,11 +314,11 @@ const activateLambdas = async (config) => {
309314 } ) )
310315
311316 console . log ( 'Activating the following ARNs:' , lambdaARNs )
312-
317+ const cacheBehavior = config . cacheBehavior
313318 // then, set the arns in the config (filter out missing arns)
314319 const updatedConfig = Object . entries ( lambdaARNs )
315320 . filter ( ( [ , arn ] ) => ! ! arn )
316- . reduce ( ( config , [ triggerName , arn ] ) => changeCloudFrontDistributionLambdaARN ( config , arn , triggerName ) , distroConfig )
321+ . reduce ( ( config , [ triggerName , arn ] ) => changeCloudFrontDistributionLambdaARN ( config , arn , triggerName , cacheBehavior ) , distroConfig )
317322
318323 // do not update if this is a dry run
319324 if ( config . dryRun ) {
0 commit comments