Skip to content

(terraform): add -replace option to plan and apply commands #2168

@fabn

Description

@fabn

Describe the feature

Add support for the -replace flag (and -refresh=false) to the ExecuteTerraformCommand tool for both plan and apply operations.

Use Case

The -replace flag is essential for forcing replacement of specific resources when troubleshooting or dealing with:

  • Tainted resources that need recreation
  • Resources with external configuration drift
  • Testing disaster recovery procedures
  • Forcing recreation of resources without manual terraform taint

Example scenarios:

# Force replace a specific EC2 instance
terraform plan -replace="aws_instance.web_server"

# Plan with multiple replacements
terraform plan -replace="aws_instance.app" -replace="aws_instance.worker"

# Apply with replacement (after reviewing plan)
terraform apply -replace="aws_instance.web_server"

Similarly, -refresh=false is useful to skip the refresh phase when you know the state is current and want faster planning.

Proposed Solution

Add optional parameters to the ExecuteTerraformCommand tool:

  • replace_targets: list of resource addresses to force replacement
  • refresh: boolean flag to control state refresh (default: true)

Example API usage:

await execute_terraform_command(
    command="plan",
    working_directory="./infrastructure",
    replace_targets=["aws_instance.web_server", "aws_db_instance.main"],
    refresh=False
)

Other Information

This complements issue #2013 which requests -target support. Together, these options provide more granular control over Terraform operations via MCP.

Terraform documentation:

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    To triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions