Amazon EC2
Deploy your Mastra applications to Amazon EC2 (Elastic Cloud Compute).
This guide assumes your Mastra application has been created using the default
npx create-mastra@latest
command.
For more information on how to create a new Mastra application,
refer to our getting started guide
Prerequisites
- An AWS account with EC2  access
- An EC2 instance running Ubuntu 24+ or Amazon Linux
- A domain name with an A record pointing to your instance
- A reverse proxy configured (e.g., using nginx )
- SSL certificate configured (e.g., using Let’s Encrypt )
- Node.js 18+ installed on your instance
Deployment Steps
Clone your Mastra application
Connect to your EC2 instance and clone your repository:
Public Repository
git clone https://github.com/<your-username>/<your-repository>.git
Navigate to the repository directory:
cd "<your-repository>"
Install dependencies
npm install
Set up environment variables
Create a .env
file and add your environment variables:
touch .env
Edit the .env
file and add your environment variables:
OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variables
Build the application
npm run build
Run the application
node --import=./.mastra/output/instrumentation.mjs --env-file=".env" .mastra/output/index.mjs
Your Mastra application will run on port 4111 by default. Ensure your reverse proxy is configured to forward requests to this port.
Connect to your Mastra server
You can now connect to your Mastra server from your client application using a MastraClient
from the @mastra/client-js
package.
Refer to the MastraClient
documentation for more information.
import { MastraClient } from "@mastra/client-js";
const mastraClient = new MastraClient({
baseUrl: "https://<your-domain-name>",
});