Skip to content

Commit 12a6371

Browse files
KiraLempereur-SpaceliftTheOutdoorProgrammereliecharrapeterdemeDaniellem97
authored
Fix state data location (#1036)
* feat: sync to a bucket unchunked (#997) * feat: add doc about space module permissions (#1002) * docs: Update changelog to include enhanced APM error reporting details (#991) * docs: Update changelog with new features and fixes for Terraform modules and telemetry improvements (#1006) * feat: add support for OpenTofu 1.11.2 in changelog (#1008) * adding clarification to the pr delta comment label (#1007) * feat: add Intent access control docs (#1005) Add a section to the Intent documentation about access control explaining how to setup a custom RBAC role to allow managing Intent projects for users that are not space admins * Changelog: fix(stacks): exclude modules from state preview query (#1010) * Update changelog for task 869brcvge * Update changelog for task 869brcvge * rename --------- Co-authored-by: spacelift-flows-gh-changelog[bot] <253729139+spacelift-flows-gh-changelog[bot]@users.noreply.github.com> Co-authored-by: Krzysztof Niepokojczycki <[email protected]> * Add EKS upgrade policy documentation (#1014) Add informational note explaining the eks_upgrade_policy configuration option in the EKS deployment guide. Clarifies the differences between STANDARD (automatic upgrades, lower cost) and EXTENDED (manual upgrades, higher cost) support types, and notes that switching from extended to standard support is not possible once entered. * fix: clarify EKS upgrade policy details and implications (#1019) * Update TF state management page --------- Co-authored-by: Joey Stout <[email protected]> Co-authored-by: Elie CHARRA <[email protected]> Co-authored-by: Peter Deme <[email protected]> Co-authored-by: Daniellem97 <[email protected]> Co-authored-by: Andrius <[email protected]> Co-authored-by: spacelift-flows-gh-changelog[bot] <253729139+spacelift-flows-gh-changelog[bot]@users.noreply.github.com> Co-authored-by: Krzysztof Niepokojczycki <[email protected]>
1 parent 3829cbf commit 12a6371

File tree

2 files changed

+59
-120
lines changed

2 files changed

+59
-120
lines changed
64.1 KB
Loading
Lines changed: 59 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,117 @@
11
# State management
22

3-
For those of you who don't want to manage Terraform state, Spacelift offers an optional sophisticated state backend
4-
synchronized with the rest of the application to maximize security and convenience. The ability to have Spacelift manage
5-
the state for you is only available during [stack creation](../../concepts/stack/creating-a-stack.md#terraform).
3+
Spacelift offers a sophisticated state backend synchronized with the rest of the application to manage your Terraform state and maximize security and convenience.
64

7-
As you can see, it's also possible to import an existing Terraform state at this point, which is useful for users who
8-
want to upgrade their previous Terraform workflow.
5+
The option to have Spacelift manage the state for you is only available during [stack creation](../../concepts/stack/creating-a-stack.md#opentofuterraform). You can also import an existing Terraform state at this point, which is useful for users who want to upgrade their previous Terraform workflow.
96

107
!!! info
118

129
If you're using Spacelift to manage your stack, do not specify any [Terraform backend](https://www.terraform.io/docs/backends/index.html){: rel="nofollow"} whatsoever. The one-off config will be dynamically injected into every [run](../../concepts/run/README.md) and [task](../../concepts/run/task.md).
1310

14-
## Do. Or do not. There is no try.
11+
## Benefits of Spacelift state management
1512

16-
In this section we'd like to give you a few reasons why it could be useful to trust Spacelift to take care of your
17-
Terraform state. To keep things level, we'll also give you a reason not to.
18-
19-
### Do
20-
21-
1. It's **super simple** - just two clicks during stack setup. Otherwise there's nothing to set up on your end, so one
22-
fewer sensitive thing to worry about. Feel free to refer
23-
to [how it works on our end](state-management.md#how-it-works), but overall we believe it to be a rather sensible and
24-
secure setup, at least on par with anything you could set up on your end.
25-
26-
2. It's **protected against accidental or malicious access**. Again, you can refer to the more technical section on the
27-
inner workings of the state server, but the gist is that we're able to map state access and state changes to
28-
legitimate Spacelift runs, thus automatically blocking all other unauthorized traffic. As far as we know, no other
29-
backend is capable of that, which is one more reason to give us a go.
30-
31-
### Don't
32-
33-
1. We'll let you in on a little secret now - behind the pixie dust it's still Amazon S3 all the way down, and at this
34-
stage [we store all our data in Ireland](../../product/security/README.md). If you're not OK with that, you're better off
35-
managing the state on your end.
13+
1. **Simple and easy setup:** Allow Spacelift to manage your Terraform state during stack creation and you don't need to set anything up on your end.
14+
2. **Protected against accidental or malicious access**: Spacelift maps state access and state changes to legitimate Spacelift runs, thus automatically blocking all other unauthorized traffic.
15+
3. **Encrypted data storage:** Spacelift offers several different [regions for data storage](../../product/security/README.md#encryption) with Amazon S3.
3616

3717
## How it works
3818

39-
S3, like half of the Internet. The pixie dust we're adding on top of it involves generating one-off credentials for
40-
every [run](../../concepts/run/README.md) and [task](../../concepts/run/task.md) and injecting them directly into the
41-
root of your Terraform project as a `.tf` file.
19+
Spacelift state management uses S3, like much of the internet. We generate one-off credentials for every [run](../../concepts/run/README.md) and [task](../../concepts/run/task.md) and injecting them directly into the root of your Terraform project as a `.tf` file.
4220

4321
!!! warning
4422

45-
If you have some Terraform state backend already specified in your code, the initialization phase will keep failing until you remove it.
23+
If you have some Terraform state backend already specified in your code, the [initialization](../../concepts/run/README.md#initializing) phase will keep failing until you remove it.
4624

47-
The state server is an HTTP endpoint implementing the
48-
Terraform [standard state management protocol](https://www.terraform.io/docs/backends/types/http.html){: rel="nofollow"}.
49-
Our backend always ensures that the credentials belong to one of the runs or tasks that are currently marked
50-
as active on our end, and their state indicates that they should be accessing or modifying the state. Once this is
51-
established, we just pass the request to S3 with the right parameters.
25+
The state server is an HTTP endpoint implementing the Terraform [standard state management protocol](https://www.terraform.io/docs/backends/types/http.html){: rel="nofollow"}. Spacelift's backend always ensures:
5226

53-
## State history
27+
- The credentials belong to one of the runs or tasks that are currently marked as active on our end.
28+
- Their state indicates that they should be accessing or modifying the state.
5429

55-
![State history tab](../../assets/screenshots/terraform/state-management/state-history-tab.png)
30+
Once this is established, we pass the request to S3 with the right parameters.
5631

57-
If your state is managed by spacelift, you can list all the changes on your state, and eventually rollback to an old
58-
version if needed.
32+
## State history
5933

60-
![State history list](../../assets/screenshots/terraform/state-management/state-history-list.png)
34+
You can view your stack's state history in Spacelift. Navigate to _Ship Infra_ > _Stacks_, click the name of the stack you want to view the history of, and view the **State history** tab. You'll see a list all the changes to your state and can rollback to an old version if needed.
6135

62-
!!! info
63-
Not all runs or tasks will trigger a new state version, so you should not expect to see an exhaustive list of your runs
64-
and tasks in this list.
65-
For example runs that produce no Terraform changes do not result in a new state version being created.
36+
![State history list](<../../assets/screenshots/terraform/state-management/state-history-list.png>)
37+
38+
Not all runs or tasks will trigger a new state version, so you should not expect to see an exhaustive list of your runs and tasks in this list. For example, runs that produce no Terraform changes do not result in a new state version being created.
6639

6740
Non-current state versions are kept for **30 days**.
6841

6942
### State rollback
7043

71-
In certain unusual scenarios you can end up with a broken or corrupted state being created. This could happen for
72-
example if there was a bug during a Terraform provider upgrade.
73-
74-
State rollback allows you to recover from this by rolling back your state to a previous version.
44+
Although it's rare, you can sometimes end up with a broken or corrupted state, such as when a bug exists in a Terraform provider upgrade. State rollback allows you to recover from this by rolling back your state to a previous version.
7545

76-
Rolling back your state will **not apply any changes to your current infrastructure**. It just reverts your state to an
77-
older version.
78-
It's up to you to trigger the proper tasks or runs to fix the state and re-apply the desired Terraform configuration.
46+
Rolling back your state **will not apply any changes** to your current infrastructure. It just reverts your state to an older version. You must then trigger the proper tasks or runs to fix the state and re-apply the desired Terraform configuration.
7947

8048
!!! warning
81-
You should really understand what you are doing when performing a rollback.
8249

83-
State rollback should be used as a break-glass operation just after a corrupted state has been created.
50+
State rollback should only be used as a break-glass operation just after a corrupted state has been created.
8451

8552
If a stack is currently using a rolled-back state, a warning will be shown in the stack header.
8653

87-
![stack with current state rollback](../../assets/screenshots/terraform/state-management/state-rollback-stack-header.png)
54+
![Stack with current state rollback](<../../assets/screenshots/terraform/state-management/state-rollback-stack-header.png>)
8855

89-
To be able to roll back a state, the 3 conditions below must be satisfied:
56+
To roll back a state, these conditions must be satisfied:
9057

91-
- You must be a stack admin
92-
- The stack must be locked
93-
- The stack must not have any pending runs or tasks
58+
- You must be a stack admin.
59+
- The stack must be locked.
60+
- The stack must not have any pending runs or tasks.
9461

9562
If those three conditions are met, you will be able to rollback your stack to a previous version of your state file.
9663

97-
![state rollback action](../../assets/screenshots/terraform/state-management/state-rollback-action.png)
64+
![state rollback action](<../../assets/screenshots/terraform/state-management/state-rollback-action.png>)
9865

9966
After rollback completes successfully, a new version of your state will appear above the other state versions and will be marked as a rollback.
10067

101-
![rolled back state](../../assets/screenshots/terraform/state-management/rolled-back-state.png)
102-
103-
## Importing resources into your Terraform State
104-
105-
So you have an existing resource that was created by other means and would like that resource to be reflected in your
106-
terraform state. This is an excellent use case for the [terraform import](https://www.terraform.io/cli/import){: rel="nofollow"}
107-
command. When you're managing your own terraform state, you would typically run this command locally to
108-
import said resource(s) to your state file, but what do I do when I'm using Spacelift-managed state you might ask?
109-
Spacelift [Task](../../concepts/run/task.md) to the rescue!
110-
111-
To do this, use the following steps:
68+
![rolled back state](<../../assets/screenshots/terraform/state-management/rolled-back-state.png>)
11269

113-
- Select the Spacelift Stack to which you would like to import state for.
114-
- Within the navigation, select "Tasks"
70+
## Importing resources into your Terraform state
11571

116-
![](<../../assets/screenshots/Screen Shot 2022-02-15 at 10.25.20 AM.png>)
72+
If you have an existing resource that was created by other means, and would like that resource to be reflected in your Terraform state, you need to use the [Terraform import](https://www.terraform.io/cli/import){: rel="nofollow"} command. When managing your own Terraform state, you would typically run this command locally to import said resource(s) to your state file.
11773

118-
- Run the `terraform import` command needed to import your state file to the Spacelift-managed state by typing the
119-
command into the text input and clicking the perform button. Note: If you are using Terragrunt on Spacelift, you will
120-
need to run `terragrunt import`
74+
When Spacelift is managing your Terraform state, you can import resources with [tasks](../../concepts/run/task.md):
12175

122-
![](<../../assets/screenshots/Screen Shot 2022-02-15 at 1.05.23 PM.png>)
76+
1. Select the Spacelift stack to which you would like to import state for.
77+
2. Navigate to the **Tasks** tab.
78+
3. Run the `terraform import` command needed to import your resourcesc by type the `terraform import` command into the text input and click **Perform**.
79+
![Use Terraform import command](<../../assets/screenshots/Screen Shot 2022-02-15 at 1.05.23 PM.png>)
80+
- If you are using Terragrunt on Spacelift, you will need to run `terragrunt import`.
81+
4. Follow the status of your task's execution to ensure it was executed successfully. When completed, you should see an output like this within the "Performing" step of the task.
82+
![Successful Terraform import](<../../assets/screenshots/Screen Shot 2022-02-15 at 1.31.29 PM.png>)
12383

124-
- Follow the status of your task's execution to ensure it was executed successfully. When completed, you should see an
125-
output similar to the following within the "Performing" step of your task.
126-
127-
![](<../../assets/screenshots/Screen Shot 2022-02-15 at 1.31.29 PM.png>)
128-
129-
## Importing existing state file into your Terraform Stacks
84+
## Importing existing state file into your Terraform stacks
13085

13186
### On an existing stack
13287

133-
State import allows you to import a state on top of the latest managed state.
134-
135-
!!! warning
136-
137-
You should really understand what you are doing when importing a state.
138-
139-
To be able to import a state, the 3 conditions below must be satisfied:
88+
State import allows you to import a state on top of the latest managed state. To be able to import a state, these conditions must be satisfied:
14089

141-
- You must be a stack admin
142-
- The stack must be locked
143-
- The stack must not have any pending runs or tasks
144-
145-
To import a new state you need to go to the state history tab on your stack, and then click on the button to import the state.
146-
147-
![](../../assets/screenshots/terraform/state-management/import_state_action.png)
90+
- You must be a stack admin.
91+
- The stack must be locked.
92+
- The stack must not have any pending runs or tasks.
14893

14994
!!! info
15095

15196
The maximum allowed file size for a state is **100MB**.
15297

153-
Then choose a valid terraform state file and upload it.
154-
155-
![](../../assets/screenshots/terraform/state-management/import_state_modal.png)
98+
To import a new state:
15699

157-
Once the file is uploaded, you can click on **Import state**.
100+
1. Navigate to the _State history_ tab on your stack, and then click **Import state**.
101+
![Import state](<../../assets/screenshots/terraform/state-management/import_state_action.png>)
102+
2. Upload a valid Terraform state file.
103+
![Import a state dialog](<../../assets/screenshots/terraform/state-management/import_state_modal.png>)
104+
3. Once the file is uploaded, click **Import state**. The imported state will appear in the list as manually imported.
158105

159-
The imported state will appear in the list as manually imported.
160-
161-
![](../../assets/screenshots/terraform/state-management/import_state_list.png)
106+
![Manually imported state](<../../assets/screenshots/terraform/state-management/import_state_list.png>)
162107

163108
### During stack creation
164109

165-
When creating a stack, you can optionally import an existing Terraform state file so that Spacelift can manage it going
166-
forward.
110+
When creating a stack, you can import an existing Terraform state file so that Spacelift can manage it going forward.
167111

168-
![](../../assets/screenshots/Stack_settings_importing_state_file.png)
112+
![Import during stack creation](<../../assets/screenshots/getting-started/create-stack/import-state-file-create-stack.png>)
169113

170-
You can also import an existing Terraform state file when using Spacelift Terraform provider.
114+
You can also import an existing Terraform state file when using the Spacelift Terraform provider.
171115

172116
```terraform title="stack.tf"
173117
resource "spacelift_stack" "example-stack" {
@@ -189,29 +133,24 @@ resource "spacelift_stack" "example-stack" {
189133

190134
If you enable [external state access](external-state-access.md), you can export the stack's state from outside of Spacelift.
191135

192-
If a Terraform stack's state is managed by Spacelift and you need to export it you can do so by running the following
193-
command in a [Task](../../concepts/run/task.md#performing-a-task):
136+
If a Terraform stack's state is managed by Spacelift and you need to export it, you can run this command in a [task](../../concepts/run/task.md#performing-a-task):
194137

195138
```shell
196139
terraform state pull > terraform.tfstate
197140
```
198141

199-
The local workspace is discarded after the Task has finished so you most likely want to combine this command with
200-
another one that pushes the `terraform.tfstate` file to some remote location.
201-
202-
Here is an example of pushing the state file to an AWS S3 bucket (without using an intermediary file):
142+
The local workspace is discarded after the task has finished, so you most likely want to combine it with another one that pushes the `terraform.tfstate` file to a remote location. Here is an example of pushing the state file to an AWS S3 bucket (without using an intermediary file):
203143

204144
```shell
205145
terraform state pull | aws s3 cp - s3://example-bucket/folder/sub-folder/terraform.tfstate
206146
```
207147

208-
### Configure terraform plan locking
148+
### Configure Terraform plan locking
149+
150+
By default, `terraform plan` acquires a state lock. If you want to disable such lock during planning, you can pass `SPACELIFT_DISABLE_STATE_LOCK` to the stack _Environment_.
209151

210-
By default `terraform plan` acquires a state lock. If you want to disable such lock during planning,
211-
you can pass `SPACELIFT_DISABLE_STATE_LOCK` to the stack _Environment_.
212-
![](../../assets/screenshots/disable-state-lock-in-stack.png)
152+
![Disable state locking via variables](<../../assets/screenshots/disable-state-lock-in-stack.png>)
213153

214154
!!! warning
215155

216-
You should really understand what you are doing when disabling state lock for planning.
217-
Disabling the lock may lead to incorrect results in case of concurrent apply operation to the state.
156+
Disabling the planning state lock may lead to incorrect results in a concurrent apply operation to the state.

0 commit comments

Comments
 (0)