Back to Mastra

Deploy Mastra to Amazon EC2 | Deployment

docs/src/content/en/guides/deployment/amazon-ec2.mdx

2025-12-183.5 KB
Original Source

import Steps from "@site/src/components/Steps"; import StepItem from "@site/src/components/StepItem"; import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";

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

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

<Steps> <StepItem> Connect to your EC2 instance and clone your repository:
<Tabs>
  <TabItem value="public" label="Public Repository">
    ```bash
    git clone https://github.com/<your-username>/<your-repository>.git
    ```
  </TabItem>

  <TabItem value="private" label="Private Repository">
    ```bash
    git clone [email protected]:<your-username>/<your-repository>.git
    ```
  </TabItem>
</Tabs>

Navigate to the repository directory:

```bash
cd "<your-repository>"
```
</StepItem> <StepItem> Install dependencies:
```bash npm2yarn
npm install
```
</StepItem> <StepItem> Create a `.env` file and add your environment variables:
```bash
touch .env
```

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

```bash
OPENAI_API_KEY=<your-openai-api-key>
# Add other required environment variables
```
</StepItem> <StepItem> Build the application:
```bash npm2yarn
npm run build
```
</StepItem> <StepItem> Run the application:
```bash
node --env-file=".env" .mastra/output/index.mjs
```

This is a basic example. In production, use a process manager like [PM2](https://pm2.keymetrics.io/) or [systemd](https://systemd.io/) to keep your application running and handle restarts.

:::warning
Set up [authentication](/docs/server/auth) before exposing your endpoints publicly.
:::
</StepItem> <StepItem> 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.
</StepItem> <StepItem> Verify your deployment at `https://<your-ec2-address>/api/agents`, which should return a JSON list of your agents. </StepItem> <StepItem> You can now call your Mastra endpoints over HTTP. </StepItem> </Steps>

Next steps