Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to use -target and -replace flags with CLI driven workflow #1905

Merged
merged 10 commits into from
Mar 17, 2025

Conversation

alfespa17
Copy link
Member

@alfespa17 alfespa17 commented Mar 17, 2025

Fix #1775

This PR will allow to use the target and replace flags when using the CLI driven workflow

Without target flag

user@pop-os:~/git/simple-terraform$ terraform plan

Running plan in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-zas3imae35e.ws-us118.gitpod.io/app/simple/simple_test/runs/run-1

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.next will be created
  + resource "null_resource" "next" {
      + id = (known after apply)
    }

  # null_resource.next2 will be created
  + resource "null_resource" "next2" {
      + id = (known after apply)
    }

  # null_resource.next3 will be created
  + resource "null_resource" "next3" {
      + id = (known after apply)
    }

  # null_resource.next4 will be created
  + resource "null_resource" "next4" {
      + id = (known after apply)
    }

  # null_resource.previous will be created
  + resource "null_resource" "previous" {
      + id = (known after apply)
    }

  # time_sleep.wait_30_seconds will be created
  + resource "time_sleep" "wait_30_seconds" {
      + create_duration  = "30s"
      + destroy_duration = "45s"
      + id               = (known after apply)
    }

  # module.time_module.random_integer.time will be created
  + resource "random_integer" "time" {
      + id     = (known after apply)
      + max    = 5
      + min    = 1
      + result = (known after apply)
    }

Plan: 7 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + creation_time = "30s"
  + fake_data     = {
      + data     = "Hello World"
      + resource = {
          + resource1 = "fake"
        }
    }

With target flag

user@pop-os:~/git/simple-terraform$ terraform plan -target=time_sleep.wait_30_seconds

Running plan in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-zas3imae35e.ws-us118.gitpod.io/app/simple/simple_test/runs/run-2

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.previous will be created
  + resource "null_resource" "previous" {
      + id = (known after apply)
    }

  # time_sleep.wait_30_seconds will be created
  + resource "time_sleep" "wait_30_seconds" {
      + create_duration  = "30s"
      + destroy_duration = "45s"
      + id               = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + creation_time = "30s"
╷
│ Warning: Resource targeting is in effect
│ 
│ You are creating a plan with the -target option, which means that the
│ result of this plan may not represent all of the changes requested by the
│ current configuration.
│ 
│ The -target option is not for routine use, and is provided only for
│ exceptional situations such as recovering from errors or mistakes, or when
│ Terraform specifically suggests to use it as part of an error message.

Replace flag

user@pop-os:~/git/simple-terraform$ terraform plan -replace=time_sleep.wait_30_seconds

Running plan in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...

To view this run in a browser, visit:
https://8080-azbuilder-terrakube-zas3imae35e.ws-us118.gitpod.io/app/simple/simple_test/runs/run-4

Waiting for the plan to start...

***************************************
Running Terraform PLAN
***************************************
module.time_module.random_integer.time: Refreshing state... [id=3]
null_resource.previous: Refreshing state... [id=8453405252639364908]
time_sleep.wait_30_seconds: Refreshing state... [id=2025-03-17T19:37:42Z]
null_resource.next4: Refreshing state... [id=695527150358536858]
null_resource.next: Refreshing state... [id=5608491521324904523]
null_resource.next3: Refreshing state... [id=7978755604606084072]
null_resource.next2: Refreshing state... [id=1031510302611639606]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # time_sleep.wait_30_seconds will be replaced, as requested
-/+ resource "time_sleep" "wait_30_seconds" {
      ~ id               = "2025-03-17T19:37:42Z" -> (known after apply)
        # (2 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

@alfespa17 alfespa17 merged commit 713e775 into main Mar 17, 2025
5 checks passed
@jcanizalez jcanizalez deleted the feat/target-flag branch March 21, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Support of -target option
1 participant