Skip to content

CLI reference

logs

stacktape logs prints the logs of a deployed resource from its CloudWatch log group. By default it shows recent log lines, optionally filtered by time and pattern. With --query, it runs a CloudWatch Logs Insights query instead.

Usage

The command finds the log group through your Stacktape config and the deployed stack, so run it in the project directory or pass --config.

Fetch the last hour of logs from a resource named myApi.

stacktape logs myApi --stage prod --region eu-west-1

Fetch logs from a container workload. Use --container to specify which container's log group to target.

stacktape logs myService --stage prod --region eu-west-1 --container main

Two modes

Standard mode

Standard mode fetches recent log events from the resource's CloudWatch log group. By default it returns up to 100 events from the last hour. Narrow results with --since, --filter, and --limit.

stacktape logs myApi --stage prod --region eu-west-1 --since "2h" --filter "ERROR" --limit 50

The command resolves log streams for the target log group, fetches matching events, and either formats them for terminal display or outputs JSON when --raw or --agent is set.

In an interactive terminal, the command shows the same newest events (up to --limit, 100 by default) and then follows the log group, printing new events that match --filter as they arrive until you press Ctrl+C. It sends at most two CloudWatch Logs requests per second, so following a busy production log group stays within the account's request quota. When the output is piped or redirected, or with --raw or --agent, the command prints the events once and exits. --follow=false does the same in a terminal.

--follow keeps following without a terminal, for example in a coding agent's shell or in CI. Each log line is written as plain text as soon as it arrives (with --agent, as NDJSON records; see below). The command ends on Ctrl+C or SIGTERM, and when the reading end of its output closes, as with | head -20: it notices on the next line it writes. --follow cannot be combined with --raw.

stacktape logs myApi --stage dev --region eu-west-1 --follow --filter "ERROR" > api-errors.log &

Insights mode

Pass a --query flag to run a CloudWatch Logs Insights query instead. Insights mode lets you aggregate, sort, and transform log data using the Logs Insights query language. When --query is provided, the command calls CloudWatch Logs Insights and prints the returned results.

stacktape logs myApi --stage prod --region eu-west-1 --query "fields @timestamp, @message | filter @message like /ERROR/ | sort @timestamp desc | limit 20"

A resource with logging.logClass: infrequent-access can't be filtered or followed: CloudWatch supports neither for that log class. The command then runs a bounded Logs Insights snapshot instead, prints the events in the usual shape and exits. Use --query instead of --filter to narrow such a log group.

In Insights mode, non-agent output prints each result row with a highlighted timestamp and message. In agent mode, that JSON payload is text in a type: "log" record's message within the CLI NDJSON stream; the final result record is separate.

Important flags

startTime

Controls how far back to fetch logs. Accepts two formats:

  • Relative shorthand — a number followed by a unit: 1h (1 hour), 30m (30 minutes), 1d (1 day), 5s (5 seconds). This counts back from --until, or now when it is omitted.
  • Absolute date — any string the JavaScript Date constructor accepts, such as 2026-05-28T14:00:00Z.

When omitted, defaults to 1 hour ago.

stacktape logs myApi --stage prod --region eu-west-1 --since "6h"

filter

A CloudWatch Logs filter pattern applied server-side before events are returned. Supports simple text matching and structured JSON filter syntax. See the AWS filter pattern documentation for the full syntax. This flag applies in standard mode only.

Match any log line containing the word "ERROR".

stacktape logs myApi --stage prod --region eu-west-1 --filter "ERROR"

Match structured JSON logs where the level field equals "error".

stacktape logs myApi --stage prod --region eu-west-1 --filter "{ $.level = \"error\" }"

query

A CloudWatch Logs Insights query string. When provided, the command switches to Insights mode. Insights queries support aggregation (stats count(*)), pattern matching (parse), and field extraction — useful for understanding error rates, latency distributions, or any structured analysis across large volumes of logs.

stacktape logs myApi --stage prod --region eu-west-1 --query "stats count(*) by bin(5m) as period"

limit

Maximum number of log events to return in standard mode. Defaults to 100. Increase for deeper debugging sessions; keep low for quick checks.

stacktape logs myApi --stage prod --region eu-west-1 --limit 500

container

Specifies which container's log group to target. This flag is optional in the CLI schema and is passed to the log group resolver. Use it when the deployed resource has more than one container (e.g., a multi-container workload) and Stacktape needs a container name to identify the correct log group.

stacktape logs myWorkload --stage prod --region eu-west-1 --container sidecar

raw

Outputs logs as JSON instead of the default formatted view. In standard mode, the output is a JSON object with logGroup and events fields, where each event has timestamp, message, and logStream. Useful for piping into jq or other tools.

stacktape logs myApi --stage prod --region eu-west-1 --raw

agent

Without --follow, --agent uses the CLI NDJSON protocol. The log payload is JSON text inside a type: "log" record's message; the final type: "result" record is separate and has result: null. Standard-mode payloads contain logGroup and events; Insights payloads contain logGroup, query and results. Parse both the protocol and the message payload, or use MCP stacktape_cli, which extracts it. Do not parse all stdout as one log object.

With --agent --follow, every line of stdout stays an NDJSON record. Each batch of new events is a type: "log" record whose message holds the same logGroup/events JSON as the one-shot payload, so the same parser reads both. The final result record follows when following ends.

stacktape logs myApi --stage prod --region eu-west-1 --agent

--until bounds the one-shot fetch, Insights queries and the initial follow backlog, and anchors a relative --since. Following continues beyond that time.

Output format

In standard mode, the output is formatted for the terminal. Pass --raw for plain JSON, or --agent for the CLI's JSONL protocol.

--raw prints this JSON object for standard retrieval. With --agent without --follow, this is the payload encoded as text in a log record's message:

JSON
{  "logGroup": "/aws/lambda/my-stack-myApi",  "events": [    {      "timestamp": "2026-05-29T10:15:30.000Z",      "message": "Processing request...",      "logStream": "2026/05/29/[$LATEST]abc123"    }  ]}

In Insights mode with --agent, the log record's JSON message payload includes the query and results:

JSON
{  "logGroup": "/aws/lambda/my-stack-myApi",  "query": "fields @timestamp, @message | limit 10",  "results": [    { "@timestamp": "2026-05-29T10:15:30", "@message": "Processing request..." }  ]}

If the resource has no log streams yet, the command prints an empty result. With --agent, the log record's message then contains {"logGroup": "...", "events": []}.

How log group resolution works

The command initializes the deployed stack and resolves the CloudWatch log group for the resource name you pass as the first argument. The optional --container flag is forwarded to the resolver so it can target the intended container when the resource has multiple containers.

The resolver works with compute resources that write to CloudWatch Logs. For container resources with multiple containers, pass --container so the resolver can identify the correct log group.

Examples

Fetch the last 30 minutes of logs.

stacktape logs myApi --stage prod --region eu-west-1 --since "30m"

Fetch only error-level logs from the last day.

stacktape logs myApi --stage prod --region eu-west-1 --since "1d" --filter "ERROR"

Run a Logs Insights query to count errors by 5-minute window.

stacktape logs myApi --stage prod --region eu-west-1 --query "filter @message like /ERROR/ | stats count(*) by bin(5m)"

Get raw JSON output for piping into another tool.

stacktape logs myApi --stage prod --region eu-west-1 --raw | jq '.events[] | select(.message | contains("timeout"))'

Fetch logs from a specific container in a multi-container workload.

stacktape logs myWorkload --stage prod --region eu-west-1 --container worker --since "2h" --limit 200

Use agent mode for structured output in a CI script.

stacktape logs myApi --stage prod --region eu-west-1 --agent --since "30m" --filter "ERROR"

logs FAQ

How much does CloudWatch Logs cost?

CloudWatch Logs pricing depends on ingestion, storage, retrieval, and query/analysis usage. Logs Insights queries (via --query) charge per GB of data scanned. High-throughput production services should consider setting log retention limits or using log forwarding to cheaper storage. Check the AWS CloudWatch pricing page for current rates.

Options

stacktape logs <resource-name>19 options
  • -r, --regionRequiredstring
    The AWS region for the operation.

    For a list of available regions, see the AWS documentation.

    Inside a project whose config declares region, the flag is optional and overrides the config.

    21 allowed values: us-east-2us-east-1us-west-1us-west-2ap-east-1ap-south-1
    Show all ap-northeast-3ap-northeast-2ap-southeast-1ap-southeast-2ap-northeast-1ca-central-1eu-central-1eu-west-1eu-west-2eu-west-3eu-north-1me-south-1sa-east-1af-south-1eu-south-1
  • The name of the resource as defined in your Stacktape configuration.

  • -s, --stageRequiredstring
    The stage for the operation (e.g., production, staging, dev-john).

    You can set a default stage using the defaults set command. Stage names use lowercase letters, numbers and dashes, with at least 2 characters. The project name, a dash and the stage form the CloudFormation stack name, which can have at most 128 characters. Stacktape shortens an AWS resource name that would be too long and adds a hash.

  • The name of the AWS account to use for the operation.

    The account must first be connected in the Stacktape Console.

    Without it, the account is chosen by --profile, AWS_PROFILE or AWS credentials in environment variables, then by your saved default, then by your organization's only connected account. --aws-account takes precedence over all of them, but AWS credentials in environment variables must then belong to that account.

  • The path to your Stacktape configuration file, relative to the current working directory.

    Without it, Stacktape uses the one stacktape.ts, stacktape.yml, stacktape.yaml or stacktape.js in the working directory. When there are several, the command stops and asks you to choose one with this option.

  • The name of the container as defined in your container compute resource configuration.

  • --cwdstring
    The working directory for the operation.

    All file paths in your configuration will be resolved relative to this directory. By default, this is the directory containing the configuration file.

  • --untilnumber | string
    The query end time; accepts JavaScript Date inputs and defaults to now.

    Relative --since counts back from it. Metrics, logs and Logs Insights enforce it. When following logs, it bounds the initial backlog, and following then continues beyond it.

  • --filterstring
    A pattern to filter the logs.

    Only logs matching the pattern will be printed. For more information on filter patterns, see the AWS documentation.

  • --followboolean
    If true, keeps printing new log lines as they arrive until the command is stopped.

    In a terminal, logs follows by default; --follow=false prints once and exits. Piped, redirected, in CI or for an agent, logs prints once unless --follow is set. Following without a terminal prints plain lines, one at a time, and ends on Ctrl+C, SIGTERM, or when the reading end of the output closes.

  • If provided, the command will not execute and will instead print help information.

  • --limitnumber

    Maximum number of items to return.

  • --log-level"info" | "debug" | "error"
    Log Level

    The level of logs to print to the console.

    • info: Basic information about the operation.
    • error: Only errors.
    • debug: Detailed information for debugging.
  • The AWS profile to use for the command.

    You can manage profiles using the aws-profile:* commands and set a default profile with defaults set.

    For commands that use a connected AWS account, --profile (or AWS_PROFILE) selects the connected account the profile belongs to, unless --aws-account is set. The command fails if that account is not connected.

  • --projectstring
    The name of the Stacktape project for this operation.

    Commands that target a stack take it from projectName in the project's config when the flag is absent.

  • --querystring
    CloudWatch Logs Insights query string.

    Example: "fields @timestamp, @message | filter @message like /ERROR/ | limit 50"

  • --quietboolean
    Hide progress, banners, success messages and hints.

    Command output, warnings and errors are still printed.

  • --rawboolean

    If true, prints logs in raw JSON format instead of pretty-printing them.

  • --sincenumber | string
    The query start time: a relative duration such as 30m, an ISO timestamp or a supported numeric date value.

    Relative durations count back from --until (now by default).