In this guide, you will learn how to deploy an SvelteKit SSR project to AWS Amplify. You will go through the process of setting up a new SvelteKit project, enabling server-side rendering using AWS Amplify adapter, and finally deploying it to AWS Amplify.
Table Of Contents
- Prerequisites
- Create a new SvelteKit application
- Getting Started with the Node.js Adapter for SvelteKit
- Deploy to AWS Amplify
- Conclusion
Prerequisites
You’ll need the following:
- Node.js 18 or later
- An AWS account
Create a new SvelteKit application
Let’s get started by creating a new SvelteKit project. Open your terminal and run the following command:
npm create svelte
is the recommended way to scaffold an SvelteKit project quickly.
When prompted, choose:
SvelteKit demo app
when prompted to select the app template.Yes, using TypeScript syntax
when prompted to add type checking with TypeScript.Add Prettier for code formatting
when prompted to select additional options.
Once that’s done, you can move into the project directory, install the dependencies and start the app:
The app should be running on localhost:5173.
Next, in your first terminal window, run the command below to install the necessary libraries and packages for building the application:
The libraries installed include:
dotenv
: A library for handling environment variables.@vercel/nft
: A library to determine all the files necessary for the application runtime using entrypoint(s).
The development-specific libraries include:
prepend-file
: A library to prepend data to a file.
Getting Started with the Node.js Adapter for SvelteKit
Before deploying your SvelteKit project, you need to install the @sveltejs/adapter-node
adapter. Execute the following command in your terminal window:
The libraries installed include:
@sveltejs/adapter-node
: An adapter that prepares SvelteKit websites to be ran as standalone Node.js server.
Once the adapter is installed, you’ll need to add it to your svelte.config.js
file. Open the file and add the following code:
The additions do the following:
- Imports default export of
@sveltejs/adapter-node
. - Uses the above import as the
adapter
of your SvelteKit application.
Then, create a amplify.mjs
file at the root of repository with the following code:
Then, create a amplify.yml
file at the root of repository with the following code:
The code above does the following:
- Uses
preBuild
commands to install the dependencies of your SvelteKit project. - Uses
build
commands to:- Store all the environment variables into
.env
file at the root of the project. - Build your SvelteKit application.
- Move the
node_modules
directory and.env
file to Amplify’s compute directory.
- Store all the environment variables into
Deploy to AWS Amplify
The code is now ready to deploy to AWS Amplify. Use the following steps to deploy:
-
Start by creating a GitHub repository containing your app’s code.
-
Then, navigate to the AWS Amplify Dashboard and click on Get Started under Host your web app section.
- Select GitHub as the source of your Git repository.
- Link the new project to the GitHub repository you just created.
- Give a name to your project, and click on Advanced Settings.
- In Advanced Settings, update the Environment Variables to match those in your local
.env
file, andPORT
as 3000. Click Next to proceed.
- Click Save and Deploy to deploy your website.
- Grab the deployment URL under the Domain title in the succesful build information.
Conclusion
Yay! You’ve now an SvelteKit project that automatically deploys to AWS Amplify upon Git push.
If you have any questions or comments, feel free to reach out to me on Twitter.