Skip to content

Commit db4c0fc

Browse files
Use templatefile (#94)
* Use templatefile * Auto Format * Update main.tf * Update main.tf * Update main.tf * Update outputs.tf * Auto Format Co-authored-by: cloudpossebot <11232728+cloudpossebot@users.noreply.github.com>
1 parent 3ffcb0f commit db4c0fc

File tree

4 files changed

+25
-36
lines changed

4 files changed

+25
-36
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ Available targets:
259259
|------|---------|
260260
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.0 |
261261
| <a name="provider_local"></a> [local](#provider\_local) | >= 1.3 |
262-
| <a name="provider_template"></a> [template](#provider\_template) | >= 2.0 |
263262

264263
## Modules
265264

@@ -284,7 +283,6 @@ Available targets:
284283
| [aws_iam_policy_document.replication](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
285284
| [aws_iam_policy_document.replication_sts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
286285
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
287-
| [template_file.terraform_backend_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
288286

289287
## Inputs
290288

docs/terraform.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
|------|---------|
1515
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.0 |
1616
| <a name="provider_local"></a> [local](#provider\_local) | >= 1.3 |
17-
| <a name="provider_template"></a> [template](#provider\_template) | >= 2.0 |
1817

1918
## Modules
2019

@@ -39,7 +38,6 @@
3938
| [aws_iam_policy_document.replication](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4039
| [aws_iam_policy_document.replication_sts](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4140
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
42-
| [template_file.terraform_backend_config](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
4341

4442
## Inputs
4543

main.tf

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ locals {
1919

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

22+
terraform_backend_config_content = templatefile(local.terraform_backend_config_template_file, {
23+
region = data.aws_region.current.name
24+
bucket = join("", aws_s3_bucket.default.*.id)
25+
26+
dynamodb_table = element(
27+
coalescelist(
28+
aws_dynamodb_table.with_server_side_encryption.*.name,
29+
aws_dynamodb_table.without_server_side_encryption.*.name
30+
),
31+
0
32+
)
33+
34+
encrypt = var.enable_server_side_encryption ? "true" : "false"
35+
role_arn = var.role_arn
36+
profile = var.profile
37+
terraform_version = var.terraform_version
38+
terraform_state_file = var.terraform_state_file
39+
namespace = var.namespace
40+
stage = var.stage
41+
environment = var.environment
42+
name = var.name
43+
})
44+
2245
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.this.id
2346
}
2447

@@ -242,39 +265,9 @@ resource "aws_dynamodb_table" "without_server_side_encryption" {
242265

243266
data "aws_region" "current" {}
244267

245-
data "template_file" "terraform_backend_config" {
246-
count = local.enabled ? 1 : 0
247-
248-
template = file(local.terraform_backend_config_template_file)
249-
250-
vars = {
251-
region = data.aws_region.current.name
252-
bucket = join("", aws_s3_bucket.default.*.id)
253-
254-
dynamodb_table = element(
255-
coalescelist(
256-
aws_dynamodb_table.with_server_side_encryption.*.name,
257-
aws_dynamodb_table.without_server_side_encryption.*.name,
258-
[""]
259-
),
260-
0
261-
)
262-
263-
encrypt = var.enable_server_side_encryption ? "true" : "false"
264-
role_arn = var.role_arn
265-
profile = var.profile
266-
terraform_version = var.terraform_version
267-
terraform_state_file = var.terraform_state_file
268-
namespace = var.namespace
269-
stage = var.stage
270-
environment = var.environment
271-
name = var.name
272-
}
273-
}
274-
275268
resource "local_file" "terraform_backend_config" {
276269
count = local.enabled && var.terraform_backend_config_file_path != "" ? 1 : 0
277-
content = join("", data.template_file.terraform_backend_config.*.rendered)
270+
content = local.terraform_backend_config_content
278271
filename = local.terraform_backend_config_file
279272
file_permission = "0644"
280273
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ output "dynamodb_table_arn" {
5050
}
5151

5252
output "terraform_backend_config" {
53-
value = join("", data.template_file.terraform_backend_config.*.rendered)
53+
value = local.enabled ? local.terraform_backend_config_content : ""
5454
description = "Rendered Terraform backend config file"
5555
}

0 commit comments

Comments
 (0)