Skip to content

Commit 8195af0

Browse files
authored
feat(url-shortener): use a Lambda function URL (#331)
1 parent 9bcbacd commit 8195af0

13 files changed

+1476
-1501
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.gitignore

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/tasks.json

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

package.json

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

src/url-shortener/index.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,31 @@ export class UrlShortener extends Construct {
123123
});
124124

125125
// Redirect function
126-
const redirectFunction = new RedirectFunction(this, 'Redirect');
126+
const redirectFunction = new RedirectFunction(this, 'Redirect', {
127+
environment: {
128+
BUCKET_NAME: bucket.bucketName,
129+
},
130+
});
127131
bucket.grantRead(redirectFunction);
128132

129133
const distribution = new cloudfront.Distribution(this, 'Distribution', {
130134
defaultBehavior: {
131-
origin: origins.S3BucketOrigin.withOriginAccessControl(bucket),
132-
edgeLambdas: [
133-
{
134-
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
135-
functionVersion: redirectFunction,
136-
},
137-
],
135+
origin: origins.FunctionUrlOrigin.withOriginAccessControl(redirectFunction.addFunctionUrl()),
138136
},
139137
certificate: props.certificate,
140138
domainNames: [domainName],
141139
httpVersion: cloudfront.HttpVersion.HTTP2_AND_3,
142140
});
143141

144142
// Route53 records
145-
new route53.ARecord(this, 'ARecord', {
143+
const aliasProps = {
146144
zone: props.hostedZone,
147145
target: route53.RecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)),
148146
recordName: props.recordName,
149-
});
150-
new route53.AaaaRecord(this, 'AaaaRecord', {
151-
zone: props.hostedZone,
152-
target: route53.RecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)),
153-
recordName: props.recordName,
154-
});
147+
};
148+
new route53.ARecord(this, 'ARecord', aliasProps);
149+
new route53.AaaaRecord(this, 'AaaaRecord', aliasProps);
150+
new route53.HttpsRecord(this, 'HttpsRecord', aliasProps);
155151

156152
// Lambda function to increment counter and write redirect in bucket
157153
const shortenerFunction = new ShortenerFunction(this, 'Shortener', {

src/url-shortener/redirect-function.ts

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

src/url-shortener/redirect.edge-lambda.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { GetObjectCommand, S3Client } from '@aws-sdk/client-s3';
2+
3+
const s3Client = new S3Client({});
4+
5+
export async function handler(event: AWSLambda.LambdaFunctionURLEvent): Promise<AWSLambda.LambdaFunctionURLResult> {
6+
try {
7+
const key = event.rawPath.substring(1); // remove first slash
8+
9+
const data = await s3Client.send(new GetObjectCommand({
10+
Bucket: process.env.BUCKET_NAME,
11+
Key: key,
12+
}));
13+
14+
if (!data.Body) {
15+
throw new Error('No body');
16+
}
17+
18+
const redirect = JSON.parse(await data.Body.transformToString());
19+
20+
if (!redirect.url) {
21+
throw new Error('Redirect object in S3 is missing a `url` property.');
22+
}
23+
24+
return {
25+
statusCode: 301,
26+
headers: {
27+
Location: redirect.url,
28+
},
29+
};
30+
} catch (err) {
31+
console.log(err);
32+
return { statusCode: 404 };
33+
}
34+
}

0 commit comments

Comments
 (0)