Skip to main content
Mastra 1.0 is available 🎉 Read announcement

Deploy Mastra to Amazon EC2

Deploy your Mastra server to Amazon EC2. This gives you full control over your server environment and supports long-running agents and workflows.

info

This guide covers deploying the Mastra server. If you're using a server adapter or web framework, deploy the way you normally would for that framework.

Before you begin
Direct link to Before you begin

You'll need:

For production, you'll also need:

  • A domain name pointing to your instance (required for SSL certificates)
  • An SSL certificate for your domain (e.g., using Certbot with Let's Encrypt)
  • A reverse proxy (e.g., nginx) to forward traffic to your application

Deploy
Direct link to Deploy

  1. Connect to your EC2 instance and clone your repository:

    git clone https://github.com/<your-username>/<your-repository>.git

    Navigate to the repository directory:

    cd "<your-repository>"
  2. Install dependencies:

    npm install
  3. Create a .env file and add your environment variables:

    touch .env

    Remember to set your environment variables needed to run your application (e.g. your model provider API key):

    OPENAI_API_KEY=<your-openai-api-key>
    # Add other required environment variables
  4. Build the application:

    npm run build
  5. Run the application:

    node --env-file=".env" .mastra/output/index.mjs

    This is a basic example. In production, use a process manager like PM2 or systemd to keep your application running and handle restarts.

    warning

    Set up authentication before exposing your endpoints publicly.

  6. Your Mastra server is now running on port 4111, but it's only accessible locally.

    You can open port 4111 in your EC2 security group for direct access, or configure a reverse proxy (such as nginx) to listen on ports 80 and 443 and forward requests to http://localhost:4111.

    In production, you should use a reverse proxy so you can configure HTTPS. HTTPS encrypts traffic and is required for most webhook integrations and external services your agents interact with.

  7. Verify your deployment at https://<your-ec2-address>/api/agents, which should return a JSON list of your agents.

  8. You can now call your Mastra endpoints over HTTP.

Next steps
Direct link to Next steps

On this page