From afecc0084de2b1dfc31eadc0a0c841156eb1b16d Mon Sep 17 00:00:00 2001 From: Sean Holung Date: Thu, 16 Jan 2025 08:26:24 -0800 Subject: [PATCH] remove redirects for ai answers --- .../cloudfrontLambdaAssociations.ts | 55 ------------------- infrastructure/index.ts | 7 +-- 2 files changed, 2 insertions(+), 60 deletions(-) diff --git a/infrastructure/cloudfrontLambdaAssociations.ts b/infrastructure/cloudfrontLambdaAssociations.ts index a651eb382d97..9a101e0de3e0 100644 --- a/infrastructure/cloudfrontLambdaAssociations.ts +++ b/infrastructure/cloudfrontLambdaAssociations.ts @@ -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. @@ -31,24 +30,6 @@ export function getEdgeRedirectAssociation(): aws.types.input.cloudfront.Distrib }; } -export async function getAnswersEdgeRedirectAssociation(websiteDomain: string): Promise { - 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(), @@ -121,42 +102,6 @@ function getAIAnswersRewritesLambdaCallback(): aws.lambda.Callback): aws.lambda.Callback { - - 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); } diff --git a/infrastructure/index.ts b/infrastructure/index.ts index ede31af860ad..86c7f08b15c3 100644 --- a/infrastructure/index.ts +++ b/infrastructure/index.ts @@ -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(); @@ -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. },