SvelteKit AWS Adapter Base Package

This project is for the use of SvelteKit adapters which deploy to AWS using various IAC providers. It provides functions common to a reference AWS architecture.

The project is in development and is seeking collaborators to develop implementations for IAC providers. See the examples section for the IAC providers that are currently supported. Please feel free to open an issue if you would like to discuss using or developing this package.

Installation

$ npm install sveltekit-adapter-aws-base

How to use?

This package is not intended for end-user usage. Please use one of the consumers of this package in the example IAC providers section.

For developers of AWS SvelteKit adapters that wish to implement a new IAC solution, this package provides functions for implementing the following reference architecture:

The lambda@edge function handles origin requests from a Cloudfront CDN that has a default S3 origin for static files and two lambda function URLs for the SSR server and an OPTIONS request handler (for preflight CORS checks). The router signs requests to the lambda URLs, thus they can be configured to use AWS_IAM authentication. If the S3 origin is also secured with OAC, then all origins will only be accessible through the CDN.

The functions provided by this package implement the SSR server, options handler and lambda@edge router. They are defined as follows:

buildServer(builder, artifactPath, esbuildOptions)Promise.<SiteProps>

Prepare SvelteKit SSR server files for deployment to AWS services

buildOptions(builder, artifactPath)Promise.<string>

Prepare options handler for deployment to AWS services

buildRouter(builder, static_directory, prerendered_directory, serverURL, optionsURL, artifactPath)Promise.<string>

Prepare lambda@edge origin router for deployment to AWS services

buildServer(builder, artifactPath, esbuildOptions) ⇒ Promise.<SiteProps>

Prepare SvelteKit SSR server files for deployment to AWS services.

To determine the URL request origin the server uses the following hierarchy:

  • The ORIGIN environment variable
  • The value of the 'X-Forwarded-Host' header
  • The domain name within the request event

The origin value is important to prevent CORS errors.

Kind: global function

Param Type Default Description
builder any

The SvelteKit provided Builder object

artifactPath string "build"

The path where to place to SvelteKit files

esbuildOptions any

Options to pass to esbuild

streaming boolean false

Use Lambda response streaming

buildOptions(builder, artifactPath) ⇒ Promise.<string>

Prepare options handler for deployment to AWS services

Kind: global function
Returns: Promise.<string> - Location of files for the options handler

Param Type Default Description
builder any

The SvelteKit provided Builder object

artifactPath string "build"

The path where to place to SvelteKit files

buildRouter(builder, static_directory, prerendered_directory, serverURL, optionsURL, artifactPath) ⇒ Promise.<string>

Prepare lambda@edge origin router for deployment to AWS services

Note that this function will forward the original Host header as 'X-Forwarded-Host' to the lambda URLs.

Kind: global function
Returns: Promise.<string> - Location of files for the origin router

Param Type Default Description
builder any

The SvelteKit provided Builder object

static_directory string

location of static page files

prerendered_directory string

location of prerendered page files

serverURL string

function URL for the server lambda

optionsURL string

function URL for the options handler lambda

artifactPath string "build"

The path where to place to SvelteKit files

SiteProps : Object

Kind: global typedef
Properties

Name Type Description
server_directory string

location of files for the SSR server

static_directory string

location of static page files

prerendered_directory string

location of prerendered page files

The functions above should be used within a SvelteKit adapter function; for example:

import {
  buildServer,
  buildOptions,
  buildRouter,
} from 'sveltekit-adapter-aws-base'

export default function ({
  artifactPath = 'build',
  esbuildOptions = {},
  // More options
} = {}) {
  /** @type {import('@sveltejs/kit').Adapter} */
  const adapter = {
    name: 'adapter-aws-myiacprovider',
    async adapt(builder) {
      const { serverDirectory, staticDirectory, prerenderedDirectory } =
        await buildServer(builder, artifactPath, esbuildOptions)
      const optionsDirectory = await buildOptions(builder, artifactPath)

      // Deploy server to lambda and get domain name of URL. These can use
      // AWS_IAM AuthType, as the router will sign requests.
      const serverDomain = getLambdaURLDomain(server_directory)
      const optionsDomain = getLambdaURLDomain(server_directory)

      const edgeDirectory = await buildRouter(
        builder,
        staticDirectory,
        prerenderedDirectory,
        serverDomain,
        optionsDomain,
        artifactPath
      )

      // Deploy router to lambda and get its arn
      const routerArn = getLambdaArn(edgeDirectory)

      // Upload static files to S3
      const myBucket = deployS3(staticDirectory, prerenderedDirectory)

      // Deploy a CloudFront CDN with the S3 bucket as the default origin
      // (with OAC for added security) and the lambda@edge function to handle
      // origin-requests.
      const CDN = deployCDN({
        defaultOriginBucket: myBucket,
        defaultCacheBehaviourOriginRequestHandler: routerArn,
      })
    },
  }

  return adapter
}

Example IAC providers

Credits

This package is derived from Mike Bild's adapter for CDK and James Bray's adapter for Serverless Framework.

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes