Terraform-provider-aws: RDS resource not resolving KMS Alias on subsequent plans

Created on 23 Aug 2017  路  12Comments  路  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @wv-cfromm as hashicorp/terraform#15883. It was migrated here as a result of the provider split. The original body of the issue is below._


If I create an RDS instance and reference a KMS key via Alias ARN, the instance is created and properly refers to the Alias' target. However, on subsequent plans, this forces perpetual recreation, as the ARN in state (the alias arn) does not match the ARN on the instance (the key arn).

Refreshing state should resolve the Alias before comparing to existing instance. This would allow Aliases to be usable on RDS resources.

question servicrds

Most helpful comment

I just lost an entire day to this. Very frustrating!

It essentially means we can't use pre-existing keys (referenced via data "aws_kms_alias" ...) with rds resources.

All 12 comments

I hit the same issue

data "aws_kms_alias" "kms_key" {
  name = "alias/${var.kms_key_alias}"
}
resource "aws_db_instance" "db" {
...
  kms_key_id                 = "${data.aws_kms_alias.kms_key.arn}"
...
}

After tf apply successfully, do tf plan and it results:

-/+ aws_db_instance.db (new resource required)
...
      kms_key_id:                        "arn:aws:kms:xxx:yyy:key/50534cd0-92f0-46c3-bb1b-f1c055a80813" => "arn:aws:kms:xxx:yyy:alias/foo" (forces new resource)       
...

Referencing the provider-split question of providing code, I believe trung's example is sufficient. Its pretty cut/dry. Referencing an Alias creates an instance that is properly tied to Key, but the Alias is seen as a difference by subsequent plans.

I just lost an entire day to this. Very frustrating!

It essentially means we can't use pre-existing keys (referenced via data "aws_kms_alias" ...) with rds resources.

You can use the aws_kms_alias data source, but you have to assemble its key ARN manually as a workaround. arn:aws:kms:REGION:ACCOUNT:key/${data.aws_kms_alias.NAME.target_key_id}

ListAliases API used by aws_kms_alias returns ARN of key-alias format (e.g.: arn:aws:kms:xxx:yyy:alias/foo) and not key-identifier format (e.g.: arn:aws:kms:xxx:yyy:key/50534cd0-92f0-46c3-bb1b-f1c055a80813). See ARN Syntax Format for KMS

CreateDBInstance API used by aws_db_instance resource accepts kms_key_id which can be key-alias or key-identifier ARN. However, DescribeDBInstances API used by aws_db_instance resource when reading back only returns kms_key_id of key-identifier ARN. Therefore causing the diff.

I think the fix could be to expose key-identifier ARN in aws_kms_alias data source. In the mean time, a sensible workaround would be to use replace() interpolation function to translate the ARN in key-alias to key-identifier format.

E.g:

resource "aws_db_instance" "db" {
...
  kms_key_id                 = "${replace(data.aws_kms_alias.kms_key.arn, data.aws_kms_alias.name, format("key/%s", data.aws_kms_alias.target_key_id))}"
...
}

Note: I haven't tried it myself.

Another potential issue with aws_kms_alias is that the API only pulls max of 100 aliases so there's a chance that an alias would not be found.

Updated: fixed the replacement string

Added @tombuildsstuff to review the issue

+1 facing the same issue.

+1 kms_key_id is a replacement call to RDS. Please include support

nightmare...lost a day on this issue

@bmacauley-reward heh, fancy seeing you here :)

hit this too, major pain.

Hi everyone 馃憢 Since this issue was raised awhile ago, both the aws_kms_alias resource and data source now support a target_key_arn attribute, which is compatible with all kms_key_id arguments across all Terraform AWS Provider resources. This is the recommended path for all configurations experiencing this issue.

For some additional context, the maintainers do not intend to directly support KMS Alias ARNs due to how the AWS APIs always return the KMS Key ID. Since Terraform performs drift detection with configured values against the value stored in the Terraform state when refreshed by the resource, this difference would always show unless we introduced additional cross-service API calls to every resource that supports this parameter. This API call would occur every Terraform plan for every resource and could dramatically affect account-level KMS rate limiting along with permissions issues for restrictive environments. Given these operational concerns, we prefer to recommend the target_key_arn attribute.

If the documentation around this can be improved, please reach out.

Was this page helpful?
0 / 5 - 0 ratings