Skip to content

Commit

Permalink
feat: Do not call data resources when create is false (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryantbiggs committed Nov 3, 2023
1 parent 21bd8b2 commit 4951c38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.77.1
rev: v1.83.5
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand All @@ -23,7 +23,7 @@ repos:
- '--args=--only=terraform_standard_module_structure'
- '--args=--only=terraform_workspace_remote'
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
24 changes: 17 additions & 7 deletions main.tf
@@ -1,5 +1,15 @@
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {
count = var.create ? 1 : 0
}
data "aws_caller_identity" "current" {
count = var.create ? 1 : 0
}

locals {
account_id = try(data.aws_caller_identity.current[0].account_id, "")
partition = try(data.aws_partition.current[0].partition, "")
dns_suffix = try(data.aws_partition.current[0].dns_suffix, "")
}

################################################################################
# Key
Expand Down Expand Up @@ -98,7 +108,7 @@ data "aws_iam_policy_document" "this" {

principals {
type = "AWS"
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
identifiers = ["arn:${local.partition}:iam::${local.account_id}:root"]
}
}
}
Expand Down Expand Up @@ -342,7 +352,7 @@ data "aws_iam_policy_document" "this" {

principals {
type = "Service"
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
identifiers = ["dnssec-route53.${local.dns_suffix}"]
}
}
}
Expand All @@ -358,7 +368,7 @@ data "aws_iam_policy_document" "this" {

principals {
type = "Service"
identifiers = ["dnssec-route53.${data.aws_partition.current.dns_suffix}"]
identifiers = ["dnssec-route53.${local.dns_suffix}"]
}

condition {
Expand All @@ -373,7 +383,7 @@ data "aws_iam_policy_document" "this" {
content {
test = "StringEquals"
variable = "aws:SourceAccount"
values = try(condition.value.account_ids, [data.aws_caller_identity.current.account_id])
values = try(condition.value.account_ids, [local.account_id])
}
}

Expand All @@ -383,7 +393,7 @@ data "aws_iam_policy_document" "this" {
content {
test = "ArnLike"
variable = "aws:SourceArn"
values = [try(condition.value.hosted_zone_arn, "arn:${data.aws_partition.current.partition}:route53:::hostedzone/*")]
values = [try(condition.value.hosted_zone_arn, "arn:${local.partition}:route53:::hostedzone/*")]
}
}
}
Expand Down

0 comments on commit 4951c38

Please sign in to comment.