LaunchFast Logo LaunchFast

Deploy Astro to AWS Amplify: A Step-by-Step Guide

Rishi Raj Jain
Deploy Astro to AWS Amplify

In this guide, you will learn how to deploy an Astro SSR project to AWS Amplify. You will go through the process of setting up a new Astro project, enabling server-side rendering using AWS Amplify adapter, and finally deploying it to AWS Amplify.

Table Of Contents

Prerequisites

You’ll need the following:

Create a new Astro application

Let’s get started by creating a new Astro project. Open your terminal and run the following command:

npm create astro@latest my-app

npm create astro is the recommended way to scaffold an Astro project quickly.

When prompted, choose:

Once that’s done, you can move into the project directory and start the app:

cd my-app
npm run dev

The app should be running on localhost:4321.

Next, in your first terminal window, run the command below to install the necessary libraries and packages for building the application:

npm install dotenv

The libraries installed include:

Getting Started with the AWS Amplify Adapter for Astro

Before deploying your Astro project, you need to install the astro-aws-amplify adapter. This is easily done via the Astro CLI:

npm install astro-aws-amplify

The libraries installed include:

Once the adapter is installed, you’ll need to add it to your astro.config.mjs file. Open the file and add the following code:

import { defineConfig } from 'astro/config';
import awsAmplify from 'astro-aws-amplify'; 

// https://astro.build/config
export default defineConfig({
  output: "server", 
  adapter: awsAmplify(), 
});

The additions do the following:

Then, create a amplify.yml at the root of repository with the following code:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - env >> .env
        - npm run build
        - mv node_modules ./.amplify-hosting/compute/default
        - mv .env ./.amplify-hosting/compute/default/.env
  artifacts:
    baseDirectory: .amplify-hosting
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

The code above does the following:

Deploy to AWS Amplify

The code is now ready to deploy to AWS Amplify. Use the following steps to deploy:

Host your web app in AWS Amplify GitHub as code source in AWS Amplify Link Repo in AWS Amplify Name the project in AWS Amplify Add Environment Variables in AWS Amplify Deploy to AWS Amplify Grab Deployment URL in AWS Amplify

Conclusion

Yay! You’ve now an Astro 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.

Learn More Using PhotoSwipe in Astro to Build an Image Gallery → Using Transformers for Shiki to enrich Syntax Highlighting in Astro → Using GreenSock Animation Platform (GSAP) in Astro with View Transitions: A Step-by-Step Guide →