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

Feature/do 1357 cdk v2 templating #1395

Merged
merged 10 commits into from
Sep 10, 2024
10 changes: 5 additions & 5 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/feature-env-handlers/lib/viewer-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const handler = async (
): Promise<CloudFrontRequest> => {
const { request } = event.Records[0].cf;

// Consumed by OriginRequest Lambda@Edge for Feature Environment functionality.
request.headers["x-forwarded-host"] = [
{
value: request.headers.host[0].value,
Expand Down
4 changes: 2 additions & 2 deletions packages/feature-env-handlers/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/feature-env-handlers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aligent/cdk-feature-env-handlers",
"version": "0.1.0",
"version": "2.0.1",
"description": "Cloudfront Lambda@Edge handlers to allow feature environments to function",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions packages/lambda-at-edge-handlers/lib/prerender-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ export const handler = async (
if (
!IS_FILE.test(request.uri) &&
IS_BOT.test(request.headers["user-agent"][0].value) &&
// Check if the request is from Prerender service
!request.headers["x-prerender"]
) {
// Consumed by OriginRequest Lambda@Edge to determine if this request needs to be send to Prerender service rather than other origins.
request.headers["x-request-prerender"] = [
{
key: "x-request-prerender",
value: "true",
},
];

// Consumed by OriginRequest Lambda@Edge, only when x-request-prerender header is set. Prerender service will send request to this host.
request.headers["x-prerender-host"] = [
{ key: "X-Prerender-Host", value: request.headers.host[0].value },
];
Expand Down
4 changes: 2 additions & 2 deletions packages/lambda-at-edge-handlers/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/lambda-at-edge-handlers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aligent/cdk-lambda-at-edge-handlers",
"version": "0.1.0",
"version": "0.1.1",
"description": "A Cloudfront Lambda@Edge handlers powered by Middy",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/static-hosting/lib/path-remap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class PathRemapFunction extends Construct {
local: new Esbuild({
entryPoints: [join(__dirname, "handlers/remap.ts")],
define: {
"process.env.REMAP_PATH": options.path,
"process.env.REMAP_PATH": `"${options.path}"`,
},
}),
},
Expand Down
69 changes: 46 additions & 23 deletions packages/static-hosting/lib/static-hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import { Construct } from "constructs";
import { CfnOutput, Duration, RemovalPolicy } from "aws-cdk-lib";
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
import {
BehaviorOptions,
CacheHeaderBehavior,
CachePolicy,
CfnDistribution,
Distribution,
DistributionProps,
EdgeLambda,
ErrorResponse,
HttpVersion,
IDistribution,
IResponseHeadersPolicy,
IOriginAccessIdentity,
LambdaEdgeEventType,
OriginAccessIdentity,
OriginRequestHeaderBehavior,
OriginRequestPolicy,
PriceClass,
ResponseHeadersPolicy,
SecurityPolicyProtocol,
SSLMethod,
ViewerProtocolPolicy,
BehaviorOptions,
ErrorResponse,
EdgeLambda,
CfnDistribution,
OriginRequestPolicy,
CachePolicy,
OriginRequestHeaderBehavior,
CacheHeaderBehavior,
IResponseHeadersPolicy,
LambdaEdgeEventType,
OriginAccessIdentity,
IDistribution,
IOriginAccessIdentity,
} from "aws-cdk-lib/aws-cloudfront";
import { HttpOrigin, S3Origin } from "aws-cdk-lib/aws-cloudfront-origins";
import {
Expand Down Expand Up @@ -246,6 +246,16 @@ export interface StaticHostingProps {
*/
defaultBehaviorCachePolicy?: CachePolicy;

/**
* Additional headers to include in OriginRequestHeaderBehavior
*/
additionalDefaultOriginRequestHeaders?: string[];

/**
* Additional headers to include in CacheHeaderBehavior
*/
additionalDefaultCacheKeyHeaders?: string[];

/**
* After switching constructs, you need to maintain the same logical ID
* for the underlying CfnDistribution if you wish to avoid the deletion
Expand Down Expand Up @@ -425,23 +435,36 @@ export class StaticHosting extends Construct {
});
let backendOrigin = undefined;

const additionalDefaultOriginRequestHeaders =
props.additionalDefaultOriginRequestHeaders || [];
const originRequestHeaderBehaviorAllowList = [
"x-forwarded-host", // Consumed by OriginRequest Lambda@Edge for Feature Environment functionality.
"x-request-prerender", // Consumed by OriginRequest Lambda@Edge to determine if this request needs to be send to Prerender service rather than other origins.
"x-prerender-host", // Consumed by OriginRequest Lambda@Edge, only when x-request-prerender header is set. Prerender service will send request to this host.
"x-prerender", // Consumed, if configured, by origin's custom features, such as GeoRedirection, the behave of which should depend on whether the request is from an end user.
"x-prerender-user-agent", // Consumed by Prerender service for logging original user agent rather than CloudFront's
...additionalDefaultOriginRequestHeaders,
];
TheOrangePuff marked this conversation as resolved.
Show resolved Hide resolved
const originRequestPolicy =
props.defaultBehaviorRequestPolicy ||
new OriginRequestPolicy(this, "S3OriginRequestPolicy", {
headerBehavior: OriginRequestHeaderBehavior.allowList(
"x-forwarded-host",
"x-request-prerender",
"x-prerender"
...originRequestHeaderBehaviorAllowList
),
});

const additionalDefaultCacheKeyHeaders =
props.additionalDefaultCacheKeyHeaders || [];
const cacheHeaderBehaviorAllowList = [
"x-forwarded-host", // Origin response may vary depending on the domain/path based on Feature Environment
"x-prerender", // Origin response may vary depending on whether the request is from end user or prerender service.
...additionalDefaultCacheKeyHeaders,
];
const originCachePolicy =
props.defaultBehaviorCachePolicy ||
new CachePolicy(this, "S3OriginCachePolicy", {
headerBehavior: CacheHeaderBehavior.allowList(
"x-forwarded-host",
"x-request-prerender",
"x-prerender"
...cacheHeaderBehaviorAllowList
),
enableAcceptEncodingBrotli: true,
enableAcceptEncodingGzip: true,
Expand Down Expand Up @@ -479,7 +502,7 @@ export class StaticHosting extends Construct {
origin: s3Origin,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: defaultBehaviorEdgeLambdas,
originRequestPolicy: originRequestPolicy,
originRequestPolicy,
cachePolicy: originCachePolicy,
responseHeadersPolicy: responseHeadersPolicy,
};
Expand Down Expand Up @@ -540,7 +563,7 @@ export class StaticHosting extends Construct {
}

const distributionProps: DistributionProps = {
domainNames: domainNames,
domainNames,
webAclId: props.webAclArn,
comment: props.comment,
defaultRootObject: defaultRootObject,
Expand All @@ -557,8 +580,8 @@ export class StaticHosting extends Construct {
"DomainCertificate",
props.certificateArn
),
defaultBehavior: defaultBehavior,
additionalBehaviors: additionalBehaviors,
defaultBehavior,
additionalBehaviors,
errorResponses: props.enableErrorConfig ? errorResponses : [],
};

Expand Down
2 changes: 1 addition & 1 deletion packages/static-hosting/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aligent/cdk-static-hosting",
"version": "2.3.4",
"version": "2.4.0",
"main": "index.js",
"license": "GPL-3.0-only",
"homepage": "https://github.com/aligent/aws-cdk-static-hosting-stack#readme",
Expand Down
Loading