Back to list
Terraform、AWS EC2、Amazon Bedrock で OpenClaw をデプロイ — API キーなし
Deploy OpenClaw on AWS EC2 with Amazon Bedrock Using Terraform — No API Keys Required
Translated: 2026/3/15 18:01:33
Japanese Translation
【この記事の意義】
一般的なセットアップでは、LLM プロバイダーの API キーを管理する必要があります。しかし、AWS を実行する場合は、IAM インスタンスロールを備えた Amazon Bedrock というよりクリーンなアプローチがあります。EC2 インスタンスはインスタンスメタデータを通じて Bedrock に自動的に認証され、秘密情報をローテーションする必要も、環境変数をリークするリスクもないのです。
本記事では、VPC セキュリティグループから実行中の OpenClaw ゲートウェイまで、および OIDC 認証を備えた GitHub Actions CI/CD パイプラインを含む、テラフォームによる自動デプロイ全体を歩みを進めます。
リポジトリ:github.com/Yash-Kavaiya/openclaw-bedrock-ec2
【OpenClawとは】
主要特性:
自己ホスト型:ハードウェア上で動作し、あなたの制御下にあり
アーキテクチャ概要
┌─────────────────────────────────────────────┐
Amazon Linux 2023 を実行中の t3.small EC2 インスタンス
terraform >= 1.5 がインストール済み(インストールガイド参照)
openclaw-bedrock-ec2/
IAM:キーレス Bedrock アクセス
核心のアイデアは、API キーの代わりに IAM インスタンスロールを使用することです。iam.tf ファイルは以下を生成します:
hcl
resource "aws_iam_role" "openclaw_bedrock" {
assume_role_policy = jsonencode({
resource "aws_iam_role_policy_attachment" "bedrock_full_access" {
resource "aws_iam_instance_profile" "openclaw_bedrock" {
bedrock:InvokeModel
【OpenClaw 設定テンプレート】
The openclaw.json.tpl テンプレートは、Bedrock を介して 3 つの Claude モデルを接続します:
json
{
"models": {
https://bedrock-runtime.${region}.amazonaws.com",
【EC2 バュートストラップスクリプト】
The user_data.sh.tpl スクリプトは初回起動時に実行されます:
bash
set -euo pipefail
dnf update -y
curl -fsSL https://rpm.nodesource.com/setup_22.x | bash -
pm install -g openclaw
mkdir -p /root/.openclaw
systemctl daemon-reload
起動時にも自動的に開始されます
【変数】
変数 必須 デフォルト 説明
Yes
—
EC2 キーペア名
openclaw_gateway_token
Yes
—
ゲートウェイの秘密認証トークン
region
No
us-east-1
AWS リージョン
instance_type
No
t3.small
EC2 インスタンスタイプ
ami_id
No
最新 AL2023
オーバーライド AMI
allowed_ssh_cidr
No
0.0.0.0/0
SSH アクセスの制限
aws_bearer_token_bedrock
No
""
Bearer トークン(代替認証)
【ステップバイステップデプロイメント】
git clone https://github.com/Yash-Kavaiya/openclaw-bedrock-ec2
cp terraform.tfvars.example terraform.tfvars
hcl
key_name = "my-ec2-keypair"
ステップ 3: 初期化とデプロイ
terraform init
text
openclaw_gateway_url = http://:18789
bash
openclaw config set gateway.url http://:18789
bash
ssh -i ~/.ssh/my-key.pem ec2-user@
【パイプライン動作】
トリガー 起こること
Lint + Validate → Plan(PR コメントとして投稿)
masterへのプッシュ
Lint + Validate → マニュアル承認 → 適用
workflow_dispatch
マニュアル承認 → 削除
【OIDC 認証の仕組み】
GitHub Actions は GitHub の OIDC プロバイダーから短期有効期限の JWT をリクエストします
【セットアップステップ】
terraform apply \
terraform output github_actions_role_arn
Secret Value
ステップ 1 から得た ARN
TF_KEY_NAME
あなたの EC2 キーペア名
OPENCLAW_GATEWAY_TOKEN
あなたのゲートウェイ認証トークン
生産環境を作成(リポジトリ → セッティング → 環境)とし、自身を必須レビューアーとして追加します。これにより、適用と削除のタスクがマニュアル承認後に制限されます。
ポート 18789 はデフォルトで 0.0.0.0/0 に開放されています。生産環境では、CIDR を制限するか、インスタンスを VPN、Tailscale、または TLS 終端を備えた ALB の後ろに配置してください。
EC2 t3.small:〜$15/月(オンデマンド)、レジャーヴドまたはスポット实例ではさらに安価
bash
terraform destroy
【まとめ】
API キー管理 — IAM インスタンスロールは Bedrock への認証を自動的に処理します
始めましょう:github.com/Yash-Kavaiya/openclaw-bedrock-ec2
Original Content
Deploy OpenClaw on AWS EC2 with Amazon Bedrock Using Terraform — No API Keys Required
Why This Matters
The typical setup requires managing API keys for your LLM provider. But when you run on AWS, there's a cleaner approach: Amazon Bedrock with IAM instance roles. The EC2 instance authenticates to Bedrock automatically through instance metadata — no secrets to rotate, no environment variables to leak.
This article walks through a Terraform project that automates the entire deployment, from VPC security groups to a running OpenClaw gateway, including a GitHub Actions CI/CD pipeline with OIDC authentication.
Repository: github.com/Yash-Kavaiya/openclaw-bedrock-ec2
What is OpenClaw?
Key characteristics:
Self-hosted: Runs on your hardware, under your control
Architecture Overview
┌─────────────────────────────────────────────┐
An EC2 instance running Amazon Linux 2023 on t3.small
Terraform >= 1.5 installed (install guide)
openclaw-bedrock-ec2/
IAM: Keyless Bedrock Access
The core idea is using an IAM instance role instead of API keys. The iam.tf file creates:
hcl
resource "aws_iam_role" "openclaw_bedrock" {
assume_role_policy = jsonencode({
resource "aws_iam_role_policy_attachment" "bedrock_full_access" {
resource "aws_iam_instance_profile" "openclaw_bedrock" {
bedrock:InvokeModel
OpenClaw Configuration Template
The openclaw.json.tpl template wires up three Claude models through Bedrock:
json
{
"models": {
https://bedrock-runtime.${region}.amazonaws.com",
Primary model: Claude Sonnet 4.6 — strong balance of capability and speed
EC2 Bootstrap Script
The user_data.sh.tpl script runs on first boot:
bash
set -euo pipefail
dnf update -y
curl -fsSL https://rpm.nodesource.com/setup_22.x | bash -
npm install -g openclaw
mkdir -p /root/.openclaw
systemctl daemon-reload
Starts automatically on boot
Variables
Variable Required Default Description
Yes
—
EC2 key pair name
openclaw_gateway_token
Yes
—
Secret auth token for the gateway
region
No
us-east-1
AWS region
instance_type
No
t3.small
EC2 instance type
ami_id
No
Latest AL2023
Override AMI
allowed_ssh_cidr
No
0.0.0.0/0
Restrict SSH access
aws_bearer_token_bedrock
No
""
Bearer token (alternative auth)
Step-by-Step Deployment
git clone https://github.com/Yash-Kavaiya/openclaw-bedrock-ec2
cp terraform.tfvars.example terraform.tfvars
hcl
key_name = "my-ec2-keypair"
Step 3: Initialize and Deploy
terraform init
text
openclaw_gateway_url = http://:18789
bash
openclaw config set gateway.url http://:18789
bash
ssh -i ~/.ssh/my-key.pem ec2-user@
Pipeline Behavior
Trigger What Happens
Lint + Validate → Plan (posted as PR comment)
Push to master
Lint + Validate → Manual approval → Apply
workflow_dispatch
Manual approval → Destroy
How OIDC Auth Works
GitHub Actions requests a short-lived JWT from GitHub's OIDC provider
Setup Steps
terraform apply \
terraform output github_actions_role_arn
Secret Value
ARN from step 1
TF_KEY_NAME
Your EC2 key pair name
OPENCLAW_GATEWAY_TOKEN
Your gateway auth token
Create a production environment (repo → Settings → Environments) and add yourself as a required reviewer. This gates the apply and destroy jobs behind manual approval.
Port 18789 is open to 0.0.0.0/0 by default. For production, restrict the CIDR or place the instance behind a VPN, Tailscale, or an ALB with TLS termination.
EC2 t3.small: ~$15/month (on-demand), less with Reserved or Spot
bash
terraform destroy
Wrapping Up
API key management — IAM instance roles handle authentication to Bedrock automatically
Get started: github.com/Yash-Kavaiya/openclaw-bedrock-ec2
Tags: AWS, Terraform, Amazon Bedrock, Claude, OpenClaw, Infrastructure as Code, AI Agents, DevOps, GitHub Actions, OIDC