Skip to content

Processing AWS CLI JSON Output With fx ​

The AWS CLI returns deeply nested JSON that is hard to read and awkward to filter with --query. Pipe it into fx to browse it interactively or reshape it with JavaScript. Install fx first.

Browse AWS CLI Output Interactively ​

Pipe any aws command into fx with no arguments to open the interactive viewer:

bash
aws ec2 describe-instances | fx

Collapse and expand nodes with the arrow keys, press @ to fuzzy-search paths like InstanceId, and / to search values with a regex. Press P to print the selected value to stdout. See Using fx as a TUI for all keys.

Parse aws ec2 describe-instances JSON ​

Instances are nested inside Reservations[].Instances[]. The [] operator flattens both levels into one array, and @ runs an expression for each item:

bash
aws ec2 describe-instances | fx '.Reservations[].Instances[]' '@.InstanceId'
json
[
  "i-0abc123def4567890",
  "i-0def456abc7890123",
  "i-0987fed654cba3210",
  "i-0555aaa111bbb2223"
]

Get the Name Tag of EC2 Instances ​

AWS stores tags as a [{Key, Value}] array in no fixed order. Use find, with optional chaining for instances that have no tags:

bash
aws ec2 describe-instances | fx '.Reservations[].Instances[]' \
  '@({id: x.InstanceId, type: x.InstanceType, state: x.State.Name, name: x.Tags?.find(t => t.Key === "Name")?.Value})'
json
[
  {
    "id": "i-0abc123def4567890",
    "type": "t3.micro",
    "state": "running",
    "name": "web-1"
  },
  {
    "id": "i-0def456abc7890123",
    "type": "t3.micro",
    "state": "running",
    "name": "web-2"
  },
  {
    "id": "i-0987fed654cba3210",
    "type": "m5.large",
    "state": "stopped",
    "name": "db-replica"
  },
  {
    "id": "i-0555aaa111bbb2223",
    "type": "c6g.xlarge",
    "state": "running",
    "name": null
  }
]

List Running EC2 Instances as Plain Text ​

Filter with ?, then print one line per instance with list:

bash
aws ec2 describe-instances | fx '.Reservations[].Instances[]' \
  '?.State.Name === "running"' '@[x.InstanceId, x.PrivateIpAddress].join("\t")' list
i-0abc123def4567890	10.0.1.15
i-0def456abc7890123	10.0.1.16
i-0555aaa111bbb2223	10.0.3.7

Calculate Total S3 Bucket Size From list-objects-v2 ​

aws s3api list-objects-v2 returns a Contents array with a Size in bytes for each object:

bash
aws s3api list-objects-v2 --bucket acme-assets | fx '.Contents' '@.Size' 'x => x.reduce((a, b) => a + b, 0)'
1491581781

Add humanSize from the next section as a last argument to print 1.4 GiB instead.

Find the Largest S3 Objects With Human-Readable Sizes ​

Define a helper in .fxrc.js so it is available in every fx call:

js
function humanSize(bytes) {
  const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB']
  let i = 0
  while (bytes >= 1024 && i < units.length - 1) {
    bytes /= 1024
    i++
  }
  return bytes.toFixed(i === 0 ? 0 : 1) + ' ' + units[i]
}

Sort by size with sortBy, keep the top three, and format each line:

bash
aws s3api list-objects-v2 --bucket acme-assets | fx '.Contents' 'sortBy(o => -o.Size)' '.slice(0, 3)' \
  '@`${humanSize(x.Size).padStart(10)}  ${x.Key}`' list
 717.0 MiB  backups/db-2026-09-08.sql.gz
 700.0 MiB  backups/db-2026-09-01.sql.gz
   5.0 MiB  logs/access-2026-09-20.log

Group Lambda Functions by Runtime ​

Use groupBy on the output of aws lambda list-functions, then keep only the function names:

bash
aws lambda list-functions | fx '.Functions' 'groupBy(f => f.Runtime)' \
  'x => Object.fromEntries(Object.entries(x).map(([runtime, fns]) => [runtime, fns.map(f => f.FunctionName)]))'
json
{
  "nodejs20.x": [
    "resize-image",
    "webhook-router"
  ],
  "python3.12": [ "send-email" ],
  "python3.8": [ "legacy-cron" ],
  "nodejs16.x": [ "pdf-render" ]
}

To list functions on deprecated runtimes: fx '.Functions' '?["python3.8", "nodejs16.x"].includes(x.Runtime)' '@.FunctionName'.

Find IAM Users Without MFA or With Old Access Keys ​

The IAM credential report comes back as base64-encoded CSV. Decode it with fromBase64 and convert rows to objects:

bash
aws iam generate-credential-report
aws iam get-credential-report | fx '.Content' fromBase64 'x => {
  const [head, ...rows] = x.trim().split("\n").map(line => line.split(","))
  return rows.map(r => Object.fromEntries(head.map((h, i) => [h, r[i]])))
}' > iam-users.json

Each user now has fields such as user, mfa_active, and access_key_1_last_rotated. All values are strings. Users without MFA:

bash
fx iam-users.json '?.mfa_active === "false"' '@.user'
json
[
  "bob",
  "ci-deploy"
]

Active access keys not rotated in 90 days, comparing ISO dates with Date:

bash
fx iam-users.json '?.access_key_1_active === "true"' \
  '?Date.now() - new Date(x.access_key_1_last_rotated) > 90 * 86400000' \
  '@`${x.user} ${x.access_key_1_last_rotated}`' list
bob 2024-01-15T12:30:00+00:00
ci-deploy 2021-02-03T08:05:00+00:00

Parse CloudWatch Logs Events JSON ​

aws logs filter-log-events returns timestamp in epoch milliseconds and message as a string. Convert both:

bash
aws logs filter-log-events --log-group-name /aws/lambda/checkout | fx '.events' \
  '@({time: new Date(x.timestamp).toISOString(), ...JSON.parse(x.message)})' '?.level === "error"'
json
[
  {
    "time": "2026-09-26T00:00:01.530Z",
    "level": "error",
    "msg": "payment gateway timeout",
    "path": "/orders",
    "ms": 5003
  },
  {
    "time": "2026-09-26T00:00:04.918Z",
    "level": "error",
    "msg": "db connection reset",
    "path": "/orders/42",
    "ms": 230
  }
]

JSON.parse throws if a message is plain text. For mixed logs, see Parsing JSON Logs.

fx as an Alternative to aws --query (JMESPath) ​

--query uses JMESPath, a separate language with its own filter and literal syntax. fx uses plain JavaScript. The same Name-tag lookup in both:

bash
aws ec2 describe-instances \
  --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value | [0]]'

aws ec2 describe-instances | fx '.Reservations[].Instances[]' \
  '@[x.InstanceId, x.Tags?.find(t => t.Key === "Name")?.Value]'

Both run on the client; server-side --filters Name=instance-state-name,Values=running still combines with fx.

See Also ​