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

DO-1545: Prerender Fargate Readme update #1119

Merged
merged 5 commits into from
Nov 10, 2023
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
157 changes: 139 additions & 18 deletions packages/prerender-fargate/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,139 @@
# Prerender in Fargate

A construct to host [Prerender](https://github.com/prerender/prerender) in Fargate.

## Props

- `prerenderName`: Name of the Prerender service
- `domainName`: Domain name for Prerender
- `vpcId`: VPC to host Prerender in
- `bucketName`: Optional S3 bucket name
- `expirationDays`: Optional days until items expire in bucket (default to 7 days)
- `tokenList`: List of tokens to accept as authentication
- `certificateArn`: Certificate arn to match the domain
- `desiredInstanceCount`: Number of Prerender instances to run (default 1)
- `maxInstanceCount`: Maximum number of Prerender instances to run (default 2)
- `instanceCPU`: CPU to allocate to each instance (default 512)
- `instanceMemory`: Amount of memory to allocate to each instance (default 1024)
- `enableRedirectCache`: Cache 301 and 302 responses, too (default false)
# PrerenderFargate Construct

The PrerenderFargate construct sets up an AWS Fargate service to run a [Prerender] service in an ECS Fargate cluster.

TheOrangePuff marked this conversation as resolved.
Show resolved Hide resolved
The Prerender server listens for an HTTP request, takes the URL, and loads it in Headless Chrome, waits for the page to finish loading, and then returns your content to the requesting client.

## AWS Resources Created/Configured by this Construct

- **S3 Bucket:** For storing prerendered web pages.
- **Fargate Service:** For running the Prerender service.
- **ECR Asset:** For managing the Docker image of the Prerender service.
- **VPC & VPC Endpoints:** For network configuration and enabling direct access to S3.
- **Recache API:** (optional) To trigger recaching of URLs.

## Usage and PrerenderFargateOptions

To use the PrerenderFargate construct, you can instantiate it with suitable PrerenderFargateOptions and place it within a CDK stack. The PrerenderOptions parameter allows the developer to customize various aspects of the Prerender service.

### `prerenderName` (string)

- The name of the Prerender service.

### `domainName` (string)

- The domain name to prerender.

### `vpcId` (string, optional)

- The ID of the VPC to deploy the Fargate service in.

### `bucketName` (string, optional)

- The name of the S3 bucket to store prerendered pages in.

### `expirationDays` (number, optional)

- The number of days to keep prerendered pages in the S3 bucket before expiring them.

### `tokenList` (Array of strings, deprecated)

- A list of tokens to use for authentication with the Prerender service. (This parameter is deprecated and will be removed in a future release. Please use the `tokenUrlAssociation` parameter instead. If `tokenUrlAssociation` is provided, `tokenList` will be ignored.)

### `certificateArn` (string)

- The ARN of the SSL certificate to use for HTTPS connections.

### `desiredInstanceCount` (number, optional)

- The desired number of Fargate instances to run.

### `maxInstanceCount` (number, optional)

- The maximum number of Fargate instances to run.

### `minInstanceCount` (number, optional)

- The minimum number of Fargate instances to run.

### `instanceCPU` (number, optional)

- The amount of CPU to allocate to each Fargate instance.

### `instanceMemory` (number, optional)

- The amount of memory to allocate to each Fargate instance.

### `enableRedirectCache` (string, optional)

- Whether to enable caching of HTTP redirects.

### `enableS3Endpoint` (boolean, optional)

- Whether to enable the [VPC endpoint](https://docs.aws.amazon.com/vpc/latest/privatelink/create-interface-endpoint.html) for S3.

### `tokenUrlAssociation` (PrerenderTokenUrlAssociationOptions, optional)

- Configuration for associating tokens with specific domain URLs. During the recaching process, these tokens will be used to validate the request.

TheOrangePuff marked this conversation as resolved.
Show resolved Hide resolved
### `prerenderFargateScalingOptions` (PrerenderFargateScalingOptions, optional)

- This allows to alter the scaling behavior. The default configuration should be sufficient for most of the cases.

### `prerenderFargateRecachingOptions` (PrerenderFargateRecachingOptions, optional)

- This allows to alter the re-caching behavior. The default configuration should be sufficient.

## Example

Here's an example of how to use the PrerenderFargate construct in a TypeScript CDK application:

```typescript
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { PrerenderFargate, PrerenderFargateOptions } from "@aligent/cdk-prerender-fargate";


export class RagPrerenderStackStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);

new PrerenderFargate(this, "PrerenderService", {
prerenderName: "myPrerender",
bucketName: "myPrerenderBucket",
expirationDays: 7,
vpcId: "vpc-xxxxxxxx",
desiredInstanceCount: 1,
instanceCPU: 512,
instanceMemory: 1024,
domainName: "prerender.mydomain.com",
certificateArn:
"arn:aws:acm:region:account:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
enableRedirectCache: "false",
maxInstanceCount: 2,
enableS3Endpoint: true,
tokenUrlAssociation: {
token1: ["https://example.com", "https://acme.example.com"],
token2: ["https://example1.com", "https:acme.example1.com"],
},
ssmPathPrefix: "/prerender/recache/tokens",
prerenderFargateRecachingOptions: {
maxConcurrentExecutions: 1,
},
prerenderFargateScalingOptions: {
healthCheckGracePeriod: 20,
healthCheckInterval: 30,
unhealthyThresholdCount: 3,
scaleInCooldown: 120,
scaleOutCooldown: 60,
},
});
}
}
```

## Acknowledgements

- [prerender.io](https://prerender.io/) - The Prerender service.

[Prerender]:https://github.com/prerender/prerender
10 changes: 10 additions & 0 deletions packages/prerender-fargate/lib/prerender-fargate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PrerenderFargateOptions {
domainName: string;
/**
* The ID of the VPC to deploy the Fargate service in.
* @default - The default VPC will be used
*/
vpcId?: string;
/**
Expand All @@ -22,6 +23,7 @@ export interface PrerenderFargateOptions {
bucketName?: string;
/**
* The number of days to keep prerendered pages in the S3 bucket before expiring them.
* @default - 10 days
*/
expirationDays?: number;
/**
Expand All @@ -37,30 +39,37 @@ export interface PrerenderFargateOptions {
certificateArn: string;
/**
* The minimum number of Fargate instances to run.
* @default - 1
*/
minInstanceCount?: number;
/**
* The desired number of Fargate instances to run.
* @default - 1
*/
desiredInstanceCount?: number;
/**
* The maximum number of Fargate instances to run.
* @default - 1
*/
maxInstanceCount?: number;
/**
* The amount of CPU to allocate to each Fargate instance.
* @default - 0.5 vCPU
*/
instanceCPU?: number;
/**
* The amount of memory to allocate to each Fargate instance.
* @default - 512MB
*/
instanceMemory?: number;
/**
* Whether to enable caching of HTTP redirects.
* @default - false
*/
enableRedirectCache?: string;
/**
* Whether to enable the S3 endpoint for the VPC.
* @default - false
*/
enableS3Endpoint?: boolean;
/**
Expand Down Expand Up @@ -91,6 +100,7 @@ export interface PrerenderFargateOptions {
/**
* Prerender Fargate Re-caching options
* This allows to alter the re-caching behavior. The default configuration should be sufficient.
* @default - { maxConcurrentExecutions: 1 }
*/
prerenderFargateRecachingOptions?: PrerenderFargateRecachingOptions;
}
Expand Down
Loading