feat(aws): EC2 launch type setting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-16 12:54:24 -08:00
1 parent edef77feed
commit 86f253b7da
6 files changed
+225 -22

No files matched your search

@@ -1248,6 +1248,18 @@ fn validate_aws_update(
crate::models::validate_region(&place.region)?;
crate::aws::tofu_modules::validate_environment_name(&place.environment)?;
crate::models::validate_aws_workspace(cfg)?;
// Docker access and VPN need the user's own instance (E6, E7);
// Fargate refuses them. Grandfather a value the stored project
// already had on AWS so an old row can still be saved.
if cfg.launch_type != crate::models::LaunchType::Ec2 {
let was_aws = stored.aws.is_some();
if project.allow_docker_access && !(was_aws && stored.allow_docker_access) {
return Err("Docker access on AWS needs the EC2 launch type".into());
}
if project.vpn_support_enabled && !(was_aws && stored.vpn_support_enabled) {
return Err("VPN support on AWS needs the EC2 launch type".into());
}
}
// Q6: forwarded over loopback, TCP only, never a reserved port
// (rows the save adds or edits; everything when newly on AWS).
crate::host::port_forwards::validate_aws_port_mappings_update(
@@ -2896,6 +2908,26 @@ mod tests {
assert!(validate_gateway_sidecar_update(&off, &off, &half).is_ok());
}
#[test]
fn docker_and_vpn_on_aws_need_the_ec2_launch_type() {
use crate::models::LaunchType;
let hosts = vec![host("h1")];
let stored = aws_project();
for (docker, vpn) in [(true, false), (false, true)] {
let mut p = stored.clone();
p.allow_docker_access = docker;
p.vpn_support_enabled = vpn;
assert!(validate_aws_update(&stored, &p, &hosts).is_err());
// A value the AWS project already had is grandfathered.
let mut old = stored.clone();
old.allow_docker_access = docker;
old.vpn_support_enabled = vpn;
assert!(validate_aws_update(&old, &old, &hosts).is_ok());
p.aws.as_mut().unwrap().launch_type = LaunchType::Ec2;
assert!(validate_aws_update(&stored, &p, &hosts).is_ok());
}
}
#[test]
fn a_stored_workspace_access_point_blocks_moving_the_host() {
let hosts = vec![host("h1"), host("h2")];
+164 -16
View File
@@ -521,8 +521,20 @@ fn default_aws_ephemeral_gib() -> u32 {
21
}
/// Where an AWS workspace's task runs.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LaunchType {
#[default]
Fargate,
Ec2,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AwsWorkspaceConfig {
/// Fargate (default; absent in old JSON) or the user's own EC2 instances.
#[serde(default)]
pub launch_type: LaunchType,
#[serde(default = "default_aws_cpu")]
pub cpu: u32,
#[serde(default = "default_aws_memory_mib")]
@@ -568,6 +580,7 @@ pub const MAX_SETUP_SCRIPT_BYTES: usize = 32_768;
impl Default for AwsWorkspaceConfig {
fn default() -> Self {
Self {
launch_type: LaunchType::default(),
cpu: default_aws_cpu(),
memory_mib: default_aws_memory_mib(),
arch: Arch::default(),
@@ -700,23 +713,101 @@ fn describe_sizes(sizes: &[u32]) -> String {
format!("{} to {} MiB in steps of {}", first, last, sizes[1] - sizes[0])
}
pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
let sizes = fargate_memory_sizes(cfg.cpu).ok_or_else(|| {
format!(
"{} is not a Fargate CPU size (256, 512, 1024, 2048, 4096, 8192 or 16384)",
cfg.cpu
)
})?;
if !sizes.contains(&cfg.memory_mib) {
return Err(format!(
"{} MiB of memory is not valid with {} CPU units: use {}",
cfg.memory_mib,
cfg.cpu,
describe_sizes(&sizes)
));
/// CPU units and memory (MiB) held back on an EC2 instance for the ECS agent
/// and the OS.
pub const EC2_RESERVED_CPU_UNITS: u32 = 128;
pub const EC2_RESERVED_MEMORY_MIB: u32 = 256;
/// `(vcpu * 1024, memory MiB)` of a common EC2 instance type, or None when the
/// type is not in the built-in table (m7i, m7g, c7i, c7g, r7i, r7g, t3, t4g).
pub fn ec2_instance_spec(instance_type: &str) -> Option<(u32, u32)> {
let (family, size) = instance_type.split_once('.')?;
let vcpu: u32 = match size {
"large" => 2,
"xlarge" => 4,
"2xlarge" => 8,
"4xlarge" => 16,
_ => return None,
};
// GiB per vCPU: general purpose 4, compute 2, memory 8.
let gib_per_vcpu: u32 = match family {
"m7i" | "m7g" => 4,
"c7i" | "c7g" => 2,
"r7i" | "r7g" => 8,
"t3" | "t4g" => {
// Burstable: 2 vCPU with 8 GiB (large), then no 4xlarge exists.
return match size {
"large" => Some((2048, 8192)),
"xlarge" => Some((4096, 16384)),
"2xlarge" => Some((8192, 32768)),
_ => None,
};
}
_ => return None,
};
Some((vcpu * 1024, vcpu * gib_per_vcpu * 1024))
}
/// Whether an EC2 workspace's reservations fit an instance type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Fit {
Ok,
/// The reservations exceed what the instance leaves for tasks.
TooBig { available_cpu: u32, available_memory_mib: u32 },
/// The type is not in the table: the caller shows a "must fit" warning.
UnknownType,
}
/// Check an EC2 config's reservations against `instance_type`, less the agent
/// and OS reserve. A Fargate config always fits (no instance involved).
pub fn fits_instance(cfg: &AwsWorkspaceConfig, instance_type: &str) -> Fit {
if cfg.launch_type != LaunchType::Ec2 {
return Fit::Ok;
}
if !(21..=200).contains(&cfg.ephemeral_gib) {
return Err("Ephemeral storage must be 21 to 200 GiB".into());
let Some((cpu, mem)) = ec2_instance_spec(instance_type) else {
return Fit::UnknownType;
};
let available_cpu = cpu - EC2_RESERVED_CPU_UNITS;
let available_memory_mib = mem - EC2_RESERVED_MEMORY_MIB;
if cfg.cpu > available_cpu || cfg.memory_mib > available_memory_mib {
Fit::TooBig { available_cpu, available_memory_mib }
} else {
Fit::Ok
}
}
pub fn validate_aws_workspace(cfg: &AwsWorkspaceConfig) -> Result<(), String> {
match cfg.launch_type {
LaunchType::Fargate => {
let sizes = fargate_memory_sizes(cfg.cpu).ok_or_else(|| {
format!(
"{} is not a Fargate CPU size (256, 512, 1024, 2048, 4096, 8192 or 16384)",
cfg.cpu
)
})?;
if !sizes.contains(&cfg.memory_mib) {
return Err(format!(
"{} MiB of memory is not valid with {} CPU units: use {}",
cfg.memory_mib,
cfg.cpu,
describe_sizes(&sizes)
));
}
if !(21..=200).contains(&cfg.ephemeral_gib) {
return Err("Ephemeral storage must be 21 to 200 GiB".into());
}
}
LaunchType::Ec2 => {
// Reservations on the user's instance; the instance type is not on
// the project, so `fits_instance` does the fit check later.
if cfg.cpu == 0 || cfg.memory_mib == 0 {
return Err("An EC2 workspace needs a positive CPU and memory reservation".into());
}
if cfg.spot {
return Err("Spot is not available on EC2 yet".into());
}
}
}
if let Some(m) = cfg.idle_stop_minutes {
if !(5..=1440).contains(&m) {
@@ -1436,6 +1527,63 @@ mod tests {
assert!(p.is_aws());
}
fn ec2(cpu: u32, mem: u32) -> AwsWorkspaceConfig {
AwsWorkspaceConfig { launch_type: LaunchType::Ec2, cpu, memory_mib: mem, ..Default::default() }
}
#[test]
fn old_json_is_fargate_and_ec2_round_trips() {
let c: AwsWorkspaceConfig = serde_json::from_str("{}").unwrap();
assert_eq!(c.launch_type, LaunchType::Fargate);
let e: AwsWorkspaceConfig = serde_json::from_str(r#"{"launch_type":"ec2"}"#).unwrap();
assert_eq!(e.launch_type, LaunchType::Ec2);
assert!(serde_json::to_string(&e).unwrap().contains(r#""launch_type":"ec2""#));
}
#[test]
fn ec2_validation_shape() {
// Reservations need not be a Fargate pair.
assert!(validate_aws_workspace(&ec2(1500, 3000)).is_ok());
assert!(validate_aws_workspace(&ec2(0, 3000)).is_err());
assert!(validate_aws_workspace(&ec2(1024, 0)).is_err());
let mut s = ec2(1024, 2048);
s.spot = true;
assert!(validate_aws_workspace(&s).unwrap_err().contains("Spot"));
// Ephemeral size is ignored on EC2.
let mut e = ec2(1024, 2048);
e.ephemeral_gib = 5;
assert!(validate_aws_workspace(&e).is_ok());
// Fargate still rejects the non-pair.
assert!(validate_aws_workspace(&cfg(1500, 3000)).is_err());
}
#[test]
fn instance_table_values() {
assert_eq!(ec2_instance_spec("m7i.large"), Some((2048, 8192)));
assert_eq!(ec2_instance_spec("m7g.4xlarge"), Some((16384, 65536)));
assert_eq!(ec2_instance_spec("c7i.xlarge"), Some((4096, 8192)));
assert_eq!(ec2_instance_spec("r7g.2xlarge"), Some((8192, 65536)));
assert_eq!(ec2_instance_spec("t3.large"), Some((2048, 8192)));
assert_eq!(ec2_instance_spec("t4g.2xlarge"), Some((8192, 32768)));
assert_eq!(ec2_instance_spec("t3.4xlarge"), None);
assert_eq!(ec2_instance_spec("p5.48xlarge"), None);
assert_eq!(ec2_instance_spec("m7i"), None);
}
#[test]
fn fit_checks_reserve_for_the_agent() {
// m7i.large: 2048 / 8192 less 128 / 256.
assert_eq!(fits_instance(&ec2(1920, 7936), "m7i.large"), Fit::Ok);
assert_eq!(
fits_instance(&ec2(1921, 4096), "m7i.large"),
Fit::TooBig { available_cpu: 1920, available_memory_mib: 7936 }
);
assert!(matches!(fits_instance(&ec2(1024, 7937), "m7i.large"), Fit::TooBig { .. }));
assert_eq!(fits_instance(&ec2(1024, 2048), "z9.huge"), Fit::UnknownType);
// Fargate configs have no instance to fit.
assert_eq!(fits_instance(&cfg(16384, 122880), "m7i.large"), Fit::Ok);
}
#[test]
fn cpu_and_memory_must_be_a_fargate_pair() {
// Shared with the frontend's copy (`lib/awsWorkspace.test.ts`).
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import RuntimeSection from "./RuntimeSection";
import type { AuthBridgeStatus, Project } from "../../../../lib/types";
import { awsProject } from "../../../../test/awsFixtures";
// The auth-bridge row owns its own IPC — see `AuthBridgeRow.tsx` for why it
// does not go through `save`.
@@ -181,3 +182,18 @@ describe("RuntimeSection — auth bridge toggle", () => {
expect(save).not.toHaveBeenCalled();
});
});
describe("RuntimeSection — Docker and VPN on AWS follow the launch type", () => {
beforeEach(() => vi.clearAllMocks());
it("are off and disabled on Fargate, switchable on EC2", () => {
const fargate = awsProject();
const { unmount } = renderSection(fargate);
expect(screen.getByRole("switch", { name: VPN })).toBeDisabled();
expect(screen.getByRole("switch", { name: "Allow container spawning" })).toBeDisabled();
unmount();
renderSection({ ...fargate, aws: { ...fargate.aws!, launch_type: "ec2" } });
expect(screen.getByRole("switch", { name: VPN })).toBeEnabled();
expect(screen.getByRole("switch", { name: "Allow container spawning" })).toBeEnabled();
});
});
@@ -21,6 +21,8 @@ export default function RuntimeSection({
disabledReason,
}: Props) {
const aws = isAwsProject(project);
// Docker access and VPN need the user's own EC2 instance (not Fargate).
const noPrivileged = aws && project.aws?.launch_type !== "ec2";
return (
<>
<ConfigGroup
@@ -54,15 +56,15 @@ export default function RuntimeSection({
<SwitchRow
label="Allow container spawning"
hint={
aws
noPrivileged
? NOT_ON_AWS
: "Mounts the Docker socket so Claude can build and run Docker containers from inside the sandbox."
}
control={
<Toggle
label="Allow container spawning"
checked={aws ? false : project.allow_docker_access}
disabled={disabled || aws}
checked={noPrivileged ? false : project.allow_docker_access}
disabled={disabled || noPrivileged}
onChange={(v) => save({ allow_docker_access: v })}
/>
}
@@ -70,12 +72,12 @@ export default function RuntimeSection({
<SwitchRow
label="VPN support"
hint={aws ? NOT_ON_AWS : "Grants NET_ADMIN and the /dev/net/tun device so a VPN client (PIA, WireGuard, OpenVPN) can build a tunnel inside the container. Without it a client installs and runs but its connection hangs until it times out. Anything in the container can then reconfigure the container's own network stack; the host's is untouched. Changing this recreates the container on its next start — the home and .claude volumes are preserved."}
hint={noPrivileged ? NOT_ON_AWS : "Grants NET_ADMIN and the /dev/net/tun device so a VPN client (PIA, WireGuard, OpenVPN) can build a tunnel inside the container. Without it a client installs and runs but its connection hangs until it times out. Anything in the container can then reconfigure the container's own network stack; the host's is untouched. Changing this recreates the container on its next start — the home and .claude volumes are preserved."}
control={
<Toggle
label="VPN support"
checked={aws ? false : project.vpn_support_enabled}
disabled={disabled || aws}
checked={noPrivileged ? false : project.vpn_support_enabled}
disabled={disabled || noPrivileged}
onChange={(v) => save({ vpn_support_enabled: v })}
/>
}
+1
View File
@@ -77,6 +77,7 @@ export function formatMemory(mib: number): string {
/** Mirrors Rust `AwsWorkspaceConfig::default()`. */
export function defaultAwsWorkspace(): AwsWorkspaceConfig {
return {
launch_type: "fargate",
cpu: 1024,
memory_mib: 4096,
arch: "x86_64",
+4
View File
@@ -92,7 +92,11 @@ export interface RepoSource {
folder: string;
}
export type LaunchType = "fargate" | "ec2";
export interface AwsWorkspaceConfig {
/** Absent in old data = fargate. */
launch_type: LaunchType;
cpu: number;
memory_mib: number;
arch: Arch;