Docs / Use Cases / EC2 Docker Instances
Diagram: an EC2 instance backed by a real Docker container, reachable via localemu ssh, SSH through EC2 Instance Connect, or SSM Send-Command.

EC2: demo & walkthrough

Launch real Docker containers as EC2 instances. SSH in, run user data scripts, manage instance lifecycle. No AWS account needed, no VMs, just containers that behave like EC2 instances.

Looking for the API surface and CLI examples? See EC2: API reference. The commands below use the real AWS CLI with --endpoint-url; LocalEmu also ships awsemu, a zero-config wrapper that sets it for you.

What the demo does. Creates an RSA key pair and downloads the PEM, creates a security group with an SSH ingress rule on port 22, launches a t2.micro Ubuntu 22.04 instance with a user-data script that writes /tmp/hello.txt at boot, waits for the instance to reach running, then reads the user-data output back with docker exec (the read-only equivalent of localemu ssh, which needs a real terminal), connects with real SSH from an in-VPC client container, pushes a 60-second ephemeral key with EC2 Instance Connect, runs a command keylessly with aws ssm send-command, then exercises the lifecycle: stop-instances -> start-instances -> terminate-instances, verifying the Docker container state at each step. Instances boot from the pre-baked localemu/ec2-base image with openssh-server already installed, so sshd is reachable in ~1 second. Requires EC2_VM_MANAGER=docker on the LocalEmu host. Source: 15-ec2-demo/ in the examples repo.
🖥

Real SSH Access

SSH into instances with real key pairs. Use standard ssh or the localemu ssh shortcut.

📦

SSM Send-Command

Run commands keylessly via the real aws ssm send-command / get-command-invocation path, no SSH keys needed.

🧰

User Data Scripts

Bootstrap scripts run at boot. Install packages, write files, configure services.

🛡

Security Groups

Create and attach security groups with ingress rules. Full API compatibility.

⚙

IMDS Metadata

Instance Metadata Service available inside containers. Query instance identity like real EC2.

Step-by-Step Walkthrough

Step 1: Start LocalEmu with EC2 Docker backend

Terminal
EC2_VM_MANAGER=docker localemu start

The EC2_VM_MANAGER=docker flag tells LocalEmu to create real Docker containers when you launch EC2 instances.

Step 2: Create an SSH key pair

Terminal
$ aws --endpoint-url=http://localhost:4566 ec2 create-key-pair --key-name my-key \
    --query KeyMaterial --output text > /tmp/my-key.pem

$ chmod 400 /tmp/my-key.pem

LocalEmu generates a real RSA key pair. The private key is saved to a file, and the public key is injected into instances that reference this key name.

Step 3: Create a security group with SSH access

Terminal
$ SG_ID=$(aws --endpoint-url=http://localhost:4566 ec2 create-security-group \
    --group-name ssh-open \
    --description "SSH open" \
    --query 'GroupId' --output text)

$ echo $SG_ID
sg-df9813973ad922ec6

$ aws --endpoint-url=http://localhost:4566 ec2 authorize-security-group-ingress \
    --group-id $SG_ID --protocol tcp --port 22 --cidr 0.0.0.0/0

Return: true
SecurityGroupRules:
  - SecurityGroupRuleId: sgr-ee7274d05c857c4c3
    GroupId: sg-df9813973ad922ec6
    IpProtocol: tcp
    FromPort: 22
    ToPort: 22
    CidrIpv4: 0.0.0.0/0

Security groups are created through the standard EC2 API. Ingress rules control which ports are accessible.

Step 4: Launch an instance with key pair, security group, and user data

Terminal
$ aws --endpoint-url=http://localhost:4566 ec2 run-instances \
    --image-id ami-ubuntu-22.04 \
    --instance-type t2.micro \
    --count 1 \
    --key-name my-key \
    --security-group-ids $SG_ID \
    --user-data '#!/bin/bash
  echo "Hello from LocalEmu EC2" > /tmp/hello.txt
  date > /tmp/boot-time.txt'

InstanceId: i-345e9b5f68e616e47
ImageId: ami-ubuntu-22.04
InstanceType: t2.micro
State: pending
KeyName: my-key
SecurityGroups:
  - GroupId: sg-df9813973ad922ec6
    GroupName: ssh-open
PrivateIpAddress: 10.43.9.71

LocalEmu boots the localemu/ec2-base:v4 pre-baked image (Ubuntu + ubuntu user + openssh-server + awscli + tooling), starts a container, injects the SSH public key into both ubuntu's and root's authorized_keys, and runs the user data script.

Step 5: Describe the running instance

Terminal
$ aws --endpoint-url=http://localhost:4566 ec2 describe-instances --instance-ids i-345e9b5f68e616e47

InstanceId: i-345e9b5f68e616e47
State: running
PrivateIpAddress: 10.43.9.71
KeyName: my-key
InstanceType: t2.micro
ImageId: ami-ubuntu-22.04
AvailabilityZone: us-east-1a

This instance has no public-facing address, so PublicIpAddress and PublicDnsName are both omitted, matching the real AWS contract. PrivateIpAddress is what you connect to (see Step 11).

Step 6: Verify the Docker container is running

Terminal
$ docker ps --filter "label=localemu.service=ec2"

CONTAINER ID   IMAGE                  COMMAND                     STATUS          NAMES
ee1fb33a5530   localemu/ec2-base:v4   "sh -c '#!/bin/sh\nse..."   Up 45 seconds   localemu-ec2-i-345e9b5f68e616e47

# No published host ports -- the container gets a real IP on the
# VPC bridge instead of a Docker port mapping.

Step 7: List SSH-accessible instances

Terminal
$ localemu ssh --list

NAMES                              STATUS
localemu-ec2-i-345e9b5f68e616e47   Up About a minute

Step 8: Verify user data ran successfully

Terminal
$ docker exec localemu-ec2-i-345e9b5f68e616e47 cat /tmp/hello.txt
Hello from LocalEmu EC2

The user data script wrote "Hello from LocalEmu EC2" to /tmp/hello.txt at boot time. localemu ssh <id> <cmd> always attaches a TTY (docker exec -it), so it errors with "cannot attach stdin to a TTY-enabled container" outside a real terminal. This read-only check therefore uses plain docker exec directly, the same thing localemu ssh <id> <cmd> does under the hood minus the TTY requirement. Run localemu ssh i-345e9b5f68e616e47 yourself in a real terminal for an interactive shell.

Step 9: Second read-only check via docker exec

Terminal
$ docker exec localemu-ec2-i-345e9b5f68e616e47 whoami
root

Runs as root inside the container, since the SSH public key is materialised into /root/.ssh/authorized_keys (see "How It Works" below). The OS version check happens over real SSH in the next step.

Step 10: Connect with standard SSH using your key pair, from an in-VPC client container

Terminal
# LocalEmu does not open host-to-VPC-private-IP reachability on macOS
# by design, so SSH has to come from a container on the same VPC
# network -- the in-VPC client container pattern:
$ docker run -d --name bastion --network $(docker inspect \
    --format '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}{{"\n"}}{{end}}' \
    localemu-ec2-i-345e9b5f68e616e47 | grep localemu-vpc) \
    --entrypoint sh localemu/ec2-base:v4 -c 'sleep 3600'
$ docker cp /tmp/my-key.pem bastion:/tmp/my-key.pem
$ docker exec bastion sh -c \
    "chmod 400 /tmp/my-key.pem && ssh -i /tmp/my-key.pem ubuntu@10.43.9.71 whoami"
ubuntu

$ docker exec bastion sh -c \
    "ssh -i /tmp/my-key.pem ubuntu@10.43.9.71 cat /etc/os-release" | head -5
PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy

Standard OpenSSH works, using the instance's PrivateIpAddress from a container on the same VPC network (see EC2: API reference for why the host itself can't reach it directly on macOS).

Step 11: Push a 60-second ephemeral SSH key with EC2 Instance Connect

Terminal
$ ssh-keygen -t ed25519 -f /tmp/eic-key -N ""
$ aws ec2-instance-connect send-ssh-public-key \
    --instance-id i-345e9b5f68e616e47 \
    --instance-os-user ubuntu \
    --ssh-public-key file:///tmp/eic-key.pub

{"RequestId": "...", "Success": true}

# valid for 60 seconds, from a container on the same VPC network:
$ ssh -i /tmp/eic-key ubuntu@10.43.9.71 whoami
ubuntu

send-ssh-public-key writes the key into authorized_keys immediately, and it's automatically removed 60 seconds later, matching AWS's own TTL exactly.

Step 12: Run a command with SSM Send-Command, the keyless AWS-native path

Terminal
$ CMD_ID=$(aws ssm send-command \
    --document-name AWS-RunShellScript \
    --instance-ids i-345e9b5f68e616e47 \
    --parameters 'commands=["cat /tmp/hello.txt"]' \
    --query 'Command.CommandId' --output text)

$ aws ssm get-command-invocation \
    --command-id $CMD_ID --instance-id i-345e9b5f68e616e47 \
    --query 'Status' --output text
Success

$ aws ssm get-command-invocation \
    --command-id $CMD_ID --instance-id i-345e9b5f68e616e47 \
    --query 'StandardOutputContent' --output text
Hello from LocalEmu EC2

ssm send-command queues a shell command without needing SSH keys or network access to the instance at all. The result is polled back with get-command-invocation.

Step 13: Stop and restart the instance

Terminal
$ aws --endpoint-url=http://localhost:4566 ec2 stop-instances --instance-ids i-345e9b5f68e616e47

InstanceId: i-345e9b5f68e616e47
CurrentState: stopping
PreviousState: running

$ docker ps --filter "label=localemu.service=ec2"
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    NAMES
(empty - container stopped)

$ aws --endpoint-url=http://localhost:4566 ec2 start-instances --instance-ids i-345e9b5f68e616e47

InstanceId: i-345e9b5f68e616e47
CurrentState: pending
PreviousState: stopped

$ docker ps --filter "label=localemu.service=ec2"
CONTAINER ID   IMAGE                  COMMAND                     STATUS          NAMES
ee1fb33a5530   localemu/ec2-base:v4   "sh -c '#!/bin/sh\nse..."   Up 13 seconds   localemu-ec2-i-345e9b5f68e616e47

Stopping an instance stops the Docker container. Starting it again brings the same container back. Full lifecycle management.

Step 14: Terminate and cleanup

Terminal
$ aws --endpoint-url=http://localhost:4566 ec2 terminate-instances --instance-ids i-345e9b5f68e616e47

InstanceId: i-345e9b5f68e616e47
CurrentState: shutting-down
PreviousState: running

$ docker ps --filter "label=localemu.service=ec2"
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
(empty - container removed)

Terminating removes the Docker container completely. No orphaned containers, no leftover resources.

Supported AMI Images

AMI ID Docker Image Description
(default for every AMI ID)localemu/ec2-base:v4Pre-baked Ubuntu base with openssh-server, iptables, curl, awscli, and other net tooling. Built once on first RunInstances call.

How It Works

1. ec2 run-instances reaches the EC2 API on LocalEmu, which records the instance, security group, key pair and tags in its in-memory EC2 metadata store.
2. That metadata is the input to the real behavior layer: the DockerVmManager picks up the new instance, ensures the localemu/ec2-base image exists (built once on first run), and starts a container on the right Docker network for the target VPC.
3. The SSH public key from the named key pair is materialised into /root/.ssh/authorized_keys inside the container, so the private PEM on your host actually opens the door.
4. The user-data script is dropped into /var/lib/cloud/instance/scripts/ and executed at boot before sshd is reachable.
5. The container gets a real IP on the VPC bridge network (recorded as PrivateIpAddress) rather than a Docker-published host port, matching how a real EC2 instance is reachable inside its VPC.
6. An IMDS sidecar (per VPC) answers http://169.254.169.254/ from inside the container via an iptables DNAT rule, so tooling that hardcodes the link-local IMDS address (boto3, awscli, the AWS SDKs) finds credentials and instance identity exactly where it expects.
7. stop-instances stops the container, start-instances brings the same container back, terminate-instances removes it. The EC2 metadata record stays in sync with the container's lifecycle.