Skip to content

Commit

Permalink
remove redirects for ai answers
Browse files Browse the repository at this point in the history
  • Loading branch information
sean1588 committed Jan 16, 2025
1 parent 66b91f3 commit afecc00
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 60 deletions.
55 changes: 0 additions & 55 deletions infrastructure/cloudfrontLambdaAssociations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "aws-lambda";
import * as URLPattern from "url-pattern";
import { LambdaEdge } from "./lambdaEdge";
import axios from "axios";
import { getResources } from "@pulumi/aws/resourcegroupstaggingapi/getResources";

// Edge functions must be defined in us-east-1.
Expand All @@ -31,24 +30,6 @@ export function getEdgeRedirectAssociation(): aws.types.input.cloudfront.Distrib
};
}

export async function getAnswersEdgeRedirectAssociation(websiteDomain: string): Promise<aws.types.input.cloudfront.DistributionDefaultCacheBehaviorLambdaFunctionAssociation> {
const response = await axios.get(`https://${websiteDomain}/answers/redirects.json`);
if (response.status !== 200) {
throw new pulumi.RunError(`Failed to fetch answers redirects: HTTP ${response.status}`);
}
const redirects = response.data;
const edgeRedirectsLambda = new LambdaEdge("redirects-answers", {
func: getAnswersRedirectsLambdaCallback(redirects),
funcDescription: "Lambda function that conditionally redirects based on a path-matching expression.",
}, { provider: usEast1Provider });
return {
includeBody: false,
lambdaArn: edgeRedirectsLambda.getLambdaEdgeArn(),
eventType: "origin-request",
};

}

export function getAIAnswersRewriteAssociation(): aws.types.input.cloudfront.DistributionDefaultCacheBehaviorLambdaFunctionAssociation {
const aiAnswersRewritesLambda = new LambdaEdge("answers-rewrites", {
func: getAIAnswersRewritesLambdaCallback(),
Expand Down Expand Up @@ -121,42 +102,6 @@ function getAIAnswersRewritesLambdaCallback(): aws.lambda.Callback<CloudFrontReq
};
}

function getAnswersRedirectsLambdaCallback(redirects: Record<string, string>): aws.lambda.Callback<CloudFrontRequestEvent, CloudFrontRequest | CloudFrontResponse> {

return (event: CloudFrontRequestEvent, context, callback) => {
const request = event.Records[0].cf.request;
// Check for a redirect that matches the request URL.
const redirect = redirects[request.uri];

// If there isn't one, just return with the original request.
if (!redirect) {
callback(null, request);
return;
}

// Return with a redirect.
const modifiedResponse = {
status: "301",
statusDescription: "Moved Permanently",
headers: {
"location": [
{
key: "Location",
value: redirect,
},
],
"cache-control": [
{
key: "Cache-Control",
value: "max-age=1800", /* half hour in seconds */
},
],
},
};
callback(null, modifiedResponse);
};
}

function getRedirect(uri: string): string | undefined {
return getRegistryRedirect(uri) || getSDKRedirect(uri) || getResourcesRedirect(uri);
}
Expand Down
7 changes: 2 additions & 5 deletions infrastructure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as fs from "fs";

import { getAIAnswersRewriteAssociation, getAnswersEdgeRedirectAssociation, getEdgeRedirectAssociation } from "./cloudfrontLambdaAssociations";
import { getAIAnswersRewriteAssociation, getEdgeRedirectAssociation } from "./cloudfrontLambdaAssociations";

const stackConfig = new pulumi.Config();

Expand Down Expand Up @@ -650,10 +650,7 @@ const distributionArgs: aws.cloudfront.DistributionArgs = {
pathPattern: '/ai/*',
originRequestPolicyId: allViewerExceptHostHeaderId,
cachePolicyId: cachingDisabledId,
lambdaFunctionAssociations: [
...config.doAIAnswersRewrites ? [getAIAnswersRewriteAssociation()] : [],
getAnswersEdgeRedirectAssociation(config.websiteDomain),
],
lambdaFunctionAssociations: config.doAIAnswersRewrites ? [getAIAnswersRewriteAssociation()] : [],
forwardedValues: undefined, // forwardedValues conflicts with cachePolicyId, so we unset it.
},

Expand Down

0 comments on commit afecc00

Please sign in to comment.