terrakube icon indicating copy to clipboard operation
terrakube copied to clipboard

Statefile not getting created in S3 Compatible Backend after Apply

Open MARVELMafia opened this issue 10 months ago • 9 comments

Feedback

After being able to create IaaC I am only able to see the state of the deployment on Terrakube UI. But I dont see anything on my S3 Compatible backend storage.

The log on the executor are as follows:

2024-04-11 03:41:17.769 ERROR 1 --- [nio-8080-exec-3] o.t.a.p.s.aws.AwsStorageTypeServiceImpl : S3 Not found: The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: 17C51CBE85693434; S3 Extended Request ID: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855; Proxy: null) 2024-04-11 03:41:18.368 INFO 1 --- [nio-8080-exec-1] o.t.a.p.s.c.TerraformOutputController : Reading output from storage

I have tried with both remote and local execution.

Also what is meant by local execution? does it execute on the kubernetes worker node?

MARVELMafia avatar Apr 11 '24 04:04 MARVELMafia

The local or remote execution mode is used with the CLI driven workflow to set where terraform will be executed, for example if terraform will run locally and just sent the updated state to terrakube or remote if the request will be sent to terrakube and the executor component will run it remotely.

https://docs.terrakube.io/user-guide/workspaces/cli-driven-workflow

For the other comment what do you mean when you mention you don't see anything in the backend storage? You should be able to see the logs, state, state history inside the storage.

alfespa17 avatar Apr 11 '24 13:04 alfespa17

Right I see. I do not need a cli driven workflow and terraform can execute remotely but the statefile should get created in the S3 Bucket I specified in my .tf script as well as be able to view it in terrakube right?

image

As mentioned, I can see the statefile in the terrakube UI

image

But when I look into the bucket via the S3 Browser I do not see any statefile getting created

image

MARVELMafia avatar Apr 11 '24 23:04 MARVELMafia

Right I see. I do not need a cli driven workflow and terraform can execute remotely but the statefile should get created in the S3 Bucket I specified in my .tf script as well as be able to view it in terrakube right?

image

As mentioned, I can see the statefile in the terrakube UI

image

But when I look into the bucket via the S3 Browser I do not see any statefile getting created

image

Ok let me explain this, terrakube internally will take care of the state file using the terrakube storage backend that you have selected when you deployed terrakube (azure, gcp, S3 or minio) you don't have to write the S3 backend like the above example.

Instead of writing the backend like you have above you could use a backend like the following to set the workspace that will save the information for your infrastructure:

terraform {
  backend "remote" {
    hostname = "8080-azbuilder-terrakube-po7evw1u15x.ws-us86.gitpod.io"
    organization = "simple"
    workspaces {
      name = "workspace1"
    }
  }
}

terraform {
  cloud {
    organization = "simple"
    hostname = "8080-azbuilder-terrakube-sscrnu9jbie.ws-us99.gitpod.io"
    workspaces {
      name = "samplecloud"
    }
  }
}

terraform {
  cloud {
    organization = "simple"
    hostname = "8080-azbuilder-terrakube-0musnfsxh7g.ws-us102.gitpod.io"

    workspaces {
      tags = ["development", "networking"]
    }
  }
}

To use the above examples you just need to run

terraform login TERRAKUBE-API

alfespa17 avatar Apr 12 '24 02:04 alfespa17

Hi, I have set my backend as follows and I can see the statefiles getting created when I look into the bucket.

storage:
  defaultStorage: true
  minio:
    accessKey: "X"
    secretKey: "Y"
    bucketName: "iaac-infrastructure"
    endpoint: "Z"
  default:
    endpoint: "Z"

image

Next I want to create the workspace name in the bucket instead of the default folders (34,35,37 etc) where the statefile gets saved.

According to your information above - what hostname is it talking about?

terraform {
  backend "minio" {
    hostname = <what hostname?>
    organization = "simple"
    workspaces {
      name = "Test-VM"
    }
  }
}

Ideally what I want to see is a folder made for the workspace TEST-VM which contains the statefile for the applied terraform code.

image

MARVELMafia avatar Apr 14 '24 23:04 MARVELMafia

You can use something like the following to manage the terraform backend for your code:

terraform {
  cloud {
    organization = "simple"
    hostname = "TERRAKUBE API HOSTNAME"
    workspaces {
      name = "TESTVM"
    }
  }
}

I see you are using the default storage, so it will use MINIO to save the statefile and all the related information like logs, state history, etc.

Terrakube will handle internally all the file in some folders inside the MINIO storage including the state file in a path like

state/terrakube org id/terrakube workspace id/....

alfespa17 avatar Apr 15 '24 00:04 alfespa17

Sorry but why would it be

terraform {
  cloud {
    organization = "simple"
    hostname = "terrakube-api-service"
    workspaces {
      name = "TESTVM"
    }
  }
}

and not

terraform {
  backend "minio" {
    hostname = "terrakube-api-service"
    organization = "simple"
    workspaces {
      name = "Test-VM"
    }
  }
}

MARVELMafia avatar Apr 15 '24 03:04 MARVELMafia

Sorry but why would it be

terraform {
  cloud {
    organization = "simple"
    hostname = "terrakube-api-service"
    workspaces {
      name = "TESTVM"
    }
  }
}

and not

terraform {
  backend "minio" {
    hostname = "terrakube-api-service"
    organization = "simple"
    workspaces {
      name = "Test-VM"
    }
  }
}

Because terrakube implement the cloud backend as explain here

https://docs.terrakube.io/user-guide/workspaces/cli-driven-workflow

alfespa17 avatar Apr 15 '24 03:04 alfespa17

You can use something like the following to manage the terraform backend for your code:

terraform {
  cloud {
    organization = "simple"
    hostname = "TERRAKUBE API HOSTNAME"
    workspaces {
      name = "TESTVM"
    }
  }
}

I see you are using the default storage, so it will use MINIO to save the statefile and all the related information like logs, state history, etc.

Terrakube will handle internally all the file in some folders inside the MINIO storage including the state file in a path like

state/terrakube org id/terrakube workspace id/....

Right I see.

So I used

cloud {
    organization = "<same name as organization defined in terrakube UI>"
    hostname = "terrakube-api-service" (name of the kubernetes service that points to the terrakube-api  pod)
    workspaces {
      name = "<same workspace name as defined in terrakube UI>"
    }
  }

However , in the bucket I still see the statefile being created with the default terrakube format

image

MARVELMafia avatar Apr 15 '24 04:04 MARVELMafia

You can use something like the following to manage the terraform backend for your code:

terraform {
  cloud {
    organization = "simple"
    hostname = "TERRAKUBE API HOSTNAME"
    workspaces {
      name = "TESTVM"
    }
  }
}

I see you are using the default storage, so it will use MINIO to save the statefile and all the related information like logs, state history, etc. Terrakube will handle internally all the file in some folders inside the MINIO storage including the state file in a path like

state/terrakube org id/terrakube workspace id/....

Right I see.

So I used

cloud {
    organization = "<same name as organization defined in terrakube UI>"
    hostname = "terrakube-api-service" (name of the kubernetes service that points to the terrakube-api  pod)
    workspaces {
      name = "<same workspace name as defined in terrakube UI>"
    }
  }

However , in the bucket I still see the statefile being created with the default terrakube format

image

You can't change the logic that terrakube uses to manage the state internally

alfespa17 avatar Apr 15 '24 16:04 alfespa17