Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(construct.awscdk.cloudfront-url-redirect): resolve cloudfront-js-v1 compatibility issues, use separate blog origin in web stack #91

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ export class HandlerLambdaCloudFrontFunction extends cloudfront.Function {
props: HandlerLambdaCloudFrontFunctionProps,
) {
const filePath = path.join(__dirname, '../dist/handler.function.mjs')
const content = fs
let content = fs
.readFileSync(filePath, 'utf8')
.replace('<FROM_HOSTNAME>', props.fromHostname)
.replace('<TO_HOSTNAME>', props.toHostname)
.replace('<REDIRECT_URI_PATTERN>', props.redirectUriPattern)
.replace('<TARGET_URI_PATTERN>', props.targetUriPattern)
const lines = content.split('\n')
// trim function export, which is not supported or expected by cloudfront js
content = lines.slice(0, -5).join('\n')
super(scope, id, {
comment: 'src/handler.lambda.ts',
...props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ const REDIRECT_URI_PATTERN = new RegExp('<REDIRECT_URI_PATTERN>', 'g')
const TO_HOSTNAME = '<TO_HOSTNAME>'
const TARGET_URI_PATTERN = '<TARGET_URI_PATTERN>'

/**
* cloudfront-js supports a limited subset of javascript/ecma features.
*/
// eslint-disable-next-line @typescript-eslint/require-await,@typescript-eslint/no-unused-vars
export async function handler(event: CloudFrontFunctionsEvent) {
const { request } = event
const { headers, uri } = request
function handler(event: CloudFrontFunctionsEvent) {
const request = event.request
const headers = request.headers
const uri = request.uri

const hostParams = headers.host
let host = ''
if (hostParams && hostParams.value) {
host = hostParams.value
}

const host = headers.host?.value
if (!host || host !== FROM_HOSTNAME) {
return request
}
Expand All @@ -25,3 +34,5 @@ export async function handler(event: CloudFrontFunctionsEvent) {
},
}
}

export default handler

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/stacks/web/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CloudFrontUrlRewrite } from '@crisiscleanup/construct.awscdk.cloudfront
import { Duration, Stack, type StackProps } from 'aws-cdk-lib'
import * as acm from 'aws-cdk-lib/aws-certificatemanager'
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'
import * as route53 from 'aws-cdk-lib/aws-route53'
import * as route53Targets from 'aws-cdk-lib/aws-route53-targets'
import type { Construct } from 'constructs'
Expand Down Expand Up @@ -114,12 +115,15 @@ export class CrisisCleanupWeb extends Stack {
}

if (props.enableBlogRedirect) {
const blogOrigin = new origins.HttpOrigin('blog.' + props.domainName)
new CloudFrontUrlRewrite(this, id + '-BlogRedirect', {
distribution: this.website.cloudFrontDistribution,
fromHostname: `blog.${props.domainName}`,
toHostname: props.fqdn,
redirectUriPattern: '^/d{4}/d{2}/(.*).html',
redirectUriPattern: '^/\\\\d{4}/\\\\d{2}/(.*)\\\\.html$',
targetUriPattern: '/blog/post/$1',
behaviorPath: '/*/*/*.html',
origin: blogOrigin,
})
if (this.zone) {
new route53.ARecord(this, id + '-alias-record-blog', {
Expand Down
Loading