Skip to content

Commit

Permalink
Replacing lookup function with try
Browse files Browse the repository at this point in the history
Replacing lookup function with try to fix issue when variable is empyt.
  • Loading branch information
brunodasilvalenga authored Nov 8, 2024
1 parent 10b5377 commit caa3673
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ resource "aws_backup_vault_lock_configuration" "backup_vault_lock" {
resource "aws_backup_plan" "backup_plan" {
count = var.enabled ? 1 : 0
name = var.name
# Rules
dynamic "rule" {
for_each = var.rules
content {
Expand All @@ -38,24 +37,24 @@ resource "aws_backup_plan" "backup_plan" {

# Lifecycle
dynamic "lifecycle" {
for_each = length(lookup(rule.value, "lifecycle", {})) == 0 ? [] : [lookup(rule.value, "lifecycle", {})]
for_each = try(rule.value.lifecycle != null ? [rule.value.lifecycle] : [], [])
content {
cold_storage_after = lookup(rule.value, "enable_continuous_backup", false) == true ? null : lookup(lifecycle.value, "cold_storage_after", 7)
cold_storage_after = try(rule.value.enable_continuous_backup, false) ? null : try(lifecycle.value.cold_storage_after, 7)
delete_after = try(lifecycle.value.delete_after, 35)
}
}

# Copy action
dynamic "copy_action" {
for_each = lookup(rule.value, "copy_actions", [])
for_each = try(rule.value.copy_actions, [])
content {
destination_vault_arn = aws_backup_vault.backup_vault.arn

# Copy Action Lifecycle
dynamic "lifecycle" {
for_each = length(lookup(copy_action.value, "lifecycle", {})) == 0 ? [] : [lookup(copy_action.value, "lifecycle", {})]
for_each = try(copy_action.value.lifecycle != null ? [copy_action.value.lifecycle] : [], [])
content {
cold_storage_after = lookup(rule.value, "enable_continuous_backup", false) == true ? null : lookup(lifecycle.value, "cold_storage_after", 7)
cold_storage_after = try(rule.value.enable_continuous_backup, false) ? null : try(lifecycle.value.cold_storage_after, 7)
delete_after = try(lifecycle.value.delete_after, 35)
}
}
Expand Down Expand Up @@ -86,10 +85,10 @@ resource "aws_backup_selection" "tag" {
# AWS Backup selection - resources arn
resource "aws_backup_selection" "resources" {
count = var.enabled ? length(var.selection_resources) > 0 && var.account_type == local.account_type.workload ? length(var.selection_resources) : 0 : 0
name = replace("${element(split(":", var.selection_resources[count.index]), 2)}-${element(split(":", var.selection_resources[count.index]), length(split(":",var.selection_resources[count.index]))-1)}-${count.index}", "/", "-")
name = "${element(split(":", var.selection_resources[count.index]), 2)}-${element(split(":", var.selection_resources[count.index]), length(var.selection_resources[count.index]))}-${count.index}"
iam_role_arn = aws_iam_role.backup_role[0].arn
plan_id = aws_backup_plan.backup_plan[0].id
resources = [var.selection_resources[count.index]]
resources = var.selection_resources
}

# AWS Backup vault notification
Expand Down

0 comments on commit caa3673

Please sign in to comment.