Should terraform be used for deployments?

Is it generally a good idea to use terraform for deployments?
Or terraform is to setup your infrastructure, now you can deploy using CI/CD and not terraform itself?

If terraform is used for deployment, any best practices? i.e. always scared if my database or s3 buckets get wiped out

It is technically possible to use Terraform to deploy applications if the deployment is done using an API that has a Terraform provider, but Terraform lacks some concepts that are often useful for deployments:

  • There’s no inherent history of what was previously deployed and when; Terraform only cares about the current state, the desired state, and the differences between the two.
  • Consequently, there is no built-in idea if “rolling back” to a previous known good version. You’d need to implement this yourself somehow.
  • Terraform calculates “desired state” using a combination of your configuration, the input variables, and the data sources it refers to, so when using it for deployment you need to find some way to cause one of those three to change.

All of this means that you can potentially use Terraform as part of a solution for deployments, but you will typically need to build some other automation around it that fills some or all of the gaps I’ve described above.

For some systems Terraform is a useful building block due to the target API being complex to interact with, but for other systems it just adds unnecessary complexity when compared to just making your release automation interact directly with the system being deployed to.

1 Like