Deploy VitePress on AWS S3 and CloudFront
You can deploy a VitePress site on your AWS account.
There are two ways to deploy your VitePress site on AWS:
-
Using the AWS Cloud Development Kit (CDK) with the CDK-SPA package.
-
Using the Thunder 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.
View code in StackBlitz
Deploy using AWS CDK
CDK-SPA is a package that simplifies the deployment of static sites to AWS using the AWS Cloud Development Kit (CDK). It provides a straightforward way to deploy your VitePress site to AWS S3 and CloudFront.
1. Create a project
You can create a new VitePress project using the following commands:
npm create vitepress@latest my-vitepress-sitecd my-vitepress-sitenpm install
pnpm create vitepress my-vitepress-sitecd my-vitepress-sitepnpm install
bun create vitepress my-vitepress-sitecd my-vitepress-sitebun install
2. Initialize your project
Install the necessary dependencies and initialize your project:
npm i tsx aws-cdk-lib @thunderso/cdk-spa --save-dev
pnpm add -D tsx aws-cdk-lib @thunderso/cdk-spa
bun add -d tsx aws-cdk-lib @thunderso/cdk-spa
Create 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: 'docs/.vitepress/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 docs/.vitepress/dist
directory.
npm run docs:build
pnpm run docs:build
bun run docs:build
By running the following script, the CDK stack will be deployed to AWS.
npx cdk deploy --all --app="npx tsx stack/index.ts"
pnpm exec cdk deploy --all --app="pnpm exec tsx stack/index.ts"
npx 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 VitePress site 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 VitePress site on AWS using the Thunder console.
Build Settings
Use the following commands in the build step settings:
npm install
npm run docs:build
docs/.vitepress/dist
npx pnpm install
npx pnpm run docs:build
docs/.vitepress/dist
npx bun install
npx bun run docs:build
docs/.vitepress/dist
Environment Variables
Environment variables in VitePress 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
.env
file at the root of your project:
VITE_APP_NAME=test
Access the variable in your VitePress config:
export default async ({ mode }) => { const env = loadEnv(mode || '', process.cwd())
const name = env.VITE_APP_NAME;
return defineConfig({ title: name, /** */ })}
Use it in your templates:
<script setup>const name = import.meta.env.VITE_APP_NAME;</script>
# {{ name }}