Reputation: 134
I am having two different sets of accounts, where kms key is created. kms key creation is handled via ci pipeline , and there are different pipelines available for both sets of accounts, now wanted to merge these two pipelines into one, so all resource creation would be from single terraform pipeline.
one set of accounts are using name
and others using name_prefix
while creating kms keys.
resource "aws_kms_alias" "backup_alias" {
count = var.create == true ? 1 : 0
name = "alias/backup-key"
name_prefix = "alias/${var.target_infra}-backup-key-"
target_key_id = aws_kms_key.backup.0.key_id
}
resource "aws_kms_alias" "backup_alias" {
count = var.create == true ? 1 : 0
name = "alias/backup-key-"
target_key_id = aws_kms_key.backup.0.key_id
}
as would like to keep the same code for both the accounts, hence thinking of using dynamic block, but it is not helping as it is block level, not variable level.
trying something like below:
resource "aws_kms_alias" "backup_alias" {
count = var.create == true ? 1 : 0
#name = var.is_lz == "enabled" ? "alias/${var.target_infra}-backup-key-" : "alias/backup-key"
dynamic "name" {
for_each = var.is_lz == "enabled" ? 0 : 1
content {
name = "alias/backup-key"
}
}
dynamic "name_prefix" {
for_each = var.is_lz == "enabled" ? 1 : 0
content {
name_prefix = "alias/${var.target_infra}-backup-key-"
}
}
target_key_id = aws_kms_key.backup.0.key_id
}
but it's not working. is there any way to have both in same resource?
Upvotes: 0
Views: 761
Reputation: 200446
You're using 0
and 1
like you would do with count
. When you use for_each
you should use a list with 0 or 1 elements.
resource "aws_kms_alias" "backup_alias" {
count = var.create == true ? ["1"] : []
#name = var.is_lz == "enabled" ? "alias/${var.target_infra}-backup-key-" : "alias/backup-key"
dynamic "name" {
for_each = var.is_lz == "enabled" ? [] : ["1"]
content {
name = "alias/backup-key"
}
}
dynamic "name_prefix" {
for_each = var.is_lz == "enabled" ? 1 : 0
content {
name_prefix = "alias/${var.target_infra}-backup-key-"
}
}
target_key_id = aws_kms_key.backup.0.key_id
}
Upvotes: 2