Terraform Provisioning with Harness
Harness lets you use Terraform to provision infrastructure as part of your deployment process. Harness can provision any resource that is supported by a Terraform provider or plugin.
Terraform Provisioning Options
You can use Harness with Terraform in two ways:
- Target Infra Provisioning: provision the target infrastructure for a deployment, and then deploy to that provisioned infrastructure.
- Non-target Provisioning: provision any resources other than the target infrastructure for the deployment.
You can do both in the same stage if you want.
Terraform Target Infra Provisioning Summary
You set up a Terraform target infrastructure provisioning in the following order:
- Select Dynamic Provisioning. In the Pipeline Infrastructure, you select the Dynamic Provisioning option and select Terraform. Harness automatically adds the Terraform Plan, Harness Approval, and Terraform Apply steps. You can change these steps, but plan, approve, and apply is the most common process. We use that process in our Terraform documentation.
- In the Terraform Plan step, you link Harness to the Terraform scripts you want to use. You add the scripts by connecting to a Git repo where the scripts are kept and setting up any inputs and other common options.
- Map outputs to the target Infrastructure. Harness needs a few script outputs so that it can target the provisioned infrastructure, such as namespace. You simply map some script outputs to the required Harness target infrastructure settings.
- Deployment. The Pipeline deploys to the provisioned infrastructure defined in its target Infrastructure Definition.
Use Terraform for Non-deployment Provisioning
You can use the Harness Terraform steps to provision any non-target resources also. You simply add the steps in the stage Execution.
See Terraform How-tos.
When rollback happens, Harness rolls back the provisioned infrastructure to the previous successful version of the Terraform state.
Harness won't increment the serial in the state, but perform a hard rollback to the exact version of the state provided.
Harness determines what to rollback using the Provision Identifier entered in the Terraform Rollback step.
If you've made these settings using Harness expressions, Harness uses the values it obtains at runtime when it evaluates the expression.
Let's say you deployed two modules successfully already: module1 and module2. Next, you try to deploy module3, but deployment failed. Harness will roll back to the successful state of module1 and module2.
However, let's look at the situation where module3 succeeds and now you have module1, module2, and module3 deployed. If the next deployment fails, the rollback will only roll back to the Terraform state with module3 deployed. Module1 and module2 weren't in the previous Terraform state, so the rollback excludes them.