$ terraform destroyってやると全部消えますが、特定のインスタンスだけ消したい場合は-targetを付けてやればdestroyされます。公式は見づらくて見つけるの大変だった..
■Command: destroy
https://www.terraform.io/docs/commands/destroy.html
The -target flag, instead of affecting “dependencies” will instead also destroy any resources that depend on the target(s) specified.
これかーい!
■terraform destroy -target
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
adachin$ terraform destroy -target=aws_instance.adachin-web01 aws_vpc: Refreshing state... (ID: vpc-xxxxxxxx) aws_security_group Refreshing state... (ID: xxxxxxxxx) aws_subnet.public-a: Refreshing state... (ID: subnet-xxxxxxxxxxx) aws_instance.adachin-web01: Refreshing state... (ID: i-xxxxxxxxxxxxxxx) An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: - aws_cloudwatch_metric_alarm.adachin-web01_CPU - aws_cloudwatch_metric_alarm.adachin-web01_Status - aws_eip.adachin-web01 - aws_instance.adachin-web01 Plan: 0 to add, 0 to change, 4 to destroy. Do you really want to destroy? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: no #一旦noでw Error: Destroy cancelled. |
EC2に紐付いてるものも抹消されます。複数やりたい場合は以下です。
1 |
$ terraform destroy -target=[リソース名] -target=[リソース名] |
ちなみにリソース名を知りたい場合は以下。
1 |
$ terraform plan -destroy |
■まとめ
-targetは冪等性に違反するのであまりやってはならん。(テストならじゃんじゃん)applyも-target使えますな。
0件のコメント