# Amazon EC2

Connect your own AWS account so Sandcovery and Sandcovery CI can launch ephemeral Amazon EC2 virtual machines over AWS Systems Manager with no open inbound ports.

Sandcovery runs each sandbox or Sandcovery CI runner as a dedicated Amazon EC2 Nitro virtual machine in your own AWS account, using the latest official AWS ECS-optimized Amazon Linux 2023 AMI. That AMI ships with both Docker and AWS Systems Manager Agent enabled from boot, so Sandcovery reaches each instance outbound over HTTPS through AWS Systems Manager with zero open inbound security group ports.

## What runs

| Option | Support |
| --- | --- |
| Sizes | `2x4`, `2x8`, `4x8`, `4x16`, `8x16`, `8x32`, `16x32`, `16x64`, `32x64`, `32x128` |
| Instance types | `c7i`/`c6i`/`c7a`/`c5` for 1:2 sizes; `m7i`/`m6i`/`m7a`/`m5` for 1:4 sizes |
| Capacity | Spot with On-Demand fallback (default), On-Demand only, or Spot only |
| Root disk | 30 GiB to 16,384 GiB gp3 EBS (defaults to 60 GiB, deleted on termination) |
| Docker & CI | Yes; native Linux kernel with Docker, Compose, Buildx, and k3s support |
| Egress | Account VPC default (`allow_all`) |
| Command timeout | Up to 3,600 seconds |
| Regions | 13 AWS commercial Regions across `us`, `eu`, and `asia` |

Every instance enforces IMDSv2 (`HttpTokens=required`), arms an in-VM TTL poweroff watchdog with `InstanceInitiatedShutdownBehavior=terminate`, and generates an ephemeral 2048-bit RSA key pair in `/run/sandcovery` (`tmpfs` RAM) at boot. File writes, including Sandcovery CI's single-use runner config, are encrypted to that in-RAM public key with RSA-OAEP-SHA256, AES-256-CTR, and HMAC-SHA256 before crossing Systems Manager, so plaintext file contents never appear in Systems Manager command history or CloudTrail.

## Create the IAM instance profile

Each EC2 instance needs an IAM role and instance profile carrying `AmazonSSMManagedInstanceCore` so SSM Agent can register without inbound ports:

```sh
aws iam create-role \
  --role-name sandcovery-ec2-instance \
  --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}'

aws iam attach-role-policy \
  --role-name sandcovery-ec2-instance \
  --policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore

aws iam create-instance-profile --instance-profile-name sandcovery-ec2-instance
aws iam add-role-to-instance-profile \
  --instance-profile-name sandcovery-ec2-instance \
  --role-name sandcovery-ec2-instance
```

## Create the access key

Create an IAM user with this policy, then create an access key for it. Replace `<account>` with your 12-digit AWS account ID.

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["ec2:RunInstances", "ec2:DescribeInstances", "ec2:CreateTags"],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "ec2:TerminateInstances",
      "Resource": "arn:aws:ec2:*:<account>:instance/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/sandcovery-managed": "true"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::<account>:role/sandcovery-ec2-instance"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ssm:GetParameter",
        "ssm:DescribeInstanceInformation",
        "ssm:SendCommand",
        "ssm:GetCommandInvocation"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "sts:GetCallerIdentity",
      "Resource": "*"
    }
  ]
}
```

## Add the credential

In **Provider Credentials** in the [dashboard](/app/credentials), add Amazon EC2 with:

- Access key ID
- Secret access key
- IAM instance profile name or ARN (`sandcovery-ec2-instance`)
- Region (default `us-west-2`)
- Capacity (default Spot with On-Demand fallback)

On Pro and Enterprise plans, you can add multiple Amazon EC2 credentials pointing to different Regions. When one Region hits an On-Demand or Spot vCPU quota or capacity limit, Sandcovery automatically fails over to your next configured Region and then to other connected providers.

## Costs

AWS bills EC2 compute and gp3 EBS root volumes directly to your AWS account. Sandcovery's estimates and spend caps count US West (Oregon) `c7i`/`m7i` On-Demand Linux list prices ($0.03885 per vCPUh and $0.002888 per GiB-hour, e.g., $0.1785/h for `4x8` and $0.2016/h for `4x16`), so Spot runs and `c6i`/`m6i` fallbacks cost less than the estimate.
