Skip to content

Commit 0260bc5

Browse files
committed
Add CloudFront invalidation support and update dependencies
- Introduced @aws-sdk/client-cloudfront to handle CloudFront invalidations. - Updated package.json and package-lock.json to include the new dependency. - Modified serverless.yml to add environment variable for CloudFront distribution ID. - Enhanced syncMap function to create CloudFront invalidations after syncing map data.
1 parent f622593 commit 0260bc5

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

backend/package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"author": "Julian Boilen",
2121
"license": "ISC",
2222
"devDependencies": {
23+
"@aws-sdk/client-cloudfront": "^3.741.0",
2324
"@aws-sdk/client-s3": "^3.741.0",
2425
"@hey-api/openapi-ts": "^0.64.1",
2526
"@sparticuz/chromium": "^132.0.0",

backend/serverless.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ provider:
1818
Action:
1919
- s3:*
2020
Resource: "*"
21+
- Effect: Allow
22+
Action:
23+
- cloudfront:CreateInvalidation
24+
Resource: "*"
2125
environment:
2226
STAGE: ${sls:stage}
2327
DB_HOST: ${ssm:/${self:service}-${sls:stage}-db-host}
@@ -37,6 +41,7 @@ provider:
3741
OPENAI_SK: ${ssm:/${self:service}-${sls:stage}-openai-sk}
3842
RAPIDAPI_API_KEY: ${ssm:/${self:service}-${sls:stage}-rapidapi-api-key}
3943
PRINTFUL_SK: ${ssm:/${self:service}-${sls:stage}-printful-sk}
44+
MAPTILES_CLOUDFRONT_DISTRIBUTION_ID: ${ssm:/${self:service}-${sls:stage}-maptiles-cloudfront-distribution-id}
4045
plugins:
4146
- serverless-plugin-conditional-functions
4247
- serverless-webpack

backend/src/cron/syncMapSelfHosted.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CloudFrontClient, CreateInvalidationCommand } from '@aws-sdk/client-cloudfront';
12
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
23
import { createReadStream } from 'fs';
34
import { getRepository } from 'typeorm';
@@ -7,6 +8,7 @@ import EffectiveAddress from '../entities/EffectiveAddress';
78
import EffectiveGeocode from '../entities/EffectiveGeocode';
89

910
const s3 = new S3Client();
11+
const cloudFront = new CloudFrontClient();
1012

1113
export default async function syncMap(): Promise<void> {
1214
console.log('Refreshing effective addresses...');
@@ -46,5 +48,25 @@ export default async function syncMap(): Promise<void> {
4648
})
4749
);
4850

51+
console.log('Creating CloudFront invalidation...');
52+
const distributionId = process.env.MAPTILES_CLOUDFRONT_DISTRIBUTION_ID;
53+
if (distributionId) {
54+
await cloudFront.send(
55+
new CreateInvalidationCommand({
56+
DistributionId: distributionId,
57+
InvalidationBatch: {
58+
Paths: {
59+
Quantity: 1,
60+
Items: ['/photos-1940s.pmtiles'],
61+
},
62+
CallerReference: `sync-map-${Date.now()}`,
63+
},
64+
})
65+
);
66+
console.log('CloudFront invalidation created successfully.');
67+
} else {
68+
console.log('MAPTILES_CLOUDFRONT_DISTRIBUTION_ID not set, skipping invalidation.');
69+
}
70+
4971
console.log('Sync of map data complete.');
5072
}

0 commit comments

Comments
 (0)