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

Provision resources in a workspace with input variables, followed by targeted destruction #164

Closed
wants to merge 1 commit into from

Conversation

RDhar
Copy link
Member

@RDhar RDhar commented Nov 15, 2023

Use-case scenario
Provision resources in a workspace with input variables, followed by targeted destruction.

For demo purposes, I have split out plan and apply outputs to separate comments, instead of updating the same one.

#1 PR Comment: Plan configuration in a workspace with input variable file.
-tf=plan -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

#2 PR Comment: Apply configuration in a workspace with input variable file.
-tf=apply -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

#3 PR Comment: Plan destruction of targeted resources in a workspace with input variable file.
-tf=plan -destroy -target=aws_instance.sample,data.aws_ami.ubuntu -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

#4 PR Comment: Apply destruction of targeted resources in a workspace with input variable file.
-tf=apply -destroy -target=aws_instance.sample,data.aws_ami.ubuntu -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

@RDhar
Copy link
Member Author

RDhar commented Nov 15, 2023

-tf=plan -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

@github-actions github-actions bot added the tf:plan Pull requests that plan TF code label Nov 15, 2023
Copy link

github-actions bot commented Nov 15, 2023

{"tf":"plan","chdir":"sample_instance","workspace":"dev","var-file":"env/dev.tfvars"}

Plan: 1 to add, 0 to change, 0 to destroy.
TF Via PR Comments by @RDhar via issue_comment at 2023-11-15T21:19:07Z.
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:

  # aws_instance.sample will be created
  + resource "aws_instance" "sample" {
      + ami                                  = "ami-0f324e1cd5c4465c8"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
      + availability_zone                    = (known after apply)
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_stop                     = (known after apply)
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + host_id                              = (known after apply)
      + host_resource_group_arn              = (known after apply)
      + iam_instance_profile                 = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_lifecycle                   = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "t2.nano"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
      + placement_group                      = (known after apply)
      + placement_partition_number           = (known after apply)
      + primary_network_interface_id         = (known after apply)
      + private_dns                          = (known after apply)
      + private_ip                           = (known after apply)
      + public_dns                           = (known after apply)
      + public_ip                            = (known after apply)
      + secondary_private_ips                = (known after apply)
      + security_groups                      = (known after apply)
      + source_dest_check                    = true
      + spot_instance_request_id             = (known after apply)
      + subnet_id                            = (known after apply)
      + tags                                 = {
          + "Name" = "unique-instance"
        }
      + tags_all                             = {
          + "Environment" = "Development"
          + "Name"        = "unique-instance"
          + "Stack"       = "sample_instance"
          + "Terraform"   = "dev"
        }
      + tenancy                              = (known after apply)
      + user_data                            = (known after apply)
      + user_data_base64                     = (known after apply)
      + user_data_replace_on_change          = false
      + vpc_security_group_ids               = (known after apply)
    }

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

Changes to Outputs:
  ~ sample_instance_id = "i-07a8f66915f738df5" -> (known after apply)

@RDhar
Copy link
Member Author

RDhar commented Nov 15, 2023

-tf=apply -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

@github-actions github-actions bot added the tf:apply Pull requests that apply TF code label Nov 15, 2023
Copy link

github-actions bot commented Nov 15, 2023

{"tf":"apply","chdir":"sample_instance","workspace":"dev","var-file":"env/dev.tfvars"}

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
TF Via PR Comments by @RDhar via issue_comment at 2023-11-15T21:19:43Z.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

sample_instance_id = "i-0c6e70cddd2f2cb0e"

@RDhar
Copy link
Member Author

RDhar commented Nov 15, 2023

-tf=plan -destroy -target=aws_instance.sample,data.aws_ami.ubuntu -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

Copy link

github-actions bot commented Nov 15, 2023

{"tf":"plan","destroy":true,"target":"aws_instance.sample,data.aws_ami.ubuntu","chdir":"sample_instance","workspace":"dev","var-file":"env/dev.tfvars"}

Plan: 0 to add, 0 to change, 1 to destroy.
TF Via PR Comments by @RDhar via issue_comment at 2023-11-15T21:21:02Z.
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.sample will be destroyed
  - resource "aws_instance" "sample" {
      - ami                                  = "ami-0f324e1cd5c4465c8" -> null
      - arn                                  = "arn:aws:ec2:us-west-1:813676077823:instance/i-0c6e70cddd2f2cb0e" -> null
      - associate_public_ip_address          = true -> null
      - availability_zone                    = "us-west-1b" -> null
      - cpu_core_count                       = 1 -> null
      - cpu_threads_per_core                 = 1 -> null
      - disable_api_stop                     = false -> null
      - disable_api_termination              = false -> null
      - ebs_optimized                        = false -> null
      - get_password_data                    = false -> null
      - hibernation                          = false -> null
      - id                                   = "i-0c6e70cddd2f2cb0e" -> null
      - instance_initiated_shutdown_behavior = "stop" -> null
      - instance_state                       = "running" -> null
      - instance_type                        = "t2.nano" -> null
      - ipv6_address_count                   = 0 -> null
      - ipv6_addresses                       = [] -> null
      - monitoring                           = false -> null
      - placement_partition_number           = 0 -> null
      - primary_network_interface_id         = "eni-0322842607e86a941" -> null
      - private_dns                          = "ip-172-31-8-229.us-west-1.compute.internal" -> null
      - private_ip                           = "172.31.8.229" -> null
      - public_dns                           = "ec2-54-193-19-181.us-west-1.compute.amazonaws.com" -> null
      - public_ip                            = "54.193.19.181" -> null
      - secondary_private_ips                = [] -> null
      - security_groups                      = [
          - "default",
        ] -> null
      - source_dest_check                    = true -> null
      - subnet_id                            = "subnet-0f62965e88a079565" -> null
      - tags                                 = {
          - "Name" = "unique-instance"
        } -> null
      - tags_all                             = {
          - "Environment" = "Development"
          - "Name"        = "unique-instance"
          - "Stack"       = "sample_instance"
          - "Terraform"   = "dev"
        } -> null
      - tenancy                              = "default" -> null
      - user_data_replace_on_change          = false -> null
      - vpc_security_group_ids               = [
          - "sg-086df1f999f21d675",
        ] -> null

      - capacity_reservation_specification {
          - capacity_reservation_preference = "open" -> null
        }

      - cpu_options {
          - core_count       = 1 -> null
          - threads_per_core = 1 -> null
        }

      - credit_specification {
          - cpu_credits = "standard" -> null
        }

      - enclave_options {
          - enabled = false -> null
        }

      - maintenance_options {
          - auto_recovery = "default" -> null
        }

      - metadata_options {
          - http_endpoint               = "enabled" -> null
          - http_protocol_ipv6          = "disabled" -> null
          - http_put_response_hop_limit = 1 -> null
          - http_tokens                 = "optional" -> null
          - instance_metadata_tags      = "disabled" -> null
        }

      - private_dns_name_options {
          - enable_resource_name_dns_a_record    = false -> null
          - enable_resource_name_dns_aaaa_record = false -> null
          - hostname_type                        = "ip-name" -> null
        }

      - root_block_device {
          - delete_on_termination = true -> null
          - device_name           = "/dev/sda1" -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - tags                  = {} -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-041eab10439985afb" -> null
          - volume_size           = 8 -> null
          - volume_type           = "gp2" -> null
        }
    }

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

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.

@RDhar
Copy link
Member Author

RDhar commented Nov 15, 2023

-tf=apply -destroy -target=aws_instance.sample,data.aws_ami.ubuntu -chdir=sample_instance -workspace=dev -var-file=env/dev.tfvars

Copy link

{"tf":"apply","destroy":true,"target":"aws_instance.sample,data.aws_ami.ubuntu","chdir":"sample_instance","workspace":"dev","var-file":"env/dev.tfvars"}

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
TF Via PR Comments by @RDhar via issue_comment at 2023-11-15T21:22:04Z.
Warning: Applied changes may be incomplete

The plan was created with the -target option in effect, so some changes
requested in the configuration may have been ignored and the output values
may not be fully updated. Run the following command to verify that no other
changes are pending:
    terraform plan
	
Note that the -target option is not suitable 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.

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

Outputs:

sample_instance_id = "i-0c6e70cddd2f2cb0e"

@RDhar RDhar closed this Nov 15, 2023
@RDhar RDhar deleted the scenario branch November 15, 2023 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tf:apply Pull requests that apply TF code tf:plan Pull requests that plan TF code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant