Skip to content

Commit

Permalink
fix: cache behavior origin matching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
necipallef committed May 10, 2024
1 parent cfa3492 commit 7b1ff11
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions mgmt-lambda/handlers/updateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ApiException, ErrorCode } from '../exceptions'

/**
* @throws {ApiException}
* @throws {import('@aws-sdk/client-lambda').LambdaServiceException}
* @throws {import("@aws-sdk/client-lambda").LambdaServiceException}
*/
export async function handleUpdate(
lambdaClient: LambdaClient,
Expand Down Expand Up @@ -125,10 +125,12 @@ async function updateCloudFrontConfig(
let fpCacheBehaviorsFound = 0
let fpCacheBehaviorsUpdated = 0
const pathPatterns = []
const fpCDNOrigins = distributionConfig.Origins?.Items?.filter((it) => it.DomainName === defaults.FP_CDN_URL)
console.log('fpCDNOrigins.length', fpCDNOrigins?.length)

if (distributionConfig.DefaultCacheBehavior?.TargetOriginId === defaults.FP_CDN_URL) {
if (fpCDNOrigins?.some((origin) => origin.Id === distributionConfig.DefaultCacheBehavior?.TargetOriginId)) {
fpCacheBehaviorsFound++
const lambdas = distributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations?.Items?.filter(
const lambdas = distributionConfig.DefaultCacheBehavior?.LambdaFunctionAssociations?.Items?.filter(
(it) => it && it.EventType === 'origin-request' && it.LambdaFunctionARN?.includes(`${lambdaFunctionName}:`)
)
if (lambdas?.length === 1) {
Expand All @@ -143,7 +145,9 @@ async function updateCloudFrontConfig(
}
}

const fpCbs = distributionConfig.CacheBehaviors?.Items?.filter((it) => it.TargetOriginId === defaults.FP_CDN_URL)
const fpCbs = distributionConfig.CacheBehaviors?.Items?.filter((cb) =>
fpCDNOrigins?.some((origin) => origin.Id === cb.TargetOriginId)
)
if (fpCbs && fpCbs?.length > 0) {
fpCacheBehaviorsFound += fpCbs.length
fpCbs.forEach((cacheBehavior) => {
Expand Down Expand Up @@ -233,7 +237,7 @@ async function getLambdaFunctionInformation(
}

/**
* @throws {import('@aws-sdk/client-lambda').LambdaServiceException}
* @throws {import("@aws-sdk/client-lambda").LambdaServiceException}
* @throws {ApiException}
*/
async function updateLambdaFunctionCode(
Expand Down

0 comments on commit 7b1ff11

Please sign in to comment.