Deploy Vite on AWS S3 and CloudFront
You can deploy a Vite single-page application (SPA) on your AWS account using Thunder.
At the end of this guide, you will have a Vite project deployed on AWS account using S3 and CloudFront. This web application will be running on resources within your AWS account, and will have a public accessible URL.
Prerequisites
You’ll need the following before you begin:
- A Vite project to deploy - Get started with Vite
- A GitHub repository (public or private) with the Vite application
- An AWS account - Create an account
- A Thunder account - Create an account
There are two ways to deploy your Vite SPA using Thunder:
-
Using the CLI. With the Single Page application pattern.
-
Using the Console to deploy your app with a few clicks.
The build artifacts (HTML, CSS and JavaScript) will be stored in an S3 bucket and served using CloudFront CDN.
Deploy using CLI
CDK-SPA is a package that simplifies the deployment of single-page applications (SPAs) to AWS using the AWS Cloud Development Kit (CDK). It provides a straightforward way to deploy your Vite app to AWS S3 and CloudFront.
1. Create a project
You can create a new Vite project with the vanilla-ts template using the following command:
npm create vite@latest my-vite-app -- --template vanilla-tscd my-vite-appnpm installpnpm create vite my-vite-app --template vanilla-tscd my-vite-apppnpm installbun create vite my-vite-app --template vanilla-tscd my-vite-appbun install2. Initialize your project
Install the necessary dependencies and initialize your project:
npm i tsx aws-cdk-lib @thunderso/cdk-spa --save-devpnpm add -D tsx aws-cdk-lib @thunderso/cdk-spabun add -d tsx aws-cdk-lib @thunderso/cdk-spaCreate a stack/index.ts file. Edit it to match your project:
import { App } from "aws-cdk-lib";import { SPAStack, type SPAProps } from "@thunderso/cdk-spa";
const myApp: SPAProps = { env: { account: 'your-account-id', region: 'us-east-1' }, application: 'your-application-id', service: 'your-service-id', environment: 'production',
rootDir: '', // e.g. 'frontend/' for monorepos outputDir: 'dist/',};
new SPAStack( new App(), `${myApp.application}-${myApp.service}-${myApp.environment}-stack`, myApp);3. Deploy
Before you deploy, run your build script to generate artifacts in the dist directory.
npm run buildpnpm run buildbun run buildBy running the following script, the CDK stack will be deployed to AWS.
cdk deploy --all --app="tsx stack/index.ts"pnpm exec cdk deploy --all --app="pnpm exec tsx stack/index.ts"cdk deploy --all --app="bunx tsx stack/index.ts"When the deployment is complete, you will see the CloudFront URL in the output. You can access your Vite app at that URL.
For complete documentation on how to use CDK-SPA, refer to the CDK-SPA documentation.
Deploy using Thunder Console
You can also deploy your Vite app on AWS using the Thunder console.
Build Settings
Use the following commands in the build step settings:
npm installnpm run builddistpnpm installpnpm run builddistbun installbun run builddistEnvironment Variables
Environment variables in Vite are available at build time and can be used both in your config files and templates. For more information, refer to the official Vite documentation on environment variables.
-
Define your environment variables in the Thunder console under the Environment Variables section of your project settings.
-
In your code, you can define them in an
.envfile at the root of your project:
VITE_API_URL=https://api.example.comUse it in your application:
const apiUrl = import.meta.env.VITE_API_URL;
console.log(`API URL: ${apiUrl}`);