An adapter to build a SvelteKit app which is aws ready for deployment with cloudfront lambda@edge and S3.
Install AWS CLI in local machine
Install adapter in your SvelteKit project Use pnpm, yarn or npm to install the required package.
pnpm i -d @juspay/sveltekit-aws-adapter
In svelte.config.js
configure the adapter as below:
Lambda function should be in us-east-1
for it to be used with cloudfront.
import preprocess from 'svelte-preprocess';
import adapter from '@juspay/sveltekit-aws-adapter';
const config = {
preprocess: preprocess(),
kit: {
adapter: adapter({
s3: {
bucketName: "some-bucket",
prefix: "",
region: "ap-south-1",
},
lambda: {
functionName: "lambda-function-name",
region: "us-east-1",
},
cloudfront: {
distributionId: "distibution-id",
region: "us-east-1",
}
}),
},
};
export default config;
Adapter takes below config for different aws services.
Generates the required files in a folder /out
.
To be added to .gitignore for CI/CD system.
pnpm run build
Above command will do the below steps :
graph LR;
A[Create Static<br>Output] --> B[Create Lambda<br>Function];
B --> C[Bundle<br>App];
C --> D[Upload to<br>S3];
D --> E[Zip<br>Directory];
E --> F[Deploy Lambda<br>Function];
F --> G[Setup CloudFront<br>Trigger];
G --> H[Invalidate CloudFront<br>Cache];
Create Static Output Copy assets using builder.writeClient(clientDir). Pre-render files using builder.writePrerendered(prerenderedDir).
Create Lambda Function
Generate server function using builder.writeServer(serverDir). Copy handler files to the server handler folder using builder.copy. Save a list of pre-rendered files in the server handler folder using fs.writeFileSync.
Bundle App
Execute bundleApp() to bundle the application.
Upload to S3
Upload the contents of clientDir to S3 using uploadToS3.
Zip Directory
Zip the contents of the build directory into a file named lambda.zip using zipDirectory.
Deploy Lambda Function
Deploy the zipped lambda function using deployLambdaFunction. Get the new version of the deployed Lambda function.
Setup CloudFront Trigger
If the Lambda version is obtained successfully, setup the CloudFront trigger using setupCloudFrontTrigger.
Invalidate CloudFront Cache
Invalidate the CloudFront cache using invalidateCache.
This documentation describes the TypeScript interfaces used to define the AWS configuration for your project.
AWSConfiguration interface is the main configuration interface that aggregates S3Config, LambdaConfig, and CloudFrontConfig.
interface AWSConfiguration {
s3: S3Config; // Configuration for AWS S3.
lambda: LambdaConfig; // Configuration for AWS Lambda.
cloudfront: CloudFrontConfig; // Configuration for AWS CloudFront.
}
S3Config
interface represents the configuration for AWS S3.
interface S3Config {
bucketName: string; // The name of your S3 bucket.
prefix: string; // The prefix to use for objects in the bucket.
region: string; // The AWS region where your S3 bucket is hosted.
}
LambdaConfig interface represents the configuration for AWS Lambda.
interface LambdaConfig {
functionName: string; // The name of your Lambda function.
region: string; // The AWS region where your Lambda function is hosted.
}
CloudFrontConfig interface represents the configuration for AWS CloudFront.
interface CloudFrontConfig {
distributionId: string; // The ID of your CloudFront distribution.
region: string; // The AWS region where your CloudFront distribution is hosted.
}