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

Server-side website: Add the requestFunction to the extensions #403

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 5 additions & 4 deletions docs/server-side-website.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,11 @@ constructs:

### Available extensions

| Extension key | CloudFormation resource | CloudFormation documentation |
|--------------- |------------------------------- |------------------------------------------------------------------------------------------------------------------------ |
| distribution | AWS::CloudFront::Distribution | [Link](https://docs.aws.amazon.com/fr_fr/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-distribution.html) |
| bucket | AWS::S3::Bucket | [Link](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html) |
| Extension key | CloudFormation resource | CloudFormation documentation |
|-----------------|-------------------------------|------------------------------------------------------------------------------------------------------------------|
| distribution | AWS::CloudFront::Distribution | [Link](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-distribution.html) |
| bucket | AWS::S3::Bucket | [Link](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html) |
| requestFunction | AWS::CloudFront::Function | [Link](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-function.html) |

### More options

Expand Down
6 changes: 5 additions & 1 deletion src/constructs/aws/ServerSideWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class ServerSideWebsite extends AwsConstruct {

private readonly distribution: Distribution;
private readonly bucket: Bucket;
private readonly requestFunction: cloudfront.Function;
private readonly domains: string[] | undefined;
private readonly bucketNameOutput: CfnOutput;
private readonly domainOutput: CfnOutput;
Expand Down Expand Up @@ -131,6 +132,8 @@ export class ServerSideWebsite extends AwsConstruct {
// Hide the stage in the URL in REST scenario
const originPath = configuration.apiGateway === "rest" ? "/" + (provider.getStage() ?? "") : undefined;

this.requestFunction = this.createRequestFunction();

this.distribution = new Distribution(this, "CDN", {
comment: `${provider.stackName} ${id} website CDN`,
defaultBehavior: {
Expand All @@ -149,7 +152,7 @@ export class ServerSideWebsite extends AwsConstruct {
originRequestPolicy: backendOriginPolicy,
functionAssociations: [
{
function: this.createRequestFunction(),
function: this.requestFunction,
eventType: FunctionEventType.VIEWER_REQUEST,
},
],
Expand Down Expand Up @@ -208,6 +211,7 @@ export class ServerSideWebsite extends AwsConstruct {
return {
distribution: this.distribution.node.defaultChild as CfnDistribution,
bucket: this.bucket.node.defaultChild as CfnBucket,
requestFunction: this.requestFunction.node.defaultChild as cloudfront.CfnFunction,
};
}

Expand Down
8 changes: 8 additions & 0 deletions test/unit/serverSideWebsite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ describe("server-side website", () => {
ObjectLockEnabled: true,
},
},
requestFunction: {
Properties: {
FunctionCode: "function handler(event) { return event; }",
},
},
},
},
},
Expand All @@ -591,6 +596,9 @@ describe("server-side website", () => {
expect(cfTemplate.Resources[computeLogicalId("backend", "Assets")].Properties).toMatchObject({
ObjectLockEnabled: true,
});
expect(cfTemplate.Resources[computeLogicalId("backend", "RequestFunction")].Properties).toMatchObject({
FunctionCode: "function handler(event) { return event; }",
});
});

it("trims CloudFront function names to stay under the limit", async () => {
Expand Down
Loading