Self-Host OpenClaw on AWS EC2
AWS EC2 gives you granular control over compute, networking, and storage for running OpenClaw Gateway. This guide covers launching an Ubuntu EC2 instance, configuring security groups for zero-trust access, installing Docker, and connecting securely via SSH tunnel or AWS Systems Manager.
Quick path
For experienced AWS users:
- Launch Ubuntu 24.04 LTS AMI on a
t3.smallin your preferred region - Security group: allow SSH (port 22) from your IP only, no other inbound rules
- SSH in, run
curl -fsSL https://get.docker.com | sh - Create directories and
.env:mkdir -p ~/.openclaw/workspace - Create
docker-compose.ymlwith127.0.0.1:18789binding and volume mounts - Run
docker compose up -d - SSH tunnel:
ssh -i key.pem -N -L 18789:127.0.0.1:18789 ubuntu@EC2_IP - Open
http://127.0.0.1:18789/
Prerequisites
- An AWS account
- AWS CLI configured locally (
aws configure) - An EC2 key pair created in your target region
- Basic understanding of AWS networking concepts
Step 1 — Launch an EC2 Instance
Choose an instance type
| Instance | vCPU | RAM | Monthly cost (us-east-1) | Notes |
|---|---|---|---|---|
| t3.small | 2 | 2 GB | ~$15 | Minimum viable, add swap |
| t3.medium | 2 | 4 GB | ~$30 | Recommended for daily use |
| t3a.medium | 2 | 4 GB | ~$27 | AMD variant, slightly cheaper |
Costs are approximate for on-demand pricing in us-east-1. Reserved instances or Savings Plans can reduce costs by 30-60%.
Launch via the AWS Console
- Go to EC2 > Launch Instance
- Name:
openclaw-gateway - AMI: Ubuntu 24.04 LTS (search "ubuntu 24.04" in the AMI catalog)
- Instance type:
t3.smallort3.medium - Key pair: Select your existing key pair or create a new one
- Network settings: Create a new security group (details below)
- Storage: 20 GB gp3 EBS volume (default is fine; increase if you work with large codebases)
- Click Launch Instance
Launch via AWS CLI
Replace ami-0c7217cdde317cfec with the current Ubuntu 24.04 AMI ID for your region.
Step 2 — Configure the Security Group
Create a security group that allows only SSH from your IP address:
Do not open port 18789 in the security group. OpenClaw is accessed through an SSH tunnel or SSM, never directly over the internet.
If using SSM Session Manager (no SSH needed)
You can remove the SSH rule entirely and use SSM for all access. This requires:
- An IAM instance profile with the
AmazonSSMManagedInstanceCorepolicy attached - The SSM agent installed on the instance (included by default in Ubuntu 24.04 AMIs)
Step 3 — Connect and Set Up the Server
SSH into the instance:
Update packages:
Add swap space (recommended for t3.small)
Step 4 — Install Docker
Log out and back in for the group change to take effect, then verify:
Step 5 — Configure OpenClaw
Create the directory structure:
Generate secrets:
Save these values securely. Consider storing them in AWS Secrets Manager for production use.
Create the environment file:
Replace the placeholder values with the secrets you generated.
Step 6 — Write the Docker Compose File
The logging configuration prevents Docker logs from filling your EBS volume over time.
Step 7 — Start OpenClaw
Confirm the gateway is listening on port 18789.
Step 8 — Secure Remote Access
Option A: SSH Tunnel
On your local machine:
Access at http://127.0.0.1:18789/.
Option B: SSM Session Manager Port Forwarding
If you configured SSM, you can forward the port without SSH:
This is the most secure option — it does not require any inbound ports open in the security group, and access is controlled entirely through IAM policies.
IAM Best Practices
Instance profile
Create a minimal IAM role for the EC2 instance. If OpenClaw only needs to run code locally, the instance profile needs no AWS permissions at all. Only add SSM permissions if you want SSM access:
Access control for SSM
Restrict who can start SSM sessions to the OpenClaw instance:
EBS Volume for Persistence
The root EBS volume persists across instance stops and starts. For additional safety:
Automated snapshots
Enable EBS snapshots via AWS Backup or a simple cron:
Separate data volume (optional)
For larger workspaces, attach a dedicated EBS volume:
Then point OpenClaw's workspace to /data/workspace.
Cost Breakdown
| Component | t3.small | t3.medium |
|---|---|---|
| EC2 on-demand | $15.18/mo | $30.37/mo |
| EBS (20 GB gp3) | $1.60/mo | $1.60/mo |
| Data transfer | ~$0.50/mo | ~$0.50/mo |
| Total | ~$17/mo | ~$32/mo |
To reduce costs:
- Spot instances: Save 60-70% if you can tolerate interruptions (not recommended for persistent gateway use)
- Reserved instances: Save 30-40% with a 1-year commitment
- Savings Plans: Flexible commitment pricing across instance families
- Scheduled stop/start: If you only use OpenClaw during work hours, stop the instance at night to save ~50%
Security Best Practices
- Never expose port 18789 to the internet via security group rules
- Rotate the gateway token periodically and update the
.envfile - Enable CloudTrail to audit API calls and SSM session activity
- Use an Elastic IP only if you need a stable public address for SSH; otherwise, use SSM exclusively
- Enable automatic security updates:
Troubleshooting
Instance runs out of CPU credits (t3 burstable)
If top shows high CPU and the instance becomes unresponsive, you may have exhausted your CPU credit balance. Check in CloudWatch under CPUCreditBalance. Solutions:
- Switch to
t3.mediumfor more baseline performance - Switch to a non-burstable instance like
m6i.largefor sustained workloads - Enable unlimited mode:
aws ec2 modify-instance-credit-specification --instance-credit-specifications InstanceId=i-xxx,CpuCredits=unlimited(charges apply for surplus credits)
Docker daemon won't start
Common cause: insufficient disk space. Check with df -h.
SSM agent not connecting
Verify the instance has an IAM role with SSM permissions and can reach the SSM endpoints (requires internet access or VPC endpoints).
Cannot connect after instance restart
The public IP changes on stop/start unless you have an Elastic IP. Use aws ec2 describe-instances to get the new IP, or switch to SSM which uses the instance ID instead of IP.
Next Steps
- Set up CloudWatch alarms for CPU, memory, and disk usage
- Configure VPC Flow Logs for network audit trails
- Explore using AWS CDK or CloudFormation to codify the entire stack as infrastructure as code
- Consider placing the instance in a private subnet with a NAT gateway for outbound-only internet access