-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from wellcomecollection/upgrade-cache-invalid…
…ation-lambdas-to-node20 Upgrade cache invalidation lambdas to node20
- Loading branch information
Showing
8 changed files
with
1,088 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 49 additions & 58 deletions
107
cloudfront/invalidation/lambda/src/cacheInvalidation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,64 @@ | ||
import { expect, test } from '@jest/globals'; | ||
import { SNSEvent, SNSEventRecord } from 'aws-lambda/trigger/sns'; | ||
import { Context } from 'aws-lambda'; | ||
|
||
import { SNSEvent, Context } from 'aws-lambda'; | ||
import { CloudFrontClient } from '@aws-sdk/client-cloudfront' | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import { handler } from './cacheInvalidation'; | ||
|
||
import AWS from 'aws-sdk'; | ||
import { | ||
CreateInvalidationRequest, | ||
InvalidationBatch, | ||
} from 'aws-sdk/clients/cloudfront'; | ||
import AWSMock from 'aws-sdk-mock'; | ||
const timeStamp = new Date("2019-01-02T12:45:07.000Z").toISOString() | ||
|
||
test('makes correct invalidation request', async () => { | ||
process.env = { | ||
DISTRIBUTION_ID: "Distro McDistrFace", | ||
}; | ||
const message = { | ||
paths: ['/path/to/invalidate', '/path/with/wildcard*'], | ||
reference: 'my-test-reference', | ||
}; | ||
const fakeEvent = getFakeEvent(JSON.stringify(message)); | ||
|
||
AWSMock.setSDKInstance(AWS); | ||
let calledWith: CreateInvalidationRequest; | ||
AWSMock.mock( | ||
'CloudFront', | ||
'createInvalidation', | ||
(params: CreateInvalidationRequest, callback: Function) => { | ||
calledWith = params; | ||
callback(null, null); | ||
} | ||
); | ||
const mockEvent = getMockEvent(JSON.stringify(message)); | ||
|
||
const expectedInvalidation = { | ||
CallerReference: 'my-test-reference', | ||
Paths: { | ||
Quantity: 2, | ||
Items: ['/path/to/invalidate', '/path/with/wildcard*'], | ||
}, | ||
} as InvalidationBatch; | ||
const cloudFrontMock = mockClient(CloudFrontClient); | ||
|
||
await handler(fakeEvent, {} as Context, () => {}); | ||
expect(calledWith!.InvalidationBatch).toStrictEqual(expectedInvalidation); | ||
const expectedInvalidationCommand = { | ||
DistributionId: "Distro McDistrFace", | ||
InvalidationBatch: { | ||
Paths: { Quantity: message.paths.length, Items: message.paths }, | ||
CallerReference: timeStamp | ||
} | ||
}; | ||
|
||
AWSMock.restore('CloudFront'); | ||
await handler(mockEvent, {} as Context, () => {}); | ||
expect(cloudFrontMock.call(0).args[0].input).toStrictEqual(expectedInvalidationCommand) | ||
}); | ||
|
||
// note - this is boilerplate from aws docs | ||
function getFakeEvent(message: string): SNSEvent { | ||
function getMockEvent(message: string): SNSEvent { | ||
return { | ||
Records: [ | ||
"Records": [ | ||
{ | ||
EventVersion: '1.0', | ||
EventSubscriptionArn: | ||
'arn:aws:sns:us-east-2:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486', | ||
EventSource: 'aws:sns', | ||
Sns: { | ||
SignatureVersion: '1', | ||
Timestamp: '2019-01-02T12:45:07.000Z', | ||
Signature: | ||
'tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==', | ||
SigningCertUrl: | ||
'https://sns.us-east-2.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem', | ||
MessageId: '95df01b4-ee98-5cb9-9903-4c221d41eb5e', | ||
Type: 'Notification', | ||
UnsubscribeUrl: | ||
'https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-2:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486', | ||
TopicArn: 'arn:aws:sns:us-east-2:123456789012:sns-lambda', | ||
Subject: 'TestInvoke', | ||
Message: message, | ||
}, | ||
} as SNSEventRecord, | ||
], | ||
} as SNSEvent; | ||
} | ||
"EventVersion": "1.0", | ||
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", | ||
"EventSource": "aws:sns", | ||
"Sns": { | ||
"SignatureVersion": "1", | ||
"Timestamp": timeStamp, | ||
"Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==", | ||
"SigningCertUrl": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem", | ||
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", | ||
"Message": message, | ||
"MessageAttributes": { | ||
"Test": { | ||
"Type": "String", | ||
"Value": "TestString" | ||
}, | ||
"TestBinary": { | ||
"Type": "Binary", | ||
"Value": "TestBinary" | ||
} | ||
}, | ||
"Type": "Notification", | ||
"UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486", | ||
"TopicArn":"arn:aws:sns:us-east-1:123456789012:sns-lambda", | ||
"Subject": "TestInvoke" | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,27 @@ | ||
import { SNSHandler, SNSMessage } from 'aws-lambda/trigger/sns'; | ||
import * as AWS from 'aws-sdk'; | ||
import { CreateInvalidationRequest as CloudFrontInvalidationRequest } from 'aws-sdk/clients/cloudfront'; | ||
import {CloudFront} from "aws-sdk"; | ||
|
||
type IncomingMessage = { | ||
reference: string; | ||
paths: string[]; | ||
}; | ||
|
||
type InvalidationRequest = { | ||
distribution: string; | ||
reference: string; | ||
paths: string[]; | ||
}; | ||
|
||
function createCloudFrontRequest( | ||
invalidationRequest: InvalidationRequest | ||
): CloudFrontInvalidationRequest { | ||
return { | ||
DistributionId: invalidationRequest.distribution, | ||
InvalidationBatch: { | ||
CallerReference: invalidationRequest.reference, | ||
Paths: { | ||
import { SNSHandler, SNSEvent } from 'aws-lambda'; | ||
import { | ||
CloudFrontClient, | ||
CreateInvalidationCommand | ||
} from '@aws-sdk/client-cloudfront' | ||
|
||
export const handler: SNSHandler = async (event: SNSEvent) => { | ||
const cloudFrontClient = new CloudFrontClient(); | ||
|
||
const distributionId = String(process.env.DISTRIBUTION_ID); | ||
const invalidationRequest = JSON.parse(event.Records[0].Sns.Message) | ||
|
||
const createInvalidationCommandInput = { | ||
DistributionId: distributionId, | ||
InvalidationBatch: { | ||
Paths: { | ||
Quantity: invalidationRequest.paths.length, | ||
Items: invalidationRequest.paths, | ||
}, | ||
// we use the time (GMT) when the notification was published to identify the invalidation | ||
CallerReference: event.Records[0].Sns.Timestamp | ||
}, | ||
} as CloudFrontInvalidationRequest; | ||
} | ||
|
||
function createInvalidationRequest( | ||
distribution: string, | ||
message: SNSMessage | ||
): InvalidationRequest { | ||
const incomingMessage = JSON.parse(message.Message) as IncomingMessage; | ||
return { | ||
distribution: distribution, | ||
reference: incomingMessage.reference, | ||
paths: incomingMessage.paths, | ||
} as InvalidationRequest; | ||
} | ||
|
||
function runInvalidation( | ||
cloudfront:CloudFront, | ||
invalidationRequest: InvalidationRequest | ||
) { | ||
const cloudFrontRequest = createCloudFrontRequest(invalidationRequest); | ||
return cloudfront.createInvalidation(cloudFrontRequest).promise(); | ||
} | ||
|
||
export const handler: SNSHandler = async (event) => { | ||
const cloudfront = new AWS.CloudFront(); | ||
|
||
const distro = String(process.env.DISTRIBUTION_ID); | ||
const invalidationRequest = createInvalidationRequest( | ||
distro, | ||
event.Records[0].Sns | ||
); | ||
} | ||
|
||
await runInvalidation(cloudfront, invalidationRequest); | ||
}; | ||
const command = new CreateInvalidationCommand(createInvalidationCommandInput); | ||
await cloudFrontClient.send(command); | ||
}; |
Oops, something went wrong.