NOTE: I have given up on using AWS EC2 instances with VS Code.
I find they timeout after 4 hours. Then it is impossible to get back into the server even after multiple restarts.
Too much pain (15 hours) so I am now using Azure. Check my other repo: https://github.com/littleredshack/azure_resources
- EC2 instances for development only
- VS Code Tunnels for browser-based development (much more reliable than Remote-SSH)
- Phased setup approach with verification at each step
- Automatic SSH configuration for emergency/admin access
- Complete resource cleanup when removing instances
- IP-restricted security (only your IP can access)
You'll need your current public IP address for security configuration:
# Method 1: Using curl (sometimes returns hex format)
curl ifconfig.me
# Method 2: Using a different service
curl ipinfo.io/ip
# Method 3: Check via web browser
# Visit: https://whatsmyip.com or https://ipinfo.ioNote: If curl ifconfig.me returns a hex value instead of normal IP format (like 200.5.49.46), use one of the other methods or the web browser option.
Important: Only this IP address will be able to SSH to your instance for security.
❌ What Crashes Instances:
- VS Code Remote-SSH on t2.micro - consistently crashes after 2-3 hours
- Heavy user data scripts - cause dpkg errors and package conflicts during boot
- Auto-installing Docker/Node.js - overwhelms 1GB RAM instances
✅ What Actually Works:
- VS Code Tunnels - browser-based, stable, full functionality
- Minimal user data - only essential tools (git, vim, htop), fast reliable boot
- Post-creation setup - install tools after basic connectivity verified
- Phased verification - test each step before proceeding, catch failures early
1. Create instance:
curl ifconfig.me
./ec2_ssh_only_setup_v2.sh --region eu-west-1 --my-ip YOUR_IP --name "my-dev"2. Alternative - Full automatic setup:
# Everything automated in one command (includes persistent tunnel)
./ec2_ssh_only_setup_v2.sh --region eu-west-1 --my-ip YOUR_IP --name "dev-server" --key-name "dev-$(date +%Y%m%d%H%M)" --post-install3. Open tunnel URL in browser:
- Full VS Code interface
- Integrated terminal
- No crashes, no Remote-SSH issues
ec2_ssh_only_setup_v2.sh- Main instance creation with phased approach (t2.small default)setup_tunnel_service.sh- Sets up persistent VS Code tunnel servicelist_ec2_instances.sh- List instances across regionsremove_ec2_instances.sh- Complete cleanup of instances and resourceslist_all_aws_resources.sh- Comprehensive AWS resource inventorymonitor_instance.sh- Real-time monitoring (prevents crashes)ec2_vscode_setup_guide.md- Detailed setup guide with troubleshootingREADME.md- This file
# List instances
./list_ec2_instances.sh eu-west-1
# Monitor instance health (prevent crashes)
./monitor_instance.sh stable-dev
# Complete cleanup when done
./remove_ec2_instances.sh --region eu-west-1 i-instance-id
# List all AWS resources
./list_all_aws_resources.sh eu-west-1🎯 PHASE 1: Create Instance
- Minimal Ubuntu 22.04 with essential tools only
- No heavy packages that cause conflicts
🔧 PHASE 2: Configure SSH
- Automatic SSH config generation
- Backup of existing config
🧪 PHASE 3: Test SSH (CRITICAL)
- Verifies connectivity before proceeding
- Stops if SSH fails - no broken setups
🚀 PHASE 4: Install VS Code
- Only after SSH verified working
- Uses snap for reliability
🛠️ PHASE 5: Post-Install (Optional)
- Node.js, Claude CLI, development tools
- Only with --post-install flag
⚙️ PHASE 6: Persistent Tunnel
- Systemd service for auto-start
- Survives reboots, auto-restarts
- Microsoft/GitHub authentication required - anonymous users can't access
- Account-based access control - only authenticated users can connect
- Encrypted tunnel connection - all traffic is secured end-to-end
- Anyone with the URL can attempt to connect
- Shared/compromised accounts could access your environment
- No IP restrictions on the tunnel URL itself (bypasses EC2 security groups)
- URL acts like a password - treat it as sensitive information
1. Use Strong Authentication:
# Use dedicated account for development
# - Create separate GitHub account for coding projects
# - Use strong, unique password + 2FA
# - Don't share account credentials2. Monitor Access:
# Check tunnel connection logs
ssh your-instance "sudo journalctl -u vscode-tunnel.service | grep -i 'connect\|auth'"
# Monitor active connections
ssh your-instance "sudo netstat -an | grep ESTABLISHED"
# In VS Code: View → Command Palette → "Remote-Tunnels: Show Log"3. Rotate Tunnel URLs:
# Get new tunnel URL by restarting service
ssh your-instance "sudo systemctl restart vscode-tunnel.service"
# Or using management script
./setup_tunnel_service.sh your-instance --restart
./setup_tunnel_service.sh your-instance --url4. Instance-Level Security:
# Your EC2 instance is IP-restricted for SSH
# But tunnel traffic goes through Microsoft's servers
# Additional monitoring recommended:
./monitor_instance.sh your-instance- ✅ Never share tunnel URLs in git repos, chat, or public forums
- ✅ Use dedicated GitHub/Microsoft account for development only
- ✅ Enable 2FA on your authentication account
- ✅ Monitor tunnel logs for unexpected connections
- ✅ Restart tunnel periodically to invalidate old URLs
- ✅ Keep sensitive data secure (use env vars for API keys)
- ✅ Don't store credentials in plain text files
- ✅ Use private/incognito browser for tunnel access
# Monitor for unusual activity
./monitor_instance.sh your-instance logs
# Check who's accessing your files
ssh your-instance "sudo auditctl -w /home/ubuntu -p rwxa"
# Monitor system resources for unexpected usage
ssh your-instance "htop"Consider these alternatives for maximum security:
- Local VS Code with SSH (despite Remote-SSH stability issues)
- VPN to your instance instead of public tunnels
- Bastion host setup with additional network security layers
- Private tunnel services (more complex but more secure)
Remember: Treat your tunnel URL like a password - never share it publicly! 🔐
Your Browser → VS Code Tunnel → EC2 Instance
↓
Local SSH (backup/admin access)
- VS Code Tunnels provide browser-based development
- Direct SSH for administration and setup
- No complex proxies or middleware
- IP-restricted security - only your IP can connect
Free Tier Eligible:
- t2.small: ~$17/month (much more reliable than "free" t2.micro that constantly crashes)
Cost Optimization:
- Stop instances when not in use
- Use remove script for complete cleanup
- "Permission denied" - Check IP:
curl ifconfig.me, update security group - "Connection refused" - Wait 2-3 minutes for instance boot
- Tunnel auth fails - Use incognito browser window
- Instance crashes - You probably used Remote-SSH (use tunnels instead!)
See the complete troubleshooting guide for solutions.
Found an issue? This guide is based on real testing and troubleshooting. Please open issues for:
- Script improvements
- Additional troubleshooting scenarios
- Cost optimization tips
- Security enhancements
MIT License - use and modify as needed.
Battle-tested and reliable! This approach has been thoroughly tested and documented based on real usage and troubleshooting. 🚀