Skip to content

Commit 45c48b0

Browse files
Gowiemactions-bot
andauthored
Adds check to only write backend config file if path doesn't exist (#53)
Co-authored-by: actions-bot <[email protected]>
1 parent abb5af7 commit 45c48b0

File tree

5 files changed

+60
-59
lines changed

5 files changed

+60
-59
lines changed

README.md

+42-57
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
<!--
2-
3-
4-
5-
6-
7-
8-
1+
# terraform-aws-tfstate-backend [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-aws-tfstate-backend.svg)](https://github.com/cloudposse/terraform-aws-tfstate-backend/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
92

3+
[![README Header][readme_header_img]][readme_header_link]
104

5+
[![Cloud Posse][logo]](https://cpco.io/homepage)
116

7+
<!--
128
139
1410
1511
1612
** DO NOT EDIT THIS FILE
17-
**
18-
** This file was automatically generated by the `build-harness`.
19-
** 1) Make all changes to `README.yaml`
13+
**
14+
** This file was automatically generated by the `build-harness`.
15+
** 1) Make all changes to `README.yaml`
2016
** 2) Run `make init` (you only need to do this once)
21-
** 3) Run`make readme` to rebuild this file.
17+
** 3) Run`make readme` to rebuild this file.
2218
**
2319
** (We maintain HUNDREDS of open source projects. This is how we maintain our sanity.)
2420
**
@@ -27,23 +23,7 @@
2723
2824
2925
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
-->
41-
[![README Header][readme_header_img]][readme_header_link]
42-
43-
[![Cloud Posse][logo]](https://cpco.io/homepage)
44-
45-
# terraform-aws-tfstate-backend [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-aws-tfstate-backend.svg)](https://github.com/cloudposse/terraform-aws-tfstate-backend/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
46-
26+
-->
4727

4828
Terraform module to provision an S3 bucket to store `terraform.tfstate` file and a DynamoDB table to lock the state file
4929
to prevent concurrent modifications and state corruption.
@@ -65,7 +45,7 @@ __NOTE:__ This module cannot be used to apply changes to the `mfa_delete` featur
6545

6646
---
6747

68-
This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps.
48+
This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps.
6949
[<img align="right" title="Share via Email" src="https://docs.cloudposse.com/images/ionicons/ios-email-outline-2.0.1-16x16-999999.svg"/>][share_email]
7050
[<img align="right" title="Share on Google+" src="https://docs.cloudposse.com/images/ionicons/social-googleplus-outline-2.0.1-16x16-999999.svg" />][share_googleplus]
7151
[<img align="right" title="Share on Facebook" src="https://docs.cloudposse.com/images/ionicons/social-facebook-outline-2.0.1-16x16-999999.svg" />][share_facebook]
@@ -86,7 +66,7 @@ It's 100% Open Source and licensed under the [APACHE2](LICENSE).
8666

8767

8868

89-
We literally have [*hundreds of terraform modules*][terraform_modules] that are Open Source and well-maintained. Check them out!
69+
We literally have [*hundreds of terraform modules*][terraform_modules] that are Open Source and well-maintained. Check them out!
9070

9171

9272

@@ -199,8 +179,9 @@ Follow this procedure to delete your deployment.
199179
200180
201181
182+
<!-- markdownlint-disable -->
202183
## Makefile Targets
203-
```
184+
```text
204185
Available targets:
205186
206187
help Help screen
@@ -209,6 +190,7 @@ Available targets:
209190
lint Lint terraform code
210191
211192
```
193+
<!-- markdownlint-restore -->
212194
## Requirements
213195

214196
| Name | Version |
@@ -235,6 +217,7 @@ Available targets:
235217
| additional\_tag\_map | Additional tags for appending to each tag map | `map(string)` | `{}` | no |
236218
| arn\_format | ARN format to be used. May be changed to support deployment in GovCloud/China regions. | `string` | `"arn:aws"` | no |
237219
| attributes | Additional attributes (e.g. `state`) | `list(string)` | <pre>[<br> "state"<br>]</pre> | no |
220+
| backend\_config\_file\_overwrite\_enabled | If false, the backend config file will not be overwritten if it exists. | `bool` | `true` | no |
238221
| billing\_mode | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
239222
| block\_public\_acls | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
240223
| block\_public\_policy | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |
@@ -282,9 +265,9 @@ Available targets:
282265

283266

284267

285-
## Share the Love
268+
## Share the Love
286269

287-
Like this project? Please give it a ★ on [our GitHub](https://github.com/cloudposse/terraform-aws-tfstate-backend)! (it helps us **a lot**)
270+
Like this project? Please give it a ★ on [our GitHub](https://github.com/cloudposse/terraform-aws-tfstate-backend)! (it helps us **a lot**)
288271

289272
Are you using this project or any of our other projects? Consider [leaving a testimonial][testimonial]. =)
290273

@@ -300,7 +283,7 @@ Check out these related projects.
300283

301284
## Help
302285

303-
**Got a question?** We got answers.
286+
**Got a question?** We got answers.
304287

305288
File a GitHub [issue](https://github.com/cloudposse/terraform-aws-tfstate-backend/issues), send us an [email][email] or join our [Slack Community][slack].
306289

@@ -309,7 +292,7 @@ File a GitHub [issue](https://github.com/cloudposse/terraform-aws-tfstate-backen
309292
## DevOps Accelerator for Startups
310293

311294

312-
We are a [**DevOps Accelerator**][commercial_support]. We'll help you build your cloud infrastructure from the ground up so you can own it. Then we'll show you how to operate it and stick around for as long as you need us.
295+
We are a [**DevOps Accelerator**][commercial_support]. We'll help you build your cloud infrastructure from the ground up so you can own it. Then we'll show you how to operate it and stick around for as long as you need us.
313296

314297
[![Learn More](https://img.shields.io/badge/learn%20more-success.svg?style=for-the-badge)][commercial_support]
315298

@@ -338,11 +321,11 @@ Participate in our [Discourse Forums][discourse]. Here you'll find answers to co
338321

339322
## Newsletter
340323

341-
Sign up for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover.
324+
Sign up for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover.
342325

343326
## Office Hours
344327

345-
[Join us every Wednesday via Zoom][office_hours] for our weekly "Lunch & Learn" sessions. It's **FREE** for everyone!
328+
[Join us every Wednesday via Zoom][office_hours] for our weekly "Lunch & Learn" sessions. It's **FREE** for everyone!
346329

347330
[![zoom](https://img.cloudposse.com/fit-in/200x200/https://cloudposse.com/wp-content/uploads/2019/08/Powered-by-Zoom.png")][office_hours]
348331

@@ -373,28 +356,30 @@ Copyright © 2017-2020 [Cloud Posse, LLC](https://cpco.io/copyright)
373356

374357

375358

376-
## License
359+
## License
377360

378-
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
361+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
379362

380363
See [LICENSE](LICENSE) for full details.
381364

382-
Licensed to the Apache Software Foundation (ASF) under one
383-
or more contributor license agreements. See the NOTICE file
384-
distributed with this work for additional information
385-
regarding copyright ownership. The ASF licenses this file
386-
to you under the Apache License, Version 2.0 (the
387-
"License"); you may not use this file except in compliance
388-
with the License. You may obtain a copy of the License at
389-
390-
https://www.apache.org/licenses/LICENSE-2.0
391-
392-
Unless required by applicable law or agreed to in writing,
393-
software distributed under the License is distributed on an
394-
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
395-
KIND, either express or implied. See the License for the
396-
specific language governing permissions and limitations
397-
under the License.
365+
```text
366+
Licensed to the Apache Software Foundation (ASF) under one
367+
or more contributor license agreements. See the NOTICE file
368+
distributed with this work for additional information
369+
regarding copyright ownership. The ASF licenses this file
370+
to you under the Apache License, Version 2.0 (the
371+
"License"); you may not use this file except in compliance
372+
with the License. You may obtain a copy of the License at
373+
374+
https://www.apache.org/licenses/LICENSE-2.0
375+
376+
Unless required by applicable law or agreed to in writing,
377+
software distributed under the License is distributed on an
378+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
379+
KIND, either express or implied. See the License for the
380+
specific language governing permissions and limitations
381+
under the License.
382+
```
398383

399384

400385

@@ -416,7 +401,7 @@ This project is maintained and funded by [Cloud Posse, LLC][website]. Like it? P
416401

417402
We're a [DevOps Professional Services][hire] company based in Los Angeles, CA. We ❤️ [Open Source Software][we_love_open_source].
418403

419-
We offer [paid support][commercial_support] on all of our projects.
404+
We offer [paid support][commercial_support] on all of our projects.
420405

421406
Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation.
422407

docs/targets.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
<!-- markdownlint-disable -->
12
## Makefile Targets
2-
```
3+
```text
34
Available targets:
45
56
help Help screen
@@ -8,3 +9,4 @@ Available targets:
89
lint Lint terraform code
910
1011
```
12+
<!-- markdownlint-restore -->

docs/terraform.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| additional\_tag\_map | Additional tags for appending to each tag map | `map(string)` | `{}` | no |
2525
| arn\_format | ARN format to be used. May be changed to support deployment in GovCloud/China regions. | `string` | `"arn:aws"` | no |
2626
| attributes | Additional attributes (e.g. `state`) | `list(string)` | <pre>[<br> "state"<br>]</pre> | no |
27+
| backend\_config\_file\_overwrite\_enabled | If false, the backend config file will not be overwritten if it exists. | `bool` | `true` | no |
2728
| billing\_mode | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
2829
| block\_public\_acls | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
2930
| block\_public\_policy | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |

main.tf

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ locals {
1414

1515
terraform_backend_config_template_file = var.terraform_backend_config_template_file != "" ? var.terraform_backend_config_template_file : "${path.module}/templates/terraform.tf.tpl"
1616

17+
terraform_backend_config_file_exists = fileexists(local.terraform_backend_config_file)
18+
19+
overwrite_backend_config_file = (! local.terraform_backend_config_file_exists || var.backend_config_file_overwrite_enabled) && var.terraform_backend_config_file_path != ""
20+
1721
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.s3_bucket_label.id
1822
}
1923

@@ -246,8 +250,11 @@ data "template_file" "terraform_backend_config" {
246250
}
247251
}
248252

253+
# We only write the backend config file if:
254+
# 1. The file doesn't exist and the module was given `terraform_backend_config_file_path`
255+
# 2. The file does exist, `backend_config_file_overwrite_enabled` is `true`, and the module was given `terraform_backend_config_file_path`
249256
resource "local_file" "terraform_backend_config" {
250-
count = var.terraform_backend_config_file_path != "" ? 1 : 0
257+
count = local.overwrite_backend_config_file ? 1 : 0
251258
content = data.template_file.terraform_backend_config.rendered
252259
filename = local.terraform_backend_config_file
253260
}

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ variable "terraform_state_file" {
221221
description = "The path to the state file inside the bucket"
222222
}
223223

224+
variable "backend_config_file_overwrite_enabled" {
225+
type = bool
226+
default = true
227+
description = "If false, the backend config file will not be overwritten if it exists."
228+
}
229+
224230
variable "s3_bucket_name" {
225231
type = string
226232
default = ""

0 commit comments

Comments
 (0)