terraform-aws-rds-cluster icon indicating copy to clipboard operation
terraform-aws-rds-cluster copied to clipboard

Use aws/rds as default KMS key

Open alexjurkiewicz opened this issue 4 years ago • 20 comments

If the user enables storage or performance insights encryption without specifying a KMS key, use the AWS-managed aws/rds one.

There's one thing I'm checking. Does the aws/rds key always exist in AWS accounts, or is it only created when needed? If the latter, loading it as a data source could fail in new accounts, leading to a chicken and egg problem with this module attempting to deploy a cluster in a new account. I've asked AWS support for clarification.

alexjurkiewicz avatar Feb 17 '21 04:02 alexjurkiewicz

/test all

Gowiem avatar Feb 17 '21 04:02 Gowiem

Unfortunately, AWS Support confirmed the key is created on-demand: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.Encryption.html#Overview.Encryption.Enabling

When you create an encrypted DB instance, you can choose a customer managed CMK or the AWS managed CMK for Amazon RDS to encrypt your DB instance. If you don't specify the key identifier for a customer managed CMK, Amazon RDS uses the AWS managed CMK for your new DB instance. Amazon RDS creates an AWS managed CMK for Amazon RDS for your AWS account. Your AWS account has a different AWS managed CMK for Amazon RDS for each AWS Region.

So, it's probably not OK for this module to require the aws/rds key. Instead, I will change the logic so this key is only looked up if the user enables encryption without specifying keys (a good optimisation anyway), and document the restriction. I'll also investigate if there's a simpler way to create this key than "create an instance with encryption enabled but no key specified".

It might also be possible to take advantage of this automatic creation. Terraform can specify storage_encrypted = true and no KMS key, and the AWS API will set the KMS key to aws/rds. But will that break on updates... hmm 🤔

alexjurkiewicz avatar Feb 18 '21 01:02 alexjurkiewicz

I've updated this PR so that the aws/rds key is only loaded with a data source if required, and mentioned the "must already exist" restriction in the docs.

Unfortunately, I don't think it's possible to have this module create the key for you. You can create a db instance with storage_enabled = true and kms_key_arn = null, but Terraform will detect kms_key_arn as changed on subsequent runs.

alexjurkiewicz avatar Feb 18 '21 10:02 alexjurkiewicz

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Feb 18 '21 19:02 mergify[bot]

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Feb 21 '21 02:02 mergify[bot]

ping 🙏

alexjurkiewicz avatar Feb 23 '21 06:02 alexjurkiewicz

@alexjurkiewicz discussing this one internally with the team. Now that we know those AWS KMS Keys are not around by default in new accounts... I'm wondering if we should just use cloudposse/kms-key/aws in the cases where no key is passed but storage_encrypted is true. Let me know your thoughts and I'll also discuss with the team.

Thanks!

Gowiem avatar Feb 23 '21 19:02 Gowiem

That makes sense. A KMS key (without rotation) is $1/mo, which is nothing compared to the cost of an RDS cluster.

alexjurkiewicz avatar Feb 23 '21 21:02 alexjurkiewicz

d1a677f:

Create KMS keys if needed

Rather than rely on aws/rds, which isn't guaranteed to exist (it's created in each account/region when you create an RDS resource with encryption and don't specify an explicit KMS key).

Create separate keys for storage and performance insights, adding a hardcoded policy to both (copied from aws/rds). Output the key ARNs as well.

Finally, split KMS logic out to a separate file, because the policy is so large it makes main.tf hard to read otherwise.

alexjurkiewicz avatar Feb 23 '21 22:02 alexjurkiewicz

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Apr 22 '21 16:04 mergify[bot]

/test all

nitrocode avatar Aug 16 '21 19:08 nitrocode

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Aug 21 '21 03:08 mergify[bot]

/test all

nitrocode avatar Oct 18 '21 19:10 nitrocode

/test all

nitrocode avatar Oct 18 '21 19:10 nitrocode

/test test/terratest

nitrocode avatar Oct 18 '21 19:10 nitrocode

/test test/readme

nitrocode avatar Oct 18 '21 19:10 nitrocode

@alexjurkiewicz

TestExamplesComplete 2021-10-18T19:34:21Z command.go:121:   on ../../kms.tf line 90, in module "performance_insights_kms_key":
TestExamplesComplete 2021-10-18T19:34:21Z command.go:121:   90:   for_each            = local.storage_create_kms_key ? [1] : []

nitrocode avatar Oct 18 '21 19:10 nitrocode

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Oct 18 '21 19:10 mergify[bot]

This pull request is now in conflict. Could you fix it @alexjurkiewicz? 🙏

mergify[bot] avatar Nov 19 '21 07:11 mergify[bot]

@alexjurkiewicz friendly ping on this one -- Still want to move this forward or should we close it?

Gowiem avatar Jul 23 '22 14:07 Gowiem

closing

hans-d avatar Mar 02 '24 00:03 hans-d

closing

hans-d avatar Mar 02 '24 00:03 hans-d