0

While, I was working with Terraform I had a question; I will be able to destroy some specific resources using terraform destroy --target [] --target [] or terraform state -rm ; this is okay if we have 50 servers but what if I have 1000 servers and would like to terminate such as odd number instances or even number instances using the array numbers in the list or could we write a script to gather all the corrupted instances and execute that script to terminate all those instances and make that script reusable!!

Is there any way for this, I have searched all over the internet but couldn't find any solution; may be this question is dumb but I was just curious!!!!

Is there any documentation that explains this is would not be possible through terraform!!!!

1

1 Answer 1

0

You could expose the count as an output:

output "server_count" {
  value = var.server_count
}

and write a script (shell/Python/etc) that takes that count as an argument and uses it to taint every odd resource:

#!/bin/bash
# usage: taint_odd_servers.sh <num servers>

SERVER_COUNT=$1

i=0
while [ $i -lt $SERVER_COUNT ]
do
  REMAINDER=$(( $i % 2 ))
  if [ $REMAINDER -ne 0 ]
  then
    terraform taint "your_server_resource[${i}]"
  fi
  i=$(($i+1))
done

You could then call that script like:

taint_odd_servers.sh $(terraform output server_count)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.