Run one-off scripts
luna script run executes a .js or .ts script with full Luna CLI context — assumed-role AWS credentials, the target environment, and controlled module resolution — so a genuinely one-off task doesn't need to become a bespoke luna command. Use it for the kind of thing you'll run once or twice (a data fix, a backfill, a debugging script), not for something that should be reusable — that belongs as a proper command instead (see Add a command).
Run it from the cli/ directory: the script path you pass resolves relative to your shell's current directory, not to any fixed repo root.
Basic usage
luna script run <scriptPath> [options] [-- script-args]# A script kept in the CLI itself
luna script run src/scripts/andi/staff-breakdown.ts
# Target an environment — same -e flag as any other luna command, placed before the subcommand
luna -e staging script run src/scripts/activities/backfill-redshift.ts
# A service script, reached from cli/ with a relative path
luna script run ../services/<service>/scripts/<your-script>.ts --modules services/<service>TypeScript scripts run automatically through ts-node — no separate build step.
Options
| Flag | Default | What it does |
|---|---|---|
-m, --modules <contexts> | cli | Comma-separated module contexts consulted for require/import resolution (see below) |
--cwd <path> | the script's own directory | Working directory the script runs in |
--debug | off | Print the resolved runtime, working directory, module paths, and tsconfig before running |
-e, --environment <env> | prompts if omitted | Target environment; the same global flag every luna command takes |
Choosing module contexts
The --modules flag controls where Node looks for packages when the script does require() or import — it's what gets set as NODE_PATH for the child process.
| Context | Resolves to |
|---|---|
cli (default) | cli/node_modules |
services/<name> | services/<name>/node_modules |
packages/<name> | packages/<name>/node_modules |
cli-scripts-legacy | cli-scripts-legacy/node_modules |
. | the script's own local node_modules, resolved from its directory (no CLI node_modules lookup) |
List multiple contexts, comma-separated, in priority order — the first one that has a package wins:
luna script run services/<service>/scripts/<your-script>.ts --modules services/<service>,cliTIP
packages/<name> works the same way as services/<name> but isn't listed in luna script run --help or the CLI's own README.md — both are stale on this point. If your script needs a package's dependencies, --modules packages/<name> still works.
Working directory vs. module resolution
--modules and --cwd control two different things, and it's easy to reach for the wrong one:
--modules— only affects package resolution (node_modules). It has no effect on relative file paths orprocess.cwd().--cwd— only affects the working directory: relative file paths (fs.readFile('./config.json')) andprocess.cwd(). It has no effect on package resolution.
Import paths inside your script are always resolved relative to the script file's own location, regardless of --cwd. If a script needs to import from a service's models/ or utils/, it has to live inside that service's directory tree.
Most scripts don't need --cwd at all — it defaults to the script's own directory, which is right for scripts that read/write files next to themselves. Set it explicitly when a script expects to run from somewhere else, e.g. a service's root:
luna script run services/<service>/scripts/<your-script>.ts --modules cli --cwd services/<service>TypeScript and Lambda layer paths
For a .ts script, the runner uses cli/node_modules/.bin/ts-node — if it's missing, run yarn install in cli/.
If a tsconfig.json is found in the working directory or the script's own directory, the runner loads it via tsconfig-paths/register. This lets a script resolve the /opt/nodejs/* Lambda layer paths a service's own code uses — but only if that service also has tsconfig-paths installed as a dev dependency and paths configured in its tsconfig.json. Without both, imports through a layer path will fail even though the script itself is fine.
Passing arguments to your script
Everything after -- is forwarded to the script untouched, via process.argv:
luna script run scripts/<your-script>.ts -- --dry-run --batch-size=100const args = process.argv.slice(2);
// ['--dry-run', '--batch-size=100']For anything past trivial flag-checking, use the CLI's parseArgs helper (cli/src/utils/parseArgs.ts) rather than hand-rolling parsing:
import { parseArgs } from '../../utils/parseArgs';
const options = parseArgs(process.argv.slice(2));
// options['dry-run'] === true
// options['batch-size'] === '100'Environment and AWS credentials
script run goes through the same environment resolution and role assumption as every other luna command:
- the target environment comes from
-e, theENVIRONMENTenv var, or an interactive prompt, and is set asprocess.env.ENVIRONMENTfor the script; - for any environment other than
local, the CLI assumes the<environment>-iam-role-luna-clirole first, so the script inherits workingAWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKENandAWS_DEFAULT_REGION— no extra setup needed to call AWS services; - the run is audited the same as any other command: a
PENDINGrecord is written before the script starts and updated toSUCCEEDED,FAILED, orCANCELLEDafterwards.localruns skip both role assumption and auditing entirely.
Troubleshooting
ts-node runtime not found— runyarn installincli/.Cannot find module '...'— the package isn't in any of thenode_modulesyour--moduleslist points at; add the right context or install the dependency there.ENOENTon a relative file path — the script is reading relative to the wrong directory; set--cwdexplicitly.- AWS calls failing / "resource not found" — usually an AWS access issue rather than the script itself. Check your environment resolves to the one you expect (
--debugprints it), and see AWS setup & CLI auth to check your credentials are set up.
