Reputation: 24261
In a Continuous Integration setup (Jenkins) I'd like to deploy (terraform apply
) changes of the same Terraform configuration code using multiple workspaces at the same time using the same copy of the repository - i.e. the very same directory with .tf
files.
Stages are executed in parallel (on same node/agent) and each of them consists of the same sequence of operations (terraform workspace select
and then terraform apply
).
It doesn't seem supported as the attempts fail with lock errors, e.g.
# terraform workspace select "workspace1"
# terraform apply [...] -input=false -auto-approve
Switched to workspace "workspace1".
Acquiring state lock. This may take a few moments...
Error: Error locking state: Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed
Lock Info:
ID: 52973611-a892-deac-985b-5aa28172fdaf
Path: my-project/env:/workspace5/state
Operation: OperationTypeApply
Who: @87ddd2118473
Version: 0.13.5
Created: 2021-01-11 15:20:06.635256155 +0000 UTC
Info:
So it looks like lock for workspace5
gets in the way for applying for workspace1
.
Is there any way out?
I use:
s3
as the backendUpvotes: 5
Views: 1520
Reputation: 160
Just tested this locally so not sure if it will work or a CI system.
Rather than using terraform workspace select xxxx
use the environment variable set for workspaces. So in one session do:
export TF_WORKSPACE=one-environment
terraform apply -input=false -auto-approve
Then in parralel you can do:
export TF_WORKSPACE=another-environment
terraform apply -input=false -auto-approve
This seems to work and doesnt throw any locking errors.
Upvotes: 4
Reputation: 652
The only way I see you able to achieve what you want is by defining a lock resource in Jenkins and then essentially locking that job/step so it will only every execute it one at a time, or by ensuring that the jobs are ran on separate nodes/agents.
Upvotes: 0
Reputation: 24261
One idea I have (which seems to work) is to copy the whole codebase to a separate directory. I.e. each workspace to be applied to each own. And run terraform workspace select
and terraform apply
from separate copies.
It works. But it's lame (inefficient).
Upvotes: 0