Skip to content

Commit 4d2d5f6

Browse files
committed
Added support for additional CloudFront CacheBehaviors
1 parent 2ec5554 commit 4d2d5f6

File tree

4 files changed

+252
-81
lines changed

4 files changed

+252
-81
lines changed

api.test.js

Lines changed: 166 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe('update lambda edge API', () => {
8686
fakeConfig = {
8787
awsRegion: undefined,
8888
cfDistributionID: 'cloudfront',
89+
cacheBehaviorPath: 'default',
8990
autoIncrementVersion: true,
9091
lambdaCodeS3Bucket: 'bucket',
9192
cfTriggers: [
@@ -316,8 +317,9 @@ describe('update lambda edge API', () => {
316317
})
317318

318319
describe('activateLambdas()', () => {
320+
let cloudFrontDistributionConfig
319321
beforeEach(() => {
320-
cloudFrontGetDistributionConfigMock.mockResolvedValue({
322+
cloudFrontDistributionConfig = {
321323
ETag: 'etag',
322324
DistributionConfig: {
323325
DefaultCacheBehavior: {
@@ -341,9 +343,38 @@ describe('update lambda edge API', () => {
341343
}
342344
]
343345
}
344-
}
345-
}
346-
})
346+
},
347+
CacheBehaviors: {
348+
Items: [
349+
{
350+
PathPattern: '/*',
351+
LambdaFunctionAssociations: {
352+
Items: [
353+
{
354+
EventType: 'viewer-request',
355+
LambdaFunctionARN: 'old-arn'
356+
},
357+
{
358+
EventType: 'origin-request',
359+
LambdaFunctionARN: 'old-arn'
360+
},
361+
{
362+
EventType: 'origin-response',
363+
LambdaFunctionARN: 'old-arn'
364+
},
365+
{
366+
EventType: 'viewer-response',
367+
LambdaFunctionARN: 'old-arn'
368+
}
369+
]
370+
}
371+
}
372+
]
373+
},
374+
},
375+
}
376+
377+
cloudFrontGetDistributionConfigMock.mockResolvedValue(cloudFrontDistributionConfig)
347378

348379
lambdaListVersionsByFunctionMock.mockImplementation(() => ({
349380
Versions: [
@@ -369,7 +400,7 @@ describe('update lambda edge API', () => {
369400
return expect(activateLambdas(fakeConfig)).rejects.toThrow('Invalid config.')
370401
})
371402

372-
it('should update the distribution config with the latest ARNs', async () => {
403+
it('should update the default cache behavior with the latest ARNs', async () => {
373404
await activateLambdas(fakeConfig)
374405

375406
expect(cloudFrontUpdateDistributionMock).toHaveBeenCalledTimes(1)
@@ -398,12 +429,101 @@ describe('update lambda edge API', () => {
398429
}
399430
]
400431
}
432+
},
433+
CacheBehaviors: {
434+
Items: [
435+
{
436+
PathPattern: '/*',
437+
LambdaFunctionAssociations: {
438+
Items: [
439+
{
440+
EventType: 'viewer-request',
441+
LambdaFunctionARN: 'old-arn'
442+
},
443+
{
444+
EventType: 'origin-request',
445+
LambdaFunctionARN: 'old-arn'
446+
},
447+
{
448+
EventType: 'origin-response',
449+
LambdaFunctionARN: 'old-arn'
450+
},
451+
{
452+
EventType: 'viewer-response',
453+
LambdaFunctionARN: 'old-arn'
454+
}
455+
]
456+
}
457+
}
458+
]
401459
}
402460
}
403461
})
404462
})
405463

406-
it('should update the distribution config with specific version ARNs', async () => {
464+
it('should update the specified cache behavior with the latest ARNs', async () => {
465+
fakeConfig.cacheBehaviorPath = '/*'
466+
await activateLambdas(fakeConfig)
467+
468+
expect(cloudFrontUpdateDistributionMock).toHaveBeenCalledTimes(1)
469+
expect(cloudFrontUpdateDistributionMock).toHaveBeenCalledWith({
470+
Id: 'cloudfront',
471+
IfMatch: 'etag',
472+
DistributionConfig: {
473+
DefaultCacheBehavior: {
474+
LambdaFunctionAssociations: {
475+
Items: [
476+
{
477+
EventType: 'viewer-request',
478+
LambdaFunctionARN: 'old-arn'
479+
},
480+
{
481+
EventType: 'origin-request',
482+
LambdaFunctionARN: 'old-arn'
483+
},
484+
{
485+
EventType: 'origin-response',
486+
LambdaFunctionARN: 'old-arn'
487+
},
488+
{
489+
EventType: 'viewer-response',
490+
LambdaFunctionARN: 'old-arn'
491+
}
492+
]
493+
}
494+
},
495+
CacheBehaviors: {
496+
Items: [
497+
{
498+
PathPattern: '/*',
499+
LambdaFunctionAssociations: {
500+
Items: [
501+
{
502+
EventType: 'viewer-request',
503+
LambdaFunctionARN: 'arn-v3'
504+
},
505+
{
506+
EventType: 'origin-request',
507+
LambdaFunctionARN: 'arn-v3'
508+
},
509+
{
510+
EventType: 'origin-response',
511+
LambdaFunctionARN: 'arn-v3'
512+
},
513+
{
514+
EventType: 'viewer-response',
515+
LambdaFunctionARN: 'arn-v3'
516+
}
517+
]
518+
}
519+
}
520+
]
521+
}
522+
}
523+
})
524+
})
525+
526+
it('should update the default cache behavior with specific version ARNs', async () => {
407527
fakeConfig.cfTriggers[0].lambdaFunctionVersion = '1'
408528
fakeConfig.cfTriggers[1].lambdaFunctionVersion = '2'
409529
fakeConfig.cfTriggers[2].lambdaFunctionVersion = '3'
@@ -437,11 +557,50 @@ describe('update lambda edge API', () => {
437557
}
438558
]
439559
}
440-
}
560+
},
561+
CacheBehaviors: {
562+
Items: [
563+
{
564+
PathPattern: '/*',
565+
LambdaFunctionAssociations: {
566+
Items: [
567+
{
568+
EventType: 'viewer-request',
569+
LambdaFunctionARN: 'old-arn'
570+
},
571+
{
572+
EventType: 'origin-request',
573+
LambdaFunctionARN: 'old-arn'
574+
},
575+
{
576+
EventType: 'origin-response',
577+
LambdaFunctionARN: 'old-arn'
578+
},
579+
{
580+
EventType: 'viewer-response',
581+
LambdaFunctionARN: 'old-arn'
582+
}
583+
]
584+
}
585+
}
586+
]
587+
},
441588
}
442589
})
443590
})
444591

592+
it('should not update anything if invalid cache behavior path is supplied', async () => {
593+
fakeConfig.cacheBehaviorPath = '/invalid/path/pattern/*'
594+
await activateLambdas(fakeConfig)
595+
596+
expect(cloudFrontUpdateDistributionMock).toHaveBeenCalledTimes(1)
597+
expect(cloudFrontUpdateDistributionMock).toHaveBeenCalledWith({
598+
Id: 'cloudfront',
599+
IfMatch: 'etag',
600+
DistributionConfig: cloudFrontDistributionConfig.DistributionConfig
601+
})
602+
})
603+
445604
it('should not update the distribution config if it\'s a dry run', async () => {
446605
fakeConfig.dryRun = true
447606
await activateLambdas(fakeConfig)

bin/update-lambda-edge

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const createConfig = args => {
5252
dryRun: !!dryRun,
5353
cfDistributionID: distributionId || projectConfig.cfDistributionID,
5454
autoIncrementVersion: lambdaVersion ? false : projectConfig.autoIncrementVersion,
55-
cacheBehavior: projectConfig.cacheBehaviorPath || 'default',
55+
cacheBehaviorPath: cacheBehaviorPath || projectConfig.cacheBehaviorPath || 'default',
5656
lambdaCodeS3Bucket: bucket || projectConfig.lambdaCodeS3Bucket,
5757
awsRegion: region || projectConfig.awsRegion,
5858
cfTriggers: projectConfig.cfTriggers
@@ -69,7 +69,7 @@ const createConfig = args => {
6969
dryRun: !!dryRun,
7070
cfDistributionID: distributionId,
7171
autoIncrementVersion: lambdaVersion ? false : !!autoIncrement,
72-
cacheBehavior: cacheBehaviorPath || 'default',
72+
cacheBehaviorPath: cacheBehaviorPath || 'default',
7373
lambdaCodeS3Bucket: bucket,
7474
awsRegion: region,
7575
cfTriggers: [
@@ -160,6 +160,10 @@ const cli = yargs(hideBin(process.argv))
160160
type: 'string',
161161
description: 'The CloudFront distribution ID'
162162
})
163+
.option('cache-behavior-path', {
164+
type: 'string',
165+
description: 'The PathPattern of the CloudFront CacheBehavior to use. Leaving it blank will use the DefaultCacheBehavior'
166+
})
163167
.option('trigger-name', {
164168
alias: 't',
165169
type: 'string',
@@ -178,10 +182,6 @@ const cli = yargs(hideBin(process.argv))
178182
type: 'string',
179183
description: 'If true, will activate this version of the Lambda (overrides auto-increment)'
180184
})
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-
})
185185
}, args => activateLambdas(createConfig(args)))
186186
.option('pwd', {
187187
type: 'string',

0 commit comments

Comments
 (0)