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@beta 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 22.13.0 or later installed on your instance
Deployment Steps
Connect to your EC2 instance and clone your repository:
- Public Repository
- Private Repository
git clone https://github.com/<your-username>/<your-repository>.gitgit clone https://<your-username>:<your-personal-access-token>@github.com/<your-username>/<your-repository>.gitNavigate to the repository directory:
cd "<your-repository>"Install dependencies:
npm installCreate a
.envfile and add your environment variables:touch .envEdit the
.envfile 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 --env-file=".env" .mastra/output/index.mjsnoteYour 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>",
});