Exclude option in Terraform Apply

Is there any option to exclude specific resource in Terraform Apply and destroy commands ? I am aware of Target option but wanted to check if there is a EXCLUDE option

Hi @anvesh.kothapelly,

There is no “negative target” / exclude option in Terraform.

Routine use of Terraform should always work on the entire configuration; the target option is only there for exceptional use to work around bugs, recover from mistakes, etc.

If you can say more about what problem you are trying to solve for which you considered excluding as a solution then I may be able to suggest a different way to achieve that goal.

@apparentlymart Thank you for your response. We have multiple AWS resources provisioned using Terraform out of which AWS Transfer Family SFTP server one of the resource. Currently the SFTP server runs 24*7 which costs us more. But SFTP is required only for few hours a day. So as part of our CI/CD pipeline we want to have two TF apply Cmds one a regular TF apply which creates all resources except SFTP server and a separate scheduled ( Cron) TF apply and destroy for SFTP server.

Hi @anvesh.kothapelly,

A typical way to achieve what you are describing would be to split the part you will create and destroy regularly into its own configuration, which can use data sources to locate the long-living objects described in the first configuration.

A good general design rule for Terraform is that things which need to create, change, or destroy together should be placed in the same configuration, whereas things which should have a separate lifecycle should be separated into their own configurations.

With that done, you can safely run terraform destroy on the child configuration without affecting the long-lived parent. This is particularly useful for automated create/destroy processes because you can be sure that the automation will only destroy objects that are tracked in the separate state of the child configuration.

A post was split to a new topic: Data sources forcing replacement even though nothing has changed