anomalyco/sst · error · Error
Invalid size ${size}
Error message
Invalid size ${size} What it means
toMBs converts a size string like '512 MB', '2 GB', or '1 TB' into megabytes. It throws this error when the unit suffix after the space is not one of MB, GB, or TB. TypeScript's template-literal types (Size/SizeGbTb) normally prevent this at compile time, but a value cast or read at runtime (e.g. from config or env) bypasses the type and hits this guard.
Source
Thrown at platform/src/components/size.ts:14
export type Size = `${number} ${"MB" | "GB"}`;
export type SizeGbTb = `${number} ${"GB" | "TB"}`;
export function toMBs(size: Size | SizeGbTb) {
const [count, unit] = size.split(" ");
const countNum = parseFloat(count);
if (unit === "MB") {
return countNum;
} else if (unit === "GB") {
return countNum * 1024;
} else if (unit === "TB") {
return countNum * 1024 * 1024;
}
throw new Error(`Invalid size ${size}`);
}
export function toGBs(size: Size | SizeGbTb) {
const [count, unit] = size.split(" ");
const countNum = parseFloat(count);
if (unit === "MB") {
return countNum / 1024;
} else if (unit === "GB") {
return countNum;
} else if (unit === "TB") {
return countNum * 1024;
}
throw new Error(`Invalid size ${size}`);
}
View on GitHub (pinned to a0bd20f762)
Solutions
- Format the size as '<number> <UNIT>' with a space and an uppercase unit from MB/GB/TB, e.g. '512 MB' instead of '512MB' or '512 mb'.
- Convert binary units: use '512 MB' not '512 MiB', or precompute the MB value yourself.
- If the value comes from a variable, cast it to Size only after validating the format with a regex like /^\d+(\.\d+)? (MB|GB|TB)$/.
- Call toGBs instead if you actually want gigabytes — but note it accepts the same units, so fix the string either way.
Example fix
// before
memory: process.env.TASK_MEMORY as Size // e.g. "512MB"
// after
const mem = process.env.TASK_MEMORY ?? "512 MB";
if (!/^\d+(\.\d+)? (MB|GB|TB)$/.test(mem)) throw new Error(`bad memory size: ${mem}`);
memory: mem as Size Defensive patterns
Strategy: validation
Validate before calling
const SIZE_RE = /^\d+(\.\d+)? (MB|GB|TB)$/;
if (!SIZE_RE.test(String(size))) throw new Error(`size must match '<n> MB|GB|TB', got: ${size}`); Type guard
function isSize(v: unknown): v is `${number} ${"MB" | "GB" | "TB"}` {
return typeof v === "string" && /^\d+(\.\d+)? (MB|GB|TB)$/.test(v);
} Try / catch
let mb: number;
try { mb = toMBs(size); } catch (e) {
throw new Error(`Invalid memory size "${size}" — use e.g. "512 MB" or "2 GB"`, { cause: e });
} Prevention
- Always include a single space between the number and unit: '512 MB', never '512MB'.
- Use only uppercase MB/GB/TB; avoid MiB/GiB binary units.
- Never cast unvalidated strings (env vars, JSON) to Size without a regex check.
- Centralize size parsing behind a helper that validates once.
When it happens
Trigger: Calling toMBs (directly or via memory/containerDefinitions/createTaskDefinition) with a string whose unit is not exactly 'MB', 'GB', or 'TB' — e.g. '512MB' (no space), '512 mb' (lowercase), '0.5 GiB', or a number without a unit.
Common situations: Specifying an ECS task/container memory in sst.config.ts with the wrong format: missing space, lowercase unit, MiB/GiB binary units, or a memory value interpolated from a non-typed source like an environment variable or JSON config.
Related errors
- You cannot provide both a function and a task in the "${name
- You cannot provide both a function and a task in the "${name
- Invalid function definition for the "${name}" Function
- Unsupported CPU: ${v}. The supported values for CPU are ${Ob
- Unsupported memory: ${v}. The supported values for memory fo
AI-assisted analysis of anomalyco/sst@a0bd20f762 (2026-08-30).
Data as JSON: /api/errors/6f94c3fb841af722.
Report an issue: GitHub.