Skip to content

Commit 2ec5554

Browse files
authored
have the option to pass the cache behavior by path (#4)
1 parent 82c396b commit 2ec5554

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

bin/update-lambda-edge

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const getActiveTriggers = ({vreq, oreq, vres, ores}) => {
3939
const createConfig = args => {
4040
// removes undefined properties
4141
const definedArgs = JSON.parse(JSON.stringify(args))
42-
const { config, pwd, filePath, distributionId, triggerName, functionName, lambdaVersion, autoIncrement, bucket, key, region, dryRun } = definedArgs
42+
const { config, pwd, filePath, distributionId, triggerName, functionName, lambdaVersion, autoIncrement, bucket, key, region, dryRun, cacheBehaviorPath } = definedArgs
4343

4444
let commandConfig
4545

@@ -52,6 +52,7 @@ const createConfig = args => {
5252
dryRun: !!dryRun,
5353
cfDistributionID: distributionId || projectConfig.cfDistributionID,
5454
autoIncrementVersion: lambdaVersion ? false : projectConfig.autoIncrementVersion,
55+
cacheBehavior: projectConfig.cacheBehaviorPath || 'default',
5556
lambdaCodeS3Bucket: bucket || projectConfig.lambdaCodeS3Bucket,
5657
awsRegion: region || projectConfig.awsRegion,
5758
cfTriggers: projectConfig.cfTriggers
@@ -68,6 +69,7 @@ const createConfig = args => {
6869
dryRun: !!dryRun,
6970
cfDistributionID: distributionId,
7071
autoIncrementVersion: lambdaVersion ? false : !!autoIncrement,
72+
cacheBehavior: cacheBehaviorPath || 'default',
7173
lambdaCodeS3Bucket: bucket,
7274
awsRegion: region,
7375
cfTriggers: [
@@ -176,6 +178,10 @@ const cli = yargs(hideBin(process.argv))
176178
type: 'string',
177179
description: 'If true, will activate this version of the Lambda (overrides auto-increment)'
178180
})
181+
.option('cache-behavior-path', {
182+
type: 'string',
183+
description: 'If set, will activate this version of the Lambda to the corresponding cache behavior'
184+
})
179185
}, args => activateLambdas(createConfig(args)))
180186
.option('pwd', {
181187
type: 'string',

index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const AWS = require('aws-sdk')
1010
* @returns {boolean}
1111
*/
1212
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']
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
*/
297302
const 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

Comments
 (0)