The aws plugin establishes an AWS session once per invocation, writes the resolved region and credentials to process.env, and publishes a minimal, non‑sensitive breadcrumb under ctx.plugins.aws for downstream consumers. It also provides a small aws subcommand for session establishment and optional forwarding to the AWS CLI.
This plugin is intended to be the parent for child plugins that need AWS auth. Compose your child plugin under awsPlugin() so that ordering and context are guaranteed: the aws parent resolves profile/region/credentials first; the child runs afterward and can safely use the AWS SDK (v3) with the environment already in place.
At a glance:
ctx.plugins.aws (e.g., { profile?, region? })aws subcommand to resolve a session and optionally forward to the AWS CLIplugins.aws.profile) → dotenv (AWS_LOCAL_PROFILE) → dotenv fallback (AWS_PROFILE) → undefined.plugins.aws.region) → dotenv (AWS_REGION) → aws configure get region --profile <profile> (best‑effort) → plugins.aws.defaultRegion.cli-export default):
aws configure export-credentials, with optional SSO login-on-demand) and then applies them to process.env.process.env (AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY + optional AWS_SESSION_TOKEN) when present.aws configure export-credentials --format process first, then tries platform-appropriate env-like formats (PowerShell / windows-cmd / env / env-no-export), then falls back to the AWS CLI default output when needed.loginOnDemand is true, the plugin runs aws sso login --profile <profile> once and retries export.aws configure get aws_access_key_id, aws_secret_access_key, and optional aws_session_token.AWS_REGION and (when unset) AWS_DEFAULT_REGION into process.env.process.env:
AWS_PROFILE / AWS_DEFAULT_PROFILE / AWS_SDK_LOAD_CONFIG.AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN) and sets AWS_PROFILE / AWS_DEFAULT_PROFILE / AWS_SDK_LOAD_CONFIG=1.AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and optional AWS_SESSION_TOKEN to process.env.{ profile?, region? }) under ctx.plugins.aws.process.env for AWS SDK default providers and can read profile/region via ctx.plugins.aws.You can configure the aws plugin via getdotenv config (JSON/YAML/JS/TS) and/or via the aws subcommand flags. Config is recommended for defaults; flags are useful per‑run.
Config keys (under plugins.aws):
profile?: string — preferred AWS profileregion?: string — preferred regiondefaultRegion?: string — fallback region when none can be detectedprofileKey?: string — dotenv/config key for local profile (default: "AWS_LOCAL_PROFILE")profileFallbackKey?: string — fallback dotenv/config key for profile (default: "AWS_PROFILE")regionKey?: string — dotenv/config key for region (default: "AWS_REGION")strategy?: "cli-export" | "none" — credential acquisition strategy (default "cli-export"). Use "none" to skip credential resolution; region resolution still applies.loginOnDemand?: boolean — attempt aws sso login once when export fails for SSO profiles (default false)Subcommand flags (map directly to config for effective defaults):
--login-on-demand / --no-login-on-demand — enable/disable SSO login retry--profile <string> — override profile--region <string> — override region--default-region <string> — override fallback region--strategy <string> — cli-export | none--profile-key <string> — override config key name for profile (AWS_LOCAL_PROFILE by default)--profile-fallback-key <string> — override fallback key (AWS_PROFILE by default)--region-key <string> — override config key name for region (AWS_REGION by default)Note on capture: The shipped host treats --capture (or GETDOTENV_STDIO=pipe) as a global behavior. The aws subcommand honors it when forwarding to the AWS CLI and in child processes; when piped, the current executor re-emits buffered stdout (stderr is not re-emitted by default).
JSON:
{
"plugins": {
"aws": {
"profile": "dev",
"region": "us-east-1",
"defaultRegion": "us-east-1",
"strategy": "cli-export",
"loginOnDemand": true
}
}
}
YAML:
plugins:
aws:
profile: dev
region: us-east-1
defaultRegion: us-east-1
strategy: cli-export
loginOnDemand: true
JS/TS (dynamic allowed): Same keys as above; you can also compute values at runtime.
Compose your plugin as a child of the aws plugin so that auth is established first. Your plugin should:
ctx.plugins.aws (non‑sensitive metadata).process.env for credentials.Install the AWS SDK client:
npm i @aws-sdk/client-sts
Author the child plugin:
import { definePlugin } from '@karmaniverous/get-dotenv/cliHost';
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
export const whoamiPlugin = () =>
definePlugin({
ns: 'whoami',
setup(cli) {
cli
.description('Print AWS caller identity (uses parent aws session)')
.action(async () => {
// The AWS SDK default providers will read credentials from process.env,
// which the aws parent has already populated.
const client = new STSClient();
const result = await client.send(new GetCallerIdentityCommand());
console.log(JSON.stringify(result, null, 2));
});
},
});
Wire the child under the aws parent:
#!/usr/bin/env node
import { createCli } from '@karmaniverous/get-dotenv/cli';
import { awsPlugin } from '@karmaniverous/get-dotenv/plugins';
import { whoamiPlugin } from './plugins/aws-whoami';
const run = createCli({
alias: 'mycli',
compose: (p) => p.use(awsPlugin().use(whoamiPlugin())),
});
await run();
Usage:
# Establish session and then print identity
mycli aws --login-on-demand whoami
Notes:
awsPlugin().use(whoamiPlugin())) guarantees that the aws parent resolves profile/region/credentials before the child runs.--capture or set GETDOTENV_STDIO=pipe to buffer outputs deterministically in CI.The aws subcommand establishes the session and can optionally forward to the AWS CLI:
mycli aws --profile dev --region us-east-1
Writes region and credentials to process.env, mirrors { profile, region } to ctx.plugins.aws, then exits.
mycli aws -- sts get-caller-identity
Tokens after -- are forwarded to aws …. The subprocess runs with an environment composed from { ...process.env, ...ctx.dotenv } and honors capture (--capture or GETDOTENV_STDIO=pipe).
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, optional AWS_SESSION_TOKEN) to process.env for this process only. It also ensures AWS_REGION is set and synchronizes AWS_DEFAULT_REGION when missing.ctx.plugins.aws. Credentials are not mirrored there.strategy: "none", credential resolution is skipped; region resolution still applies. This can be useful when credentials arrive via other means (e.g., container IMDS or an external provider), but you still want normalized region handling.