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

feat: deploy / adapter / AWS Lambda #48

Draft
wants to merge 53 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
b2cf313
feat: first draft
aheissenberger Sep 25, 2024
4280adc
fix: reduce middleware to ssr, add urlParser to add expected url obje…
aheissenberger Sep 25, 2024
4d049d7
fix: add base to url
aheissenberger Sep 25, 2024
21965ff
fix: add package.json
aheissenberger Sep 25, 2024
7782c46
fix: request text/html
aheissenberger Sep 25, 2024
96bacb4
fix: revert to include all middlewares
aheissenberger Sep 25, 2024
6b1d1d3
fix: use enviroment variable `OUT_DIR` to set path from cwd to `.reac…
aheissenberger Sep 25, 2024
0efff6d
fix: enhance demo
aheissenberger Sep 25, 2024
7be2c1b
feat: add examples for aws-cdk, serverless framework, sst v3 (ion)
aheissenberger Sep 26, 2024
0968dc1
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Sep 26, 2024
ce803ea
fix: add sst ReactServer Stack
aheissenberger Sep 28, 2024
a862521
fix: docs
aheissenberger Sep 28, 2024
feeaa15
fix: sst env
aheissenberger Sep 29, 2024
c1fa103
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Sep 29, 2024
4901105
feat: add streaming support
aheissenberger Sep 29, 2024
c305eda
feat: add docs
aheissenberger Sep 29, 2024
7edf8d3
feat: detect aws deployment tool and initialize needed configuration …
aheissenberger Sep 29, 2024
bbb7c73
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Sep 29, 2024
09f2942
fix: remove node version
aheissenberger Sep 29, 2024
28419e0
fix: broken lockfile
aheissenberger Sep 29, 2024
955c9c3
fix: add error info
aheissenberger Sep 29, 2024
6f0c13a
fix: sst setup
aheissenberger Sep 29, 2024
7769469
fix: lockfile
aheissenberger Sep 29, 2024
b011546
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Dec 6, 2024
efb4503
fix: sst implementation without add anything to the .sst folder
aheissenberger Dec 6, 2024
c14dd85
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Dec 6, 2024
cf71f36
fix: show exception
aheissenberger Dec 6, 2024
ca97d80
fix: update packages
aheissenberger Dec 7, 2024
322ae62
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Dec 16, 2024
b580261
fix: pnpm-lock
aheissenberger Dec 16, 2024
d6617f3
fix: lockfile
aheissenberger Dec 16, 2024
b88a00e
fix: remove directory
aheissenberger Dec 16, 2024
2d08fcd
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Dec 17, 2024
8eebed1
fix: linting
aheissenberger Dec 17, 2024
e6234ba
fix: rsc.component handling on aws
aheissenberger Dec 17, 2024
647af57
fix: update packages
aheissenberger Dec 17, 2024
a8af95c
fix: update example app
aheissenberger Dec 17, 2024
5630a5b
fix: typescript errors
aheissenberger Dec 17, 2024
0e6d5c1
fix: change docs for sst
aheissenberger Dec 17, 2024
961966c
feat: add better toolkit detection, provide static pages to cdk, fix …
aheissenberger Dec 18, 2024
e4c8776
fix: update example
aheissenberger Dec 18, 2024
13b3f57
fix: update docs
aheissenberger Dec 18, 2024
f67212f
fix: update packages
aheissenberger Dec 18, 2024
4ca66fd
fix: caching
aheissenberger Dec 19, 2024
c8a2e63
fix: content type for RSC
aheissenberger Dec 19, 2024
d48d966
fix: docs
aheissenberger Dec 19, 2024
b062b31
Merge branch 'main' into feat/adapter/aws-lambda
aheissenberger Dec 19, 2024
99abb38
fix: text blocks
aheissenberger Dec 19, 2024
6f85d63
fix: show toolkit aware deploy command
aheissenberger Dec 19, 2024
ab08c87
feat: configure the cloudfront routing
aheissenberger Dec 19, 2024
e771738
fix: update example
aheissenberger Dec 19, 2024
f8987ae
fix: lockfile
aheissenberger Dec 19, 2024
b0e31be
feat: disable static file server in lambda function
aheissenberger Dec 20, 2024
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
288 changes: 288 additions & 0 deletions docs/src/pages/en/(pages)/deploy/aws.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
---
title: AWS
category: Deploy
order: 2
---

import Link from "../../../../components/Link.jsx";

# AWS

To deploy to AWS, use the `@lazarv/react-server-adapter-aws` package. This adapter is specifically designed to support the AWS cloud infrastructure and different tookits (AWS CDK, sst, serverless framework).

<Link name="installation">
### Installation
</Link>

Add the `@lazarv/react-server-adapter-aws` package to your project:

```sh
pnpm add -D @lazarv/react-server-adapter-aws
```

Add this to `.gitignore`:
`.aws-react-server`

Then you need to add the adapter to your `react-server.config.mjs` file:

```mjs
export default {
adapter: '@lazarv/react-server-adapter-aws',
};
```

**Streaming** can be activated by setting the property `streaming: true`:
```mjs
export default {
adapter: [ '@lazarv/react-server-adapter-aws', {streaming: true} ]
};
```
> **Note:** check if your deployment toolkit (e.g. AWS CDK) supports streaming!

AWS deployment toolkits:
* [SST V3 (ion)](#sst-v3-ion)
* [AWS CDK](#aws-cdk)
* [Serverless Framework V3](#serverless-framework-v3)

Each toolkit can optional support additional configuration properties:
```mjs
export default {
adapter: [ '@lazarv/react-server-adapter-aws', {streaming: true,
"toolkitDefault":"cdk",
"toolkit":{
"cdk": {
"stackName":"myStack"
},
"sst": {}
}
}]
};
```
When multiple configuration exist the property `"toolkitDefault" : "cdk",` is required!

<Link name="sst-v3-ion">
## [SST V3 (ion)](https://sst.dev)
</Link>

### Architecture

* AWS Cloudfront - globals CDN and reverse proxy for the framework and the static assets
- path `_server/` origin Lambda - no caching
- path `assets/` and `client/` are created by the framework, contain your assets and client code with a cache breaker filename - origin S3
- path any other root directory copied from `public` folder or created as a result of static pages - origin S3 (__CloudFront defaults to max. 10 behaviors which allow 8 additional root directories!__)
- any url not catched above - origin Lambda - no caching
* AWS Lambda URL Function - provides the react-server framework
* AWS S3 - static assets
* Resources from S3 have a `cache-control: public,max-age=0,s-maxage=86400,stale-while-revalidate=86400` header

> **Note:** Currently only deployment is supported, dev mode is not implemented.

<Link name="installation-sst">
### Installation
</Link>

1. First you need to initialize your project with sst. You can do this by running the following command:

```sh
pnpm add sst
pnpm sst init
```

```
> JS project detected. This will...
- use the JS template
- create an sst.config.ts

? Continue:
```

**Answers:** Yes

```
? Where do you want to deploy your app? You can change this later:
```

**Answers:** aws

2. Create the `sst-react-server.ts` sst stack to your project and add the stack to `sst.config.ts`:

```sh
pnpm build
```

**Optional manual `sst.config.ts` setup:**

1. add `import { ReactServer } from "./sst-react-server";`
2. replace `async run() {},`with:
```mjs
async run() {
new sst.aws.ReactServer("ReactServertackDemoApp");
},
```

<Link name="configuration-sst">
## Configuration
</Link>

All possible configuration options are documented here:
`./sst-react-server.ts`

<Link name="deploy-sst">
### Deploy
</Link>

You can build and deploy your application to AWS using the following command:

```sh
pnpm sst deploy
```

> **Error:**
> `TypeError: aws_exports.ReactServer is not a constructor`
>
> run `pnpm build` again to fix missing ReactServer stack to sst.

**remove stack:**
```sh
pnpm sst remove
```

<Link name="aws-cdk">
## [AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html)
</Link>

> **Note:** Streaming is not supported.

### Architecture
* AWS Cloudfront - globals CDN and reverse proxy for the framework and the static assets
* AWS API Gateway - used by AWS Lambda function
* AWS Lambda Function - provides the react-server framework
* AWS S3 Bucket - browser client code and static assets with cache breakers
* AWS S3 Bucket - static assets from public folder and static pages

Bei default, all static routes to existing files are uploaded to a Cloudfront KV store and used to directly redirect requests to static items on edge to the s3 bucket.
As there is a 5MB Limit on the KV store size and only 512Byte for the keys (max. length of url) you can use the property `staticRoutes` to define a list of root directories which are directly handled by cloudfront without the need a lookup in the KV store.

<Link name="installation-cdk">
### Installation
</Link>

First you need to install this packages:

```sh
pnpm add aws-cdk-lib constructs source-map-support
pnpm add -D aws-cdk tsx
```

add to `.gitignore`:

```.gitignore
cdk.out
```

Third, add the `ReactServer()` cdk stack `cdk/lib/react-server-stack.ts`:

```sh
touch cdk.json
pnpm build
```
> **Note:** will only add a sample configuration when `cdk.json` is empty.

Optional properties of the stack `react-server.config`:
```json
{
"adapter": [
"@lazarv/react-server-adapter-aws",
{
"streaming": false,
"toolkit": {"cdk":{
"stackName": "ReactStackStack-001",
"cdkFolderPath": "cdk", // default: './cdk'
"stackProps": {
"description": "Hello World Stack"
"domainName": "example.com",
"subDomain": "www",
"certificate": "arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012" //or a certificatemanager.ICertificate
"hostedZone": route53.HostedZone.fromLookup(stack, "MyHostedZone", { domainName: "example.com" })
"staticRoutes": ["/assets/","/client/"], // (optional) this catch all routes are created as caching behaviors on cloudfront and point to the static assets on S3. Alle Routes need to start with a "/"!
"maxBehaviors": 10, // (optional) defaults to the AWS limit for Cloudfront behaviors
env: {
account: '123456789012',
region: 'us-east-1'
},
}
}}
}
]
}
```

<Link name="deploy-cdk">
### Deploy
</Link>

Build the project and deploy with the command from the cdk toolkit.

```sh
pnpm build
pnpm cdk deploy --all
```

**remove stack:**

```sh
pnpm cdk destroy --all
```

<Link name="serverless-framework-v3">
## Serverless Framework V3
</Link>

> **Note:** Streaming is not supported and there are no plans to support Version 4.x.

### Architecture
* AWS Cloudfront - globals CDN and reverse proxy for the framework and the static assets
* AWS API Gateway - used by AWS Lambda function
* AWS Lambda Function - provides the react-server framework
* AWS S3 - static assets

add the following packages:
```sh
pnpm add -D serverless@3 serverless-cloudfront-invalidate serverless-s3-sync
```

add to `.gitignore`:

```.gitignore
.serverless
```

run this commands to create a sample configuration:

```sh
touch serverless.yml
pnpm build
```

> **Note:** will only add a sample configuration when `serverless.yml` is empty.

<Link name="deploy-sls">
### Deploy
</Link>

Build the project and deploy with the command from the sls toolkit.

```sh
pnpm build
pnpm sls deploy
```

get FrontendCloudFrontDistributionUrl:
`pnpm sls info --verbose`

Configuration of the stack is possible in `serverless.yml`.

**remove stack:**
```sh
pnpm sls remove
```
14 changes: 14 additions & 0 deletions examples/hello-world-aws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# react-server adapter aws
.aws-react-server
# aws-cdk
.cdk.staging
cdk.out
# serverless framework
.serverless
# sst
bundle/*
# this files are generated by sst and should be checked in!
.sst
sst-react-server.ts
sst-env.d.ts
sst.config.ts
14 changes: 14 additions & 0 deletions examples/hello-world-aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Deploy Adapter AWS

AWS adapter for [@lazarv/react-server](https://npmjs.com/package/@lazarv/react-server).

The bundling of the app for deployment to AWS Lambda requires the aws adapter in `react-server.config.json`:

```json
{
"root": "src",
"adapter": "@lazarv/react-server-adapter-aws"
}
```

See details at https://react-server.dev/deploy/aws.
4 changes: 4 additions & 0 deletions examples/hello-world-aws/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"app": "tsx cdk/bin/infrastructure.ts",
"context": {}
}
1 change: 1 addition & 0 deletions examples/hello-world-aws/cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stack.config.ts
Loading
Loading