Skip to content

Commit

Permalink
docs: bucket router change
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Sep 18, 2024
1 parent 939c833 commit d07c89c
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 95 deletions.
2 changes: 1 addition & 1 deletion examples/aws-angular/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true
access: "public",
});

const pre = new sst.aws.Function("MyFunction", {
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-api/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
const api = new sst.aws.ApiGatewayV2("MyApi");
api.route("GET /", {
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-astro/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
new sst.aws.Astro("MyWeb", {
link: [bucket],
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-cluster/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});

const vpc = new sst.aws.Vpc("MyVpc");
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-hono/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
const hono = new sst.aws.Function("Hono", {
url: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-nextjs/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
new sst.aws.Nextjs("MyWeb", {
link: [bucket],
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-nuxt/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true
access: "public",
});
new sst.aws.Nuxt("MyWeb", {
link: [bucket],
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-remix/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
new sst.aws.Remix("MyWeb", {
link: [bucket],
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-router/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default $config({
url: true,
});
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
const router = new sst.aws.Router("MyRouter", {
domain: "router.ion.dev.sst.dev",
Expand Down
2 changes: 1 addition & 1 deletion examples/aws-svelte-kit/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true
access: "public",
});
new sst.aws.SvelteKit("MyWeb", {
link: [bucket],
Expand Down
2 changes: 1 addition & 1 deletion examples/internal/playground/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default $config({
},
async run() {
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
transform: {
bucket: (args) => {
args.tags = { foo: "bar" };
Expand Down
21 changes: 13 additions & 8 deletions platform/src/components/aws/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ export interface BucketArgs {
*/
public?: Input<boolean>;
/**
* Enable public read access for all the files in the bucket.
* Enable public read access for all the files in the bucket. By default, no access is
* granted.
*
* Following are the possible values:
* - `public`: Host files directly from the bucket.
* - `cloudfront`: Use CloudFront to serve files from the bucket.
* :::tip
* If you are using the `Router` to serve files from this bucket, you need to allow
* `cloudfront` access the bucket.
* :::
*
* This adds a statement to the bucket policy that either allows `public` access or just
* `cloudfront` access.
*
* @example
* ```js
Expand Down Expand Up @@ -279,7 +284,7 @@ interface BucketRef {
*
* ```ts title="sst.config.ts"
* new sst.aws.Bucket("MyBucket", {
* public: true
* access: "public"
* });
* ```
*
Expand Down Expand Up @@ -410,9 +415,9 @@ export class Bucket extends Component implements Link.Linkable {
access === "public"
? { type: "*", identifiers: ["*"] }
: {
type: "Service",
identifiers: ["cloudfront.amazonaws.com"],
},
type: "Service",
identifiers: ["cloudfront.amazonaws.com"],
},
],
actions: ["s3:GetObject"],
resources: [interpolate`${bucket.arn}/*`],
Expand Down
141 changes: 89 additions & 52 deletions platform/src/components/aws/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,62 @@ export interface RouterUrlRouteArgs extends BaseRouteArgs {
* ```
*/
url: Input<string>;
/**
* Rewrite the request path.
*
* @example
*
* By default, if the route path is `/api/*` and a request comes in for `/api/users/profile`,
* the request path the destination sees is `/api/users/profile`.
*
* If you want to serve the route from the root, you can rewrite the request path to
* `/users/profile`.
*
* ```js
* {
* routes: {
* "/api/*": {
* url: "https://api.example.com",
* rewrite: {
* regex: "^/api/(.*)$",
* to: "/$1"
* }
* }
* }
* }
* ```
*/
rewrite?: Input<{
/**
* The regex to match the request path.
*/
regex: Input<string>;
/**
* The replacement for the matched path.
*/
to: Input<string>;
}>;
}

export interface RouterBucketRouteArgs extends BaseRouteArgs {
/**
* A bucket to route to.
*
* :::note
* You need to let CloudFront `access` the bucket.
* :::
*
* @example
*
* For example, let's say you have a bucket.
* For example, let's say you have a bucket that gives CloudFront `access`.
*
* ```ts title="sst.config.ts" {2}
* const myBucket = new sst.aws.Bucket("MyBucket", {
* access: "cloudfront"
* });
* ```
*
* :::note
* The `access` props lets CloudFront access this bucket.
* :::
*
* You can set this directly as the destination for the route.
* You can then this directly as the destination for the route.
*
* ```js
* {
Expand All @@ -70,9 +105,6 @@ export interface RouterBucketRouteArgs extends BaseRouteArgs {
* ```
*/
bucket?: Input<Bucket | string>;
}

interface BaseRouteArgs {
/**
* Rewrite the request path.
*
Expand Down Expand Up @@ -109,6 +141,9 @@ interface BaseRouteArgs {
*/
to: Input<string>;
}>;
}

interface BaseRouteArgs {
/**
* Configure CloudFront Functions to customize the behavior of HTTP requests and responses at the edge.
*/
Expand Down Expand Up @@ -307,8 +342,10 @@ export interface RouterArgs {
* A map of routes to their destinations. The _key_ is the route path and the
* _value_ can be:
*
* - A string, the destination URL.
* - Or an object with the above properties.
* - The destination URL as a string
* - Or, an object with
* - Args for a URL route
* - Args for a bucket route
*
* :::note
* All routes need to start with `/`.
Expand Down Expand Up @@ -478,19 +515,21 @@ export interface RouterArgs {
*
* #### Route to a bucket
*
* ```ts title="sst.config.ts"
* ```ts title="sst.config.ts" {2}
* const myBucket = new sst.aws.Bucket("MyBucket", {
* access: "cloudfront",
* access: "cloudfront"
* });
*
* new sst.aws.Router("MyRouter", {
* routes: {
* "/files/*": myBucket
* "/files/*": {
* bucket: myBucket
* }
* }
* });
* ```
*
* Make sure to allow AWS CloudFront to access the bucket by setting the `access` prop on the bucket.
* Make sure to allow CloudFront access to the bucket by setting the `access` prop on the bucket.
*
* #### Route all API requests separately
*
Expand Down Expand Up @@ -587,15 +626,15 @@ export class Router extends Component implements Link.Linkable {
path: string,
config:
| {
injection: string;
kvStores?: string[];
}
injection: string;
kvStores?: string[];
}
| undefined,
rewrite:
| {
regex: string;
to: string;
}
regex: string;
to: string;
}
| undefined,
injectHostHeader: boolean,
) {
Expand All @@ -606,18 +645,16 @@ export class Router extends Component implements Link.Linkable {
keyValueStoreAssociations: config?.kvStores ?? [],
code: `
function handler(event) {
${
injectHostHeader
? `event.request.headers["x-forwarded-host"] = event.request.headers.host;`
: ""
}
${
rewrite
? `
${injectHostHeader
? `event.request.headers["x-forwarded-host"] = event.request.headers.host;`
: ""
}
${rewrite
? `
const re = new RegExp("${rewrite.regex}");
event.request.uri = event.request.uri.replace(re, "${rewrite.to}");`
: ""
}
: ""
}
${config?.injection ?? ""}
return event.request;
}`,
Expand Down Expand Up @@ -796,30 +833,30 @@ function handler(event) {
functionAssociations: [
...("url" in route || route.edge?.viewerRequest || route.rewrite
? [
{
eventType: "viewer-request",
functionArn:
route.edge?.viewerRequest || route.rewrite
? createCfRequestFunction(
path,
route.edge?.viewerRequest,
route.rewrite,
"url" in route,
).arn
: createCfRequestDefaultFunction().arn,
},
]
{
eventType: "viewer-request",
functionArn:
route.edge?.viewerRequest || route.rewrite
? createCfRequestFunction(
path,
route.edge?.viewerRequest,
route.rewrite,
"url" in route,
).arn
: createCfRequestDefaultFunction().arn,
},
]
: []),
...(route.edge?.viewerResponse
? [
{
eventType: "viewer-response",
functionArn: createCfResponseFunction(
path,
route.edge.viewerResponse,
).arn,
},
]
{
eventType: "viewer-response",
functionArn: createCfResponseFunction(
path,
route.edge.viewerResponse,
).arn,
},
]
: []),
],
...("url" in route ? urlDefaultConfig : bucketDefaultConfig),
Expand Down
6 changes: 2 additions & 4 deletions www/src/content/docs/docs/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,7 @@ new aws.s3.BucketObjectv2("MyImage", {
bucket: bucket.name,
key: "public/spongebob.svg",
contentType: "image/svg+xml",
source: new $util.asset.FileAsset(
path.join($cli.paths.root, "spongebob.svg")
),
source: $asset("spongebob.svg"),
});

const router = new sst.aws.Router("MyRouter", {
Expand Down Expand Up @@ -813,7 +811,7 @@ const api = new sst.aws.Function("MyApi", {
url: true,
});
const bucket = new sst.aws.Bucket("MyBucket", {
public: true,
access: "public",
});
const router = new sst.aws.Router("MyRouter", {
domain: "router.ion.dev.sst.dev",
Expand Down
Loading

0 comments on commit d07c89c

Please sign in to comment.