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>.gitNavigate to the repository directory:
cd "<your-repository>"Install dependencies
npm installSet up environment variables
Create a .env file and add your environment variables:
touch .envEdit the .env file and add your environment variables:
OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variablesBuild the application
npm run buildRun the application
node --import=./.mastra/output/instrumentation.mjs --env-file=".env" .mastra/output/index.mjsYour 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>",
});