@@ -53,13 +53,6 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
53
53
}
54
54
}()
55
55
start := request .GetStart ()
56
- statefilePath := filepath .Join (start .Directory , "terraform.tfstate" )
57
- if len (start .State ) > 0 {
58
- err := os .WriteFile (statefilePath , start .State , 0600 )
59
- if err != nil {
60
- return xerrors .Errorf ("write statefile %q: %w" , statefilePath , err )
61
- }
62
- }
63
56
64
57
terraform , err := tfexec .NewTerraform (start .Directory , t .binaryPath )
65
58
if err != nil {
@@ -246,14 +239,18 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
246
239
errorMessage := err .Error ()
247
240
// Terraform can fail and apply and still need to store it's state.
248
241
// In this case, we return Complete with an explicit error message.
249
- statefileContent , err := os .ReadFile (statefilePath )
242
+ state , err := terraform .Show (stream .Context ())
243
+ if err != nil {
244
+ return xerrors .Errorf ("show state: %w" , err )
245
+ }
246
+ stateData , err := json .Marshal (state )
250
247
if err != nil {
251
- return xerrors .Errorf ("read file %q : %w" , statefilePath , err )
248
+ return xerrors .Errorf ("marshal state : %w" , err )
252
249
}
253
250
return stream .Send (& proto.Provision_Response {
254
251
Type : & proto.Provision_Response_Complete {
255
252
Complete : & proto.Provision_Complete {
256
- State : statefileContent ,
253
+ State : stateData ,
257
254
Error : errorMessage ,
258
255
},
259
256
},
@@ -266,7 +263,7 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
266
263
if start .DryRun {
267
264
resp , err = parseTerraformPlan (stream .Context (), terraform , planfilePath )
268
265
} else {
269
- resp , err = parseTerraformApply (stream .Context (), terraform , statefilePath )
266
+ resp , err = parseTerraformApply (stream .Context (), terraform )
270
267
}
271
268
if err != nil {
272
269
return err
@@ -366,14 +363,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
366
363
}, nil
367
364
}
368
365
369
- func parseTerraformApply (ctx context.Context , terraform * tfexec.Terraform , statefilePath string ) (* proto.Provision_Response , error ) {
370
- statefileContent , err := os . ReadFile ( statefilePath )
366
+ func parseTerraformApply (ctx context.Context , terraform * tfexec.Terraform ) (* proto.Provision_Response , error ) {
367
+ state , err := terraform . Show ( ctx )
371
368
if err != nil {
372
- return nil , xerrors .Errorf ("read file %q: %w" , statefilePath , err )
373
- }
374
- state , err := terraform .ShowStateFile (ctx , statefilePath )
375
- if err != nil {
376
- return nil , xerrors .Errorf ("show state file %q: %w" , statefilePath , err )
369
+ return nil , xerrors .Errorf ("show state file: %w" , err )
377
370
}
378
371
resources := make ([]* proto.Resource , 0 )
379
372
if state .Values != nil {
@@ -508,6 +501,11 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
508
501
}
509
502
}
510
503
504
+ statefileContent , err := json .Marshal (state )
505
+ if err != nil {
506
+ return nil , xerrors .Errorf ("marshal state: %w" , err )
507
+ }
508
+
511
509
return & proto.Provision_Response {
512
510
Type : & proto.Provision_Response_Complete {
513
511
Complete : & proto.Provision_Complete {
0 commit comments