Amazon EC2
Amazon EC2(Elastic Cloud Compute)にMastraアプリケーションをデプロイします。
このガイドでは、Mastraアプリケーションがデフォルトの
npx create-mastra@latest
コマンドを使用して作成されていることを前提としています。
新しいMastraアプリケーションの作成方法の詳細については、
はじめにガイドを参照してください
EC2のセットアップ
AWSコンソールにログイン
AWS Management Console に移動し、アカウントにサインインします。
EC2に移動
左側のナビゲーションでAll servicesに移動します。Computeの下でEC2をクリックします。
仮想サーバーインスタンスを起動
Launch instanceをクリックして新しいEC2インスタンスを作成します。
インスタンスの詳細を設定
インスタンスに以下の詳細を追加します:
- Name: インスタンスに分かりやすい名前を付けます
- Application and OS Images: この例ではAmazon Linux環境を使用します
- Instance type: t3.microを選択します(無料利用枠対象)
- Key pair: 既存のキーペアを選択するか、安全なログインのために新しいキーペアを作成します
- Network settings: 適切なボックスにチェックを入れてインターネットからのHTTPSおよびHTTPトラフィックを許可することを確認します
インスタンスを起動
設定を確認し、Launch instanceをクリックします。
インスタンスに接続
次のステップページにリダイレクトされます。インスタンスには以下のいずれかの方法で接続できます:
- ブラウザ経由: ブラウザベースのアクセスのためにConnectボタンをクリック
- SSH経由: キーペアを使用してインスタンスにSSH接続(Connectをクリックすると手順が表示されます)
サーバー設定
EC2インスタンスにアクセスできるようになったら(SSHまたはブラウザ経由)、以下の手順に従ってサーバー環境をセットアップします:
Update the system
Update the system packages before installing any new packages.
sudo apt update && sudo apt upgrade -y
Install nginx
sudo apt install nginx -y
Ensure nginx.conf includes sites-enabled
Check if the main nginx configuration includes the sites-enabled directory. This is crucial for our site configuration to work.
sudo grep -q "include /etc/nginx/sites-enabled/" /etc/nginx/nginx.conf || sudo sed -i '/http {/a\\tinclude /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
This command checks if the include /etc/nginx/sites-enabled/*;
directive exists in the nginx.conf file. If it doesn’t exist, it adds it to the http block. This ensures that our site configuration files in sites-enabled will be loaded by nginx.
Remove nginx files if they exist
Let’s start by setting up an environment variable for the site name. In our example, we will use mastra
as the site name.
export SITE_NAME="mastra"
Then we can remove the nginx files if they exist.
sudo rm -f /etc/nginx/sites-available/$SITE_NAME
sudo rm -f /etc/nginx/sites-enabled/$SITE_NAME
Set up certbot for SSL
To set up certbot for SSL, let’s start by stopping nginx temporarily.
sudo systemctl stop nginx
Now we can obtain a certificate for our domain name.
Let’s start by setting up an environment variable for the domain name and email.
Replace <your-domain-name>
with your domain name and <your-email>
with your email.
export DOMAIN_NAME="<your-domain-name>"
export EMAIL="<your-email>"
sudo apt install certbot -y
sudo certbot certonly --standalone -d $DOMAIN_NAME --non-interactive --agree-tos -m $EMAIL
Finally, let’s ensure that the SSL files exists or we generate them.
if [ ! -f /etc/letsencrypt/options-ssl-nginx.conf ]; then
sudo wget https://raw.githubusercontent.com/certbot/certbot/refs/heads/main/certbot-nginx/src/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf -P /etc/letsencrypt/
fi
if [ ! -f /etc/letsencrypt/ssl-dhparams.pem ]; then
sudo openssl dhparam -out /etc/letsencrypt/ssl-dhparams.pem 2048
fi
Configure nginx
sudo cat > /etc/nginx/sites-available/$SITE_NAME <<EOL
limit_req_zone \$binary_remote_addr zone=mylimit:10m rate=10r/s;
server {
listen 80;
server_name $DOMAIN_NAME;
# Redirect all HTTP requests to HTTPS
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
server_name $DOMAIN_NAME;
ssl_certificate /etc/letsencrypt/live/$DOMAIN_NAME/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN_NAME/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# Enable rate limiting
limit_req zone=mylimit burst=20 nodelay;
location / {
# Proxy to Mastra server running on port 4111
proxy_pass http://localhost:4111;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
# Disable buffering for streaming support
proxy_buffering off;
proxy_set_header X-Accel-Buffering no;
}
}
EOL
To ensure there’s no issues with streaming, we need to disable buffering. That’s why we have proxy_buffering off;
and proxy_set_header X-Accel-Buffering no;
.
Create a symbolic link to the site in the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/$SITE_NAME /etc/nginx/sites-enabled/$SITE_NAME
Restart nginx.
sudo systemctl restart nginx
Install nodejs
We need to install nodejs to run the Mastra application. We will do this using nvm
.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
Once the installation is complete, you can verify the installation by running:
nvm --version
Now we can install the latest LTS version of nodejs.
nvm install --lts
Once the installation is complete, you can verify the installation by running:
node --version
Clone your Mastra application
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
Then edit .env
and add your environment variables. Here’s an example:
OPENAI_API_KEY=<your-openai-api-key>
Build the application
npm run build
Run the application
node --import=./.mastra/output/instrumentation.mjs --env-file=".env" .mastra/output/index.mjs
Mastraサーバーに接続
@mastra/client-js
パッケージのMastraClient
を使用して、クライアントアプリケーションからMastraサーバーに接続できるようになりました。
詳細については、MastraClient
ドキュメントを参照してください。
import { MastraClient } from "@mastra/client-js";
const mastraClient = new MastraClient({
baseUrl: "https://<your-domain-name>",
});