Home / AWS Research / DeepSeek

DeepSeek AI on AWS EC2

Self-Hosted AI Deployment Guide

EC2 Instance Recommendations

GPU Instance (g4dn.xlarge)

  • vCPU: 4
  • RAM: 16GB
  • GPU: NVIDIA T4 (16GB VRAM)
  • Cost: ~$380/month (24/7)

CPU Instance (t3.xlarge)

  • vCPU: 4
  • RAM: 16GB
  • Cost: ~$120/month (24/7)

Auto-Setup Script

#!/bin/bash
# Install Docker and NVIDIA drivers
sudo apt-get update
sudo apt-get install -y docker.io nvidia-driver-535
sudo systemctl enable docker

# Install NVIDIA Container Toolkit
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker

# Deploy Ollama with GPU support
docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama --restart always ollama/ollama

# Install Code-Server
curl -fsSL https://code-server.dev/install.sh | sh
sudo systemctl enable --now code-server@$USER

This script automatically installs all required components including Docker, NVIDIA drivers, and sets up both Ollama and Code-Server for development.

Server Configuration

Nginx Proxy Setup

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:11434;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

SSL Setup

sudo certbot --nginx -d your-domain.com -d code.your-domain.com

Make sure to replace your-domain.com with your actual domain before running these commands.

Monthly Cost Estimates

Component GPU Instance CPU Instance
Instance (12hr/day) $190 $60
EBS Storage (100GB) $10 $10
Data Transfer $5 $5
Total $205 $75

These estimates are based on running the instance 12 hours per day, which can be achieved using the auto-shutdown implementation described below.

Auto-Shutdown Implementation

AWS Lambda Script

import boto3
import datetime

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    instance_id = 'your-instance-id'

    current_hour = datetime.datetime.now().hour
    if current_hour == 2:  # 2 AM shutdown
        ec2.stop_instances(InstanceIds=[instance_id])

    return {'statusCode': 200}

EC2 Cron Solution

0 2 * * * /usr/sbin/shutdown -h now

Setting up auto-shutdown can significantly reduce your costs by ensuring the instance is only running when needed.

Implementation Checklist

Component Status
EC2 Instance Setup ✅ Ready
Automated Deployment ✅ Script Provided
SSL Configuration ✅ Certbot Integrated
Cost Management ✅ Lambda + Cron

Important Note

Remember to replace placeholders (your-domain.com, your-instance-id) with your actual values before deployment.

Looking for Other Resources?

Check out my other AWS research pages: