2.2.2 • Published 1 year ago

sveltekit-adapter-aws-base v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

npm stability-alpha

Unit tests Release

codecov

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:

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>

Kind: global function

ParamTypeDefaultDescription
builderanyThe SvelteKit provided Builder object
artifactPathstring"build"The path where to place to SvelteKit files
esbuildOptionsanyOptions to pass to esbuild
streamingbooleanfalseUse Lambda response streaming

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

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

ParamTypeDefaultDescription
builderanyThe SvelteKit provided Builder object
artifactPathstring"build"The path where to place to SvelteKit files

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

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

ParamTypeDefaultDescription
builderanyThe SvelteKit provided Builder object
static_directorystringlocation of static page files
prerendered_directorystringlocation of prerendered page files
serverURLstringfunction URL for the server lambda
optionsURLstringfunction URL for the options handler lambda
artifactPathstring"build"The path where to place to SvelteKit files

SiteProps : Object

Kind: global typedef
Properties

NameTypeDescription
server_directorystringlocation of files for the SSR server
static_directorystringlocation of static page files
prerendered_directorystringlocation 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.

2.2.2

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.6.0

1 year ago

1.5.5

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.8

1 year ago

1.4.7

1 year ago

1.4.6

1 year ago

1.4.5

1 year ago

1.4.4

1 year ago

1.4.3

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago