Skip to content

Commit ae5acf5

Browse files
authored
Linkry: read on redirect from DynamoDB instead of cloudfront KV store (#341)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a Lambda@Edge-based redirect handler for faster, region-aware redirects. * Enabled DynamoDB streams for record change propagation. * **Refactor** * Removed CloudFront Key-Value Store usage and simplified create/delete flows. * Consolidated replication regions to a single region to streamline deployment. * Switched CloudFront redirect logic to use an edge Lambda association. * **Chores** * Updated infra variables/inputs and removed obsolete KV-related configuration. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 97e5761 commit ae5acf5

File tree

18 files changed

+231
-393
lines changed

18 files changed

+231
-393
lines changed

src/api/functions/cloudfrontKvStore.ts

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/api/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"prettier:write": "prettier --write *.ts **/*.ts"
1616
},
1717
"dependencies": {
18-
"@aws-sdk/client-cloudfront-keyvaluestore": "^3.895.0",
1918
"@aws-sdk/client-dynamodb": "^3.895.0",
2019
"@aws-sdk/client-lambda": "^3.895.0",
2120
"@aws-sdk/client-secrets-manager": "^3.895.0",

src/api/routes/linkry.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@ import {
1010
UnauthorizedError,
1111
ValidationError,
1212
} from "../../common/errors/index.js";
13-
import { NoDataRequest } from "../types.js";
1413
import {
1514
QueryCommand,
1615
TransactWriteItemsCommand,
1716
TransactWriteItem,
1817
TransactionCanceledException,
1918
} from "@aws-sdk/client-dynamodb";
20-
import { CloudFrontKeyValueStoreClient } from "@aws-sdk/client-cloudfront-keyvaluestore";
2119
import { genericConfig } from "../../common/config.js";
2220
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
2321
import rateLimiter from "api/plugins/rateLimiter.js";
24-
import {
25-
deleteKey,
26-
getLinkryKvArn,
27-
setKey,
28-
} from "api/functions/cloudfrontKvStore.js";
2922
import { createRequest, linkrySlug } from "common/types/linkry.js";
3023
import {
3124
extractUniqueSlugs,
@@ -191,12 +184,6 @@ const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
191184
message: `Slug ${request.body.slug} is reserved by the system.`,
192185
});
193186
}
194-
195-
if (!fastify.cloudfrontKvClient) {
196-
fastify.cloudfrontKvClient = new CloudFrontKeyValueStoreClient({
197-
region: genericConfig.AwsRegion,
198-
});
199-
}
200187
},
201188
onRequest: fastify.authorizeFromSchema,
202189
},
@@ -423,24 +410,6 @@ const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
423410
message: "Failed to save data to DynamoDB.",
424411
});
425412
}
426-
// Add to cloudfront key value store so that redirects happen at the edge
427-
const kvArn = await getLinkryKvArn(fastify.runEnvironment);
428-
try {
429-
await setKey({
430-
key: request.body.slug,
431-
value: request.body.redirect,
432-
kvsClient: fastify.cloudfrontKvClient,
433-
arn: kvArn,
434-
});
435-
} catch (e) {
436-
fastify.log.error(e);
437-
if (e instanceof BaseError) {
438-
throw e;
439-
}
440-
throw new DatabaseInsertError({
441-
message: "Failed to save redirect to Cloudfront KV store.",
442-
});
443-
}
444413
await createAuditLogEntry({
445414
dynamoClient: fastify.dynamoClient,
446415
entry: {
@@ -517,15 +486,7 @@ const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
517486
}),
518487
}),
519488
),
520-
onRequest: async (request, reply) => {
521-
await fastify.authorizeFromSchema(request, reply);
522-
523-
if (!fastify.cloudfrontKvClient) {
524-
fastify.cloudfrontKvClient = new CloudFrontKeyValueStoreClient({
525-
region: genericConfig.AwsRegion,
526-
});
527-
}
528-
},
489+
onRequest: fastify.authorizeFromSchema,
529490
},
530491
async (request, reply) => {
531492
const { slug } = request.params;
@@ -614,22 +575,6 @@ const linkryRoutes: FastifyPluginAsync = async (fastify, _options) => {
614575
message: "Failed to delete data from DynamoDB.",
615576
});
616577
}
617-
const kvArn = await getLinkryKvArn(fastify.runEnvironment);
618-
try {
619-
await deleteKey({
620-
key: slug,
621-
kvsClient: fastify.cloudfrontKvClient,
622-
arn: kvArn,
623-
});
624-
} catch (e) {
625-
fastify.log.error(e);
626-
if (e instanceof BaseError) {
627-
throw e;
628-
}
629-
throw new DatabaseDeleteError({
630-
message: "Failed to delete redirect at Cloudfront KV store.",
631-
});
632-
}
633578
await createAuditLogEntry({
634579
dynamoClient: fastify.dynamoClient,
635580
entry: {

src/api/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import NodeCache from "node-cache";
77
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
88
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
99
import { SQSClient } from "@aws-sdk/client-sqs";
10-
import { CloudFrontKeyValueStoreClient } from "@aws-sdk/client-cloudfront-keyvaluestore";
1110
import { AvailableAuthorizationPolicy } from "common/policies/definition.js";
1211
import type RedisModule from "ioredis";
1312
export type Redis = RedisModule.default;
@@ -45,7 +44,6 @@ declare module "fastify" {
4544
sqsClient?: SQSClient;
4645
redisClient: Redis;
4746
secretsManagerClient: SecretsManagerClient;
48-
cloudfrontKvClient: CloudFrontKeyValueStoreClient;
4947
secretConfig: SecretConfig | (SecretConfig & SecretTesting);
5048
refreshSecretConfig: CallableFunction;
5149
}

src/common/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export type ConfigType = {
2525
PaidMemberGroupId: string;
2626
PaidMemberPriceId: string;
2727
AadValidReadOnlyClientId: string;
28-
LinkryCloudfrontKvArn?: string;
2928
ConfigurationSecretIds: string[];
3029
DiscordGuildId: string;
3130
GroupSuffix: string;
@@ -136,7 +135,6 @@ const environmentConfig: EnvironmentConfigType = {
136135
PaidMemberGroupId: "9222451f-b354-4e64-ba28-c0f367a277c2",
137136
PaidMemberPriceId: "price_1S5eAqDGHrJxx3mKZYGoulj3",
138137
AadValidReadOnlyClientId: "2c6a0057-5acc-496c-a4e5-4adbf88387ba",
139-
LinkryCloudfrontKvArn: "arn:aws:cloudfront::427040638965:key-value-store/0c2c02fd-7c47-4029-975d-bc5d0376bba1",
140138
DiscordGuildId: "1278798685706391664",
141139
EntraServicePrincipalId: "8c26ff11-fb86-42f2-858b-9011c9f0708d",
142140
GroupSuffix: "[NonProd]",

0 commit comments

Comments
 (0)