Agent Skill · Infracost

iac-generation

Ensures cost optimization and FinOps/tagging policy compliance for infrastructure as code. BLOCKING REQUIREMENT - Invoke this skill BEFORE generating any IaC code, entering plan mode, or asking clarification questions. TRIGGER when: planning or writing code which creates cloud infrastructure resources (regardless of naming a specific tool to do so), imports IaC provider SDKs (pulumi-aws, hashicorp/aws, aws-cdk), or the task involves writing Pulumi, Terraform, CloudFormation, CDK, or Terragrunt. DO NOT TRIGGER when: general programming unrelated to infrastructure provisioning.

Provider: Infracost Path in repo: plugins/infracost/skills/iac-generation/SKILL.md

Skill body

Infracost IaC Generation Assistance

Assists when creating or modifying infrastructure as code (IaC) to ensure best practices are followed and costs are optimized from the start. Uses the Infracost CLI to analyze code as it’s being written, providing real-time feedback on cost implications and optimization recommendations. The output includes actionable insights, such as suggesting more cost-effective resource types, identifying potential savings opportunities, and flagging any FinOps policy violations before the code is deployed. It also ensures guidelines supplied by the user’s organization are being followed, such as tagging policies or required resource configurations, to catch problems early rather than during code review.

Directly supported IaC types: Terraform, CloudFormation, Terragrunt. CDK is not yet directly supported.

Other IaC languages are also indirectly supported if you convert the resource definitions to Terraform (written to a temporary directory) and then scan that directory with the CLI.

Setup

Important: Verify the Infracost CLI is installed and the user is authenticated before generating IaC.

  1. Check the CLI is on the path:

    infracost --version
    

    If this fails, inform the user that they need to install the Infracost CLI by following the instructions at https://www.infracost.io/docs/features/get_started/.

  2. Check the user is logged in:

    infracost auth whoami
    

    If this reports that the user is not authenticated, ask them to run infracost auth login in a separate terminal window and let you know once it completes. Do not attempt to run the login command yourself — it is interactive.

Usage

There are several tools available to you for this job.

Five commands are relevant for this skill:

  1. policies - list all policies for the user’s organization, so we can ensure we produce code that is compliant with them
  2. guardrails - list cost guardrails (spending thresholds) configured for the repository, so we can ensure the infrastructure we generate stays within budget
  3. budgets - list tag-scoped budgets for the organization, so we can see which tag groups are already near or over their actual cloud spend limits
  4. scan - analyze IaC files and output JSON with costs, diagnostics, and policy violations
  5. price - takes a (typically smaller) standalone piece of Terraform and estimates the cost of it, which is useful for quicker feedback for faster iteration on individual (or small groups of) resources

More information on each is available below.

When writing IaC code, you should do the following:

Guardrails Command

The guardrails command lists cost guardrails configured for the repository. Guardrails define spending thresholds that, when exceeded, can trigger alerts, PR comments, or block PRs entirely.

infracost guardrails

The output shows each guardrail with its name, scope (repo or project-level), thresholds (total monthly cost, cost increase amount, cost increase percentage), and actions (PR comment, block PR, or alert only). Use this to understand the budget constraints before writing any infrastructure code.

Policies Command

The policies command lists all tagging and FinOps policies that are configured for the user’s organization. This is important to understand before writing any code, so you can ensure your code is compliant with the policies from the start.

infracost policies

The output includes the policy name, type (tagging or FinOps), description, and any parameters. Use this information to guide your code writing and ensure you are following the organization’s guidelines. For example, if there is a tagging policy that requires all resources to have a cost_center tag, make sure to include that in your resource definitions.

Budgets Command

The budgets command lists tag-scoped budgets configured for the user’s organization. Each budget has a tag scope (e.g. env=production), a limit, a period, and the actual cloud spend recorded against that scope so far.

infracost budgets

Important: Budget spend reflects actual cloud billing data across the whole organization, not a preview of the current change. Use this as context for the user, not as a hard constraint on the code you generate.

The output shows each budget’s name, limit, current spend, period, and tag scope. Before generating resources, check whether their tags match any budget’s scope. If they do, mention where that budget stands — especially if it is already over or close to its limit — so the user can make an informed call on tags, sizing, or timing.

Scan Command

Run the scan command, pointing to your IaC files or a repository root:

# Single CloudFormation template
infracost scan /path/to/cloudformation.yaml

# Terraform project directory
infracost scan /path/to/terraform/

# Repository root (auto-discovers all IaC projects in nested directories)
infracost scan /path/to/repo

Output

By default, scan prints a human-readable summary to stdout (projects, resources, monthly cost, FinOps and tagging policy counts, guardrails, budgets, diagnostics) followed by a “What’s next?” section with suggested inspect commands. Diagnostics and warnings go to stderr. Pass the global --json flag for the full machine-readable JSON output:

# Human-readable summary (default)
infracost scan /path/to/repo

# Full JSON, redirected for large repos
infracost scan --json /path/to/repo > /tmp/scan.json

--json is a global flag — it works on scan, price, and inspect and also switches log output to JSON.

For a compact, token-efficient text format suitable for piping into LLM prompts or other agentic tooling, pass --llm instead of --json. It carries the same data model in roughly 30–40% fewer tokens, with arrays of uniform records rendered as tabular rows so they grep cleanly.

Both --json and --llm outputs include a top-level summary block with pre-computed totals (total_monthly_cost, total_potential_monthly_savings, distinct failing-resource counts per policy class, per-class policy counts, guardrails triggered, budgets over). Read the summary first — most “how many X are failing?” or “what’s the total Y?” questions can be answered from it without walking projects[] yourself.

Inspecting Results

Scan results are cached automatically, so inspect picks them up with no extra arguments. You don’t need to redirect output or pass --file unless you specifically saved a JSON file with --json.

Important: The inspect command reads cached results and you DO NOT NEED to write any scripts to handle JSON yourself.

# Scan first (caches the result)
infracost scan /path/to/repo

# Drill in — no --file needed
infracost inspect [flags]

# Or, if you saved JSON yourself
infracost inspect --file /tmp/scan.json [flags]

Available flags (combine as needed):

Views

Aggregations

Resource selection

Output projection (replaces piping through cut / awk '{print $N}')

Available fields per view (use with --fields):

For “how many distinct resources fail X policy” questions, prefer --summary --fields distinct_failing_finops_resources / distinct_failing_tagging_resources over enumerating the failing-resource list and piping through sort -u | wc -l or awk. The summary already de-dupes addresses across multiple policies.

Format

--group-by dimensions

Resource-context dims (aggregate the resource list): type, provider, project, resource, file. Use resource to list every resource sorted by cost (no --top limit needed).

Anchor dims (each routes to its own collector): policy, guardrail, budget.

Compatibility rules (validated up-front):

The same mutual-exclusion rule applies to the drill-in flags --policy, --budget, and --guardrail — pick one.

Price Command

The price command reads Terraform code directly from stdin and prints a human-readable cost summary by default. For example:

echo 'resource "aws_instance" "x" { instance_type = "t2.micro" }' | infracost price

Pass --json for the same JSON shape as scan. Like scan, results are cached, so the same inspect command can be used to drill into the result without --file.

Important Guidelines