Skip to content

Commit a367b5b

Browse files
Enhance README to take advantage of terraform.tf (#46)
Co-authored-by: John McGehee <[email protected]>
1 parent 89eefd8 commit a367b5b

File tree

4 files changed

+160
-42
lines changed

4 files changed

+160
-42
lines changed

README.md

+80-21
Original file line numberDiff line numberDiff line change
@@ -101,39 +101,94 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are
101101
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-aws-tfstate-backend/releases).
102102

103103

104-
1. Define the module in your `.tf` file using local state:
104+
105+
### Create
106+
107+
Follow this procedure just once to create your deployment.
108+
109+
1. Add the `terraform_state_backend` module to your `main.tf` file. The
110+
comment will help you remember to follow this procedure in the future:
105111
```hcl
112+
# You cannot create a new backend by simply defining this and then
113+
# immediately proceeding to "terraform apply". The S3 backend must
114+
# be bootstrapped according to the simple yet essential procedure in
115+
# https://github.com/cloudposse/terraform-aws-tfstate-backend#usage
106116
module "terraform_state_backend" {
107117
source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=master"
108118
namespace = "eg"
109119
stage = "test"
110120
name = "terraform"
111121
attributes = ["state"]
112122
region = "us-east-1"
113-
}
114-
```
115-
116-
1. `terraform init`
117-
118-
1. `terraform apply`. This will create the state bucket and locking table.
119-
120-
1. Then add a `backend` that uses the new bucket and table:
121-
```hcl
122-
backend "s3" {
123-
region = "us-east-1"
124-
bucket = "< the name of the S3 bucket >"
125-
key = "terraform.tfstate"
126-
dynamodb_table = "< the name of the DynamoDB table >"
127-
encrypt = true
128-
}
123+
terraform_backend_config_file_path = "."
124+
terraform_backend_config_file_name = "backend.tf"
125+
force_destroy = false
129126
}
130127
128+
# Your Terraform configuration
131129
module "another_module" {
132130
source = "....."
133131
}
134132
```
133+
Module inputs `terraform_backend_config_file_path` and
134+
`terraform_backend_config_file_name` control the name of the backend
135+
definition file. Note that when `terraform_backend_config_file_path` is
136+
empty (the default), no file is created.
137+
138+
1. `terraform init`. This downloads Terraform modules and providers.
139+
140+
1. `terraform apply -auto-approve`. This creates the state bucket and DynamoDB locking
141+
table, along with anything else you have defined in your `*.tf` file(s). At
142+
this point, the Terraform state is still stored locally.
143+
144+
Module `terraform_state_backend` also creates a new `backend.tf` file
145+
that defines the S3 state backend. For example:
146+
```hcl
147+
backend "s3" {
148+
region = "us-east-1"
149+
bucket = "< the name of the S3 state bucket >"
150+
key = "terraform.tfstate"
151+
dynamodb_table = "< the name of the DynamoDB locking table >"
152+
profile = ""
153+
role_arn = ""
154+
encrypt = true
155+
}
156+
```
157+
158+
Henceforth, Terraform will also read this newly-created backend definition
159+
file.
160+
161+
1. `terraform init -force-copy`. Terraform detects that you want to move your
162+
Terraform state to the S3 backend, and it does so per `-auto-approve`. Now the
163+
state is stored in the S3 bucket, and the DynamoDB table will be used to lock
164+
the state to prevent concurrent modification.
165+
166+
This concludes the one-time preparation. Now you can extend and modify your
167+
Terraform configuration as usual.
135168

136-
1. `terraform init`. Terraform will detect that you're trying to move your state into S3 and ask, "Do you want to copy existing state to the new backend?" Enter "yes". Now state is stored in the bucket and the DynamoDB table will be used to lock the state to prevent concurrent modifications.
169+
### Destroy
170+
171+
Follow this procedure to delete your deployment.
172+
173+
1. In `main.tf`, change the `terraform_state_backend` module arguments as
174+
follows:
175+
```hcl
176+
module "terraform_state_backend" {
177+
...
178+
terraform_backend_config_file_path = ""
179+
force_destroy = true
180+
}
181+
```
182+
1. `terraform apply -target module.terraform_state_backend -auto-approve`.
183+
This implements the above modifications by deleting the `backend.tf` file
184+
and enabling deletion of the S3 state bucket.
185+
1. `terraform init -force-copy`. Terraform detects that you want to move your
186+
Terraform state from the S3 backend to local files, and it does so per
187+
`-auto-approve`. Now the state is once again stored locally and the S3
188+
state bucket can be safely deleted.
189+
1. `terraform destroy`. This deletes all resources in your deployment.
190+
1. Examine local state file `terraform.tfstate` to verify that it contains
191+
no resources.
137192
138193
<br/>
139194
@@ -206,7 +261,7 @@ Available targets:
206261
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `""` | no |
207262
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
208263
| terraform\_backend\_config\_file\_name | Name of terraform backend config file | `string` | `"terraform.tf"` | no |
209-
| terraform\_backend\_config\_file\_path | The path to terrafrom project directory | `string` | `""` | no |
264+
| terraform\_backend\_config\_file\_path | Directory for the terraform backend config file, usually `.`. The default is to create no file. | `string` | `""` | no |
210265
| terraform\_backend\_config\_template\_file | The path to the template used to generate the config file | `string` | `""` | no |
211266
| terraform\_state\_file | The path to the state file inside the bucket | `string` | `"terraform.tfstate"` | no |
212267
| terraform\_version | The minimum required terraform version | `string` | `"0.12.2"` | no |
@@ -369,8 +424,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
369424
370425
### Contributors
371426
372-
| [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Chris Weyl][rsrchboy_avatar]][rsrchboy_homepage]<br/>[Chris Weyl][rsrchboy_homepage] |
373-
|---|---|---|---|---|
427+
| [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Chris Weyl][rsrchboy_avatar]][rsrchboy_homepage]<br/>[Chris Weyl][rsrchboy_homepage] | [![John McGehee][jmcgeheeiv_avatar]][jmcgeheeiv_homepage]<br/>[John McGehee][jmcgeheeiv_homepage] | [![Oliver L Schoenborn][schollii_avatar]][schollii_homepage]<br/>[Oliver L Schoenborn][schollii_homepage] |
428+
|---|---|---|---|---|---|---|
374429
375430
[aknysh_homepage]: https://github.com/aknysh
376431
[aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png
@@ -382,6 +437,10 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
382437
[SweetOps_avatar]: https://img.cloudposse.com/150x150/https://github.com/SweetOps.png
383438
[rsrchboy_homepage]: https://github.com/rsrchboy
384439
[rsrchboy_avatar]: https://img.cloudposse.com/150x150/https://github.com/rsrchboy.png
440+
[jmcgeheeiv_homepage]: https://github.com/jmcgeheeiv
441+
[jmcgeheeiv_avatar]: https://img.cloudposse.com/150x150/https://github.com/jmcgeheeiv.png
442+
[schollii_homepage]: https://github.com/schollii
443+
[schollii_avatar]: https://img.cloudposse.com/150x150/https://github.com/schollii.png
385444
386445
[![README Footer][readme_footer_img]][readme_footer_link]
387446
[![Beacon][beacon]][website]

README.yaml

+78-19
Original file line numberDiff line numberDiff line change
@@ -55,40 +55,95 @@ description: |-
5555
5656
# How to use this project
5757
usage: |-
58-
1. Define the module in your `.tf` file using local state:
58+
59+
### Create
60+
61+
Follow this procedure just once to create your deployment.
62+
63+
1. Add the `terraform_state_backend` module to your `main.tf` file. The
64+
comment will help you remember to follow this procedure in the future:
5965
```hcl
66+
# You cannot create a new backend by simply defining this and then
67+
# immediately proceeding to "terraform apply". The S3 backend must
68+
# be bootstrapped according to the simple yet essential procedure in
69+
# https://github.com/cloudposse/terraform-aws-tfstate-backend#usage
6070
module "terraform_state_backend" {
6171
source = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=master"
6272
namespace = "eg"
6373
stage = "test"
6474
name = "terraform"
6575
attributes = ["state"]
6676
region = "us-east-1"
67-
}
68-
```
69-
70-
1. `terraform init`
71-
72-
1. `terraform apply`. This will create the state bucket and locking table.
73-
74-
1. Then add a `backend` that uses the new bucket and table:
75-
```hcl
76-
backend "s3" {
77-
region = "us-east-1"
78-
bucket = "< the name of the S3 bucket >"
79-
key = "terraform.tfstate"
80-
dynamodb_table = "< the name of the DynamoDB table >"
81-
encrypt = true
82-
}
77+
terraform_backend_config_file_path = "."
78+
terraform_backend_config_file_name = "backend.tf"
79+
force_destroy = false
8380
}
8481
82+
# Your Terraform configuration
8583
module "another_module" {
8684
source = "....."
8785
}
8886
```
87+
Module inputs `terraform_backend_config_file_path` and
88+
`terraform_backend_config_file_name` control the name of the backend
89+
definition file. Note that when `terraform_backend_config_file_path` is
90+
empty (the default), no file is created.
91+
92+
1. `terraform init`. This downloads Terraform modules and providers.
93+
94+
1. `terraform apply -auto-approve`. This creates the state bucket and DynamoDB locking
95+
table, along with anything else you have defined in your `*.tf` file(s). At
96+
this point, the Terraform state is still stored locally.
97+
98+
Module `terraform_state_backend` also creates a new `backend.tf` file
99+
that defines the S3 state backend. For example:
100+
```hcl
101+
backend "s3" {
102+
region = "us-east-1"
103+
bucket = "< the name of the S3 state bucket >"
104+
key = "terraform.tfstate"
105+
dynamodb_table = "< the name of the DynamoDB locking table >"
106+
profile = ""
107+
role_arn = ""
108+
encrypt = true
109+
}
110+
```
89111
90-
1. `terraform init`. Terraform will detect that you're trying to move your state into S3 and ask, "Do you want to copy existing state to the new backend?" Enter "yes". Now state is stored in the bucket and the DynamoDB table will be used to lock the state to prevent concurrent modifications.
91-
112+
Henceforth, Terraform will also read this newly-created backend definition
113+
file.
114+
115+
1. `terraform init -force-copy`. Terraform detects that you want to move your
116+
Terraform state to the S3 backend, and it does so per `-auto-approve`. Now the
117+
state is stored in the S3 bucket, and the DynamoDB table will be used to lock
118+
the state to prevent concurrent modification.
119+
120+
This concludes the one-time preparation. Now you can extend and modify your
121+
Terraform configuration as usual.
122+
123+
### Destroy
124+
125+
Follow this procedure to delete your deployment.
126+
127+
1. In `main.tf`, change the `terraform_state_backend` module arguments as
128+
follows:
129+
```hcl
130+
module "terraform_state_backend" {
131+
...
132+
terraform_backend_config_file_path = ""
133+
force_destroy = true
134+
}
135+
```
136+
1. `terraform apply -target module.terraform_state_backend -auto-approve`.
137+
This implements the above modifications by deleting the `backend.tf` file
138+
and enabling deletion of the S3 state bucket.
139+
1. `terraform init -force-copy`. Terraform detects that you want to move your
140+
Terraform state from the S3 backend to local files, and it does so per
141+
`-auto-approve`. Now the state is once again stored locally and the S3
142+
state bucket can be safely deleted.
143+
1. `terraform destroy`. This deletes all resources in your deployment.
144+
1. Examine local state file `terraform.tfstate` to verify that it contains
145+
no resources.
146+
92147
<br/>
93148
94149
![s3-bucket-with-terraform-state](images/s3-bucket-with-terraform-state.png)
@@ -110,3 +165,7 @@ contributors:
110165
github: "SweetOps"
111166
- name: "Chris Weyl"
112167
github: "rsrchboy"
168+
- name: "John McGehee"
169+
github: "jmcgeheeiv"
170+
- name: "Oliver L Schoenborn"
171+
github: "schollii"

docs/terraform.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `""` | no |
5151
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
5252
| terraform\_backend\_config\_file\_name | Name of terraform backend config file | `string` | `"terraform.tf"` | no |
53-
| terraform\_backend\_config\_file\_path | The path to terrafrom project directory | `string` | `""` | no |
53+
| terraform\_backend\_config\_file\_path | Directory for the terraform backend config file, usually `.`. The default is to create no file. | `string` | `""` | no |
5454
| terraform\_backend\_config\_template\_file | The path to the template used to generate the config file | `string` | `""` | no |
5555
| terraform\_state\_file | The path to the state file inside the bucket | `string` | `"terraform.tfstate"` | no |
5656
| terraform\_version | The minimum required terraform version | `string` | `"0.12.2"` | no |

variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ variable "terraform_backend_config_file_name" {
200200
variable "terraform_backend_config_file_path" {
201201
type = string
202202
default = ""
203-
description = "The path to terrafrom project directory"
203+
description = "Directory for the terraform backend config file, usually `.`. The default is to create no file."
204204
}
205205

206206
variable "terraform_backend_config_template_file" {

0 commit comments

Comments
 (0)