|
| 1 | +--- |
| 2 | +title: "Managing state" |
| 3 | +--- |
| 4 | + |
| 5 | +In this secion we cover approaces to manage state with digger's automation. |
| 6 | +Currently digger does not interfere with terraform's state management and we leave it up to the user |
| 7 | +to manage the state on their own accounts. This is because we realised that most of the users come to us with state |
| 8 | +already managed somewhere, usually an S3 bucket in their own account, and we didn't want to add an extra migration step whilst moving to digger. |
| 9 | + |
| 10 | +With that said, we do facilitate state management configuration within digger in order to make it easy for teams to manage it on their own account. |
| 11 | +This guide will cover an approach to facilitate this state management. |
| 12 | + |
| 13 | +The example repo for this is here: https://github.com/diggerhq/states-test |
| 14 | + |
| 15 | +In this example we have the followign directory structure: |
| 16 | + |
| 17 | +``` |
| 18 | +dev/ |
| 19 | + main.tf |
| 20 | + tf_backend.tfbackend |
| 21 | +staging/ |
| 22 | + main.tf |
| 23 | + tf_backend.tfbackend |
| 24 | +prod/ |
| 25 | + main.tf |
| 26 | + tf_backend.tfbackend |
| 27 | +``` |
| 28 | + |
| 29 | +within each main.tf root module we define a backend block: |
| 30 | +``` |
| 31 | +terraform { |
| 32 | + backend "s3" { |
| 33 | +
|
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +We ommit the backend state name, key and region on purpose since it is defined in the file tf_backend.tfbackend within the same directory: |
| 39 | + |
| 40 | +``` |
| 41 | +bucket="digger-state-test" |
| 42 | +key="/dev/terraform.tfstate" |
| 43 | +region="us-east-1" |
| 44 | +``` |
| 45 | + |
| 46 | +This is done in staging/ and prod/ as well. We consider it as a convention for all the root modules. |
| 47 | + |
| 48 | +With that in place, we can configure digger to pass this additional configuration while running terraform as follows: |
| 49 | + |
| 50 | +``` |
| 51 | +projects: |
| 52 | + - name: "dev" |
| 53 | + dir: "dev" |
| 54 | + - name: "staging" |
| 55 | + dir: "staging" |
| 56 | + - name: "prod" |
| 57 | + dir: "prod" |
| 58 | +
|
| 59 | +
|
| 60 | +workflows: |
| 61 | + default: |
| 62 | + workflow_configuration: |
| 63 | + on_pull_request_pushed: ["digger plan"] |
| 64 | + on_pull_request_closed: ["digger unlock"] |
| 65 | + on_commit_to_default: ["digger unlock"] |
| 66 | +
|
| 67 | + plan: |
| 68 | + steps: |
| 69 | + - init: |
| 70 | + extra_args: ["-backend-config=tf_backend.tfbackend" ] |
| 71 | + - plan: |
| 72 | + apply: |
| 73 | + steps: |
| 74 | + - init: |
| 75 | + extra_args: ["-backend-config=tf_backend.tfbackend" ] |
| 76 | + - apply: |
| 77 | +``` |
| 78 | + |
| 79 | +The key part here is that we override the default workflow and pass extra arguments of `-backend-config=tf_backend.tfbackend` to the `plan` and `apply` steps. |
| 80 | +In this way it is easy to add additional states simply by adding a record for them in digger.yml. Once a PR is created and applied we will end up with a bucket that has three state files: |
| 81 | + |
| 82 | + |
0 commit comments