LaunchFast Logo LaunchFast

Deploy Astro to AWS Elasic Beanstalk: A Step-by-Step Guide

Rishi Raj Jain
Deploy Astro to AWS Elasic Beanstalk

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

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, execute the command below to install the necessary libraries and packages for building the application:

npm install dotenv
npm install -D prepend-file

The libraries installed include:

The development-specific libraries include:

Getting Started with the Node.js Adapter for Astro

Before deploying your Astro project, you need to install the @astrojs/node adapter. Execute the following command:

npm install @astrojs/node

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:

// File: astro.config.mjs

import node from '@astrojs/node'; 
import { defineConfig } from 'astro/config';

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

The additions do the following:

Then, create a zip.mjs at the root of repository with the following code:

// File: zip.mjs

import zl from "zip-lib"
import { join } from "node:path"
import { sync as prependSync } from "prepend-file"

const zip = new zl.Zip()

prependSync(join("dist", "server", "entry.mjs"), `import 'dotenv/config'\n`)

zip.addFolder("./dist", "dist")
zip.addFile("./package.json", "package.json")
zip.addFile("./tsconfig.json", "tsconfig.json")
zip.addFolder("./node_modules", "node_modules")

zip.archive(`./deployment_${Math.random().toString()}.zip`)

console.log('Zipped!')

The code above does the following:

Now, execute the following command to generate a zip file containing static assets, server-side bundle and package.json:

npm run build && node zip.mjs

Once it completes, you’d see a zip file with the name beginning with deployment_. You’ll use this zip to deploy your application on AWS Elastic Beanstalk. Let’s get started with the configuring the IAM roles required to deploy an application to AWS Elastic Beanstalk.

Configure your IAM Roles

Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify

Then, click Next.

Grab Deployment URL in AWS Amplify Grab Deployment URL in AWS Amplify

and you are now done with setting up IAM Roles for your account. Let’s move on to deploy your Astro application to AWS Elastic Beanstalk.

Deploy to AWS Elastic Beanstalk

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

AWS Elastic Beanstalk Configure AWS Elastic Beanstalk application Select platform in AWS Elastic Beanstalk Select service role in AWS Elastic Beanstalk Select VPC in AWS Elastic Beanstalk Select Security Group in AWS Elastic Beanstalk Environment Variables in AWS Elastic Beanstalk Add Environment Variables in AWS Elastic Beanstalk Working AWS Elastic Beanstalk Application Upload the zip in AWS Elastic Beanstalk Open the Assigned Domain in AWS Elastic Beanstalk

Conclusion

Yay! You’ve now an Astro project that deploys to AWS Elasic Beanstalk.

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 →