Skip to content

Commit 4951c38

Browse files
authored
feat: Do not call data resources when create is false (#25)
1 parent 21bd8b2 commit 4951c38

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.1
3+
rev: v1.83.5
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate
@@ -23,7 +23,7 @@ repos:
2323
- '--args=--only=terraform_standard_module_structure'
2424
- '--args=--only=terraform_workspace_remote'
2525
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v4.4.0
26+
rev: v4.5.0
2727
hooks:
2828
- id: check-merge-conflict
2929
- id: end-of-file-fixer

main.tf

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
data "aws_partition" "current" {}
2-
data "aws_caller_identity" "current" {}
1+
data "aws_partition" "current" {
2+
count = var.create ? 1 : 0
3+
}
4+
data "aws_caller_identity" "current" {
5+
count = var.create ? 1 : 0
6+
}
7+
8+
locals {
9+
account_id = try(data.aws_caller_identity.current[0].account_id, "")
10+
partition = try(data.aws_partition.current[0].partition, "")
11+
dns_suffix = try(data.aws_partition.current[0].dns_suffix, "")
12+
}
313

414
################################################################################
515
# Key
@@ -98,7 +108,7 @@ data "aws_iam_policy_document" "this" {
98108

99109
principals {
100110
type = "AWS"
101-
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
111+
identifiers = ["arn:${local.partition}:iam::${local.account_id}:root"]
102112
}
103113
}
104114
}
@@ -342,7 +352,7 @@ data "aws_iam_policy_document" "this" {
342352

343353
principals {
344354
type = "Service"
345-
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
355+
identifiers = ["dnssec-route53.${local.dns_suffix}"]
346356
}
347357
}
348358
}
@@ -358,7 +368,7 @@ data "aws_iam_policy_document" "this" {
358368

359369
principals {
360370
type = "Service"
361-
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
371+
identifiers = ["dnssec-route53.${local.dns_suffix}"]
362372
}
363373

364374
condition {
@@ -373,7 +383,7 @@ data "aws_iam_policy_document" "this" {
373383
content {
374384
test = "StringEquals"
375385
variable = "aws:SourceAccount"
376-
values = try(condition.value.account_ids, [data.aws_caller_identity.current.account_id])
386+
values = try(condition.value.account_ids, [local.account_id])
377387
}
378388
}
379389

@@ -383,7 +393,7 @@ data "aws_iam_policy_document" "this" {
383393
content {
384394
test = "ArnLike"
385395
variable = "aws:SourceArn"
386-
values = [try(condition.value.hosted_zone_arn, "arn:${data.aws_partition.current.partition}:route53:::hostedzone/*")]
396+
values = [try(condition.value.hosted_zone_arn, "arn:${local.partition}:route53:::hostedzone/*")]
387397
}
388398
}
389399
}

0 commit comments

Comments
 (0)