Skip to content

Instantly share code, notes, and snippets.

@rpkim
Created January 8, 2019 12:12
Show Gist options
  • Save rpkim/85732ad40c4bdc034dea0cd67be61b9e to your computer and use it in GitHub Desktop.
Save rpkim/85732ad40c4bdc034dea0cd67be61b9e to your computer and use it in GitHub Desktop.
How to execute partially in Terraform

How to execute the partial execute for Terraform

There is an option -target for targetting the specific resources which you want to create with apply command or plan command. Sometimes, we faced the situation, new resources should be created first and update/destroy should be created later. In case of that, you can use the below commands.

New Resources Only

echo "terraform apply $(terraform plan -no-color | grep '\+ ' | grep -v 'new resource required' | grep -v 'create' | sed 's/\+ /\-target\=/g' | sed 's/$/ \\/g')"

Update Resources Only

echo "terraform apply $(terraform plan -no-color | grep '\~ ' | grep -v 'update in-place' | sed 's/\~ /\-target\=/g' | sed 's/\ //g' | sed 's/$/ \\/g')"

Destory/Create Resources Only

echo "terraform apply $(terraform plan -no-color | grep '\-/\+ ' | grep -v 'destroy and' | sed 's/\-\/\+ /\-target\=/g' | sed 's/\ //g' | sed 's/(newresourcerequired)//g' | sed 's/$/ \\/g')"

Destroy Resources Only

echo "terraform apply $(terraform plan -no-color | grep '\- ' | grep -v 'destroy' | sed 's/\- /\-target\=/g' | sed 's/\ //g' | sed 's/$/ \\/g')"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment