Store and Retrieve Secrets for Cross Account Services Using KMS Permission Policy and IAM Roles
Tags: awssecretsmanager, awskms, awsrds, awsiamrole
Published: 2022-08-28 08:37:41

“ I have checked the documents of AWS to resolve the issue of retrieving secrets for cross account services. So I checked for various solutions and found out that it can be possible using IAM roles and policies. Also encrypting the secret using kms custom key. Pricing of KMS custom keys costs $1 per key.”

AWS Identity and Access Management (IAM) provides fine-grained access control across all of AWS. With IAM, you can specify who can access which services and resources, and under which conditions. With IAM policies, you manage permissions to your workforce and systems to ensure least-privilege permissions. IAM is an AWS service that is offered at no additional charge. To learn more, read the [IAM](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html).

The KMS keys that you create are customer managed keys. Customer managed keys are KMS keys in your AWS account that you create, own, and manage. You have full control over these KMS keys, including establishing and maintaining their [key policies](https://docs.aws.amazon.com/kms/latest/developerguide/control-access.html), [IAM policies](https://docs.aws.amazon.com/kms/latest/developerguide/control-access.html), [and grants](https://docs.aws.amazon.com/kms/latest/developerguide/control-access.html), [enabling and disabling](https://docs.aws.amazon.com/kms/latest/developerguide/enabling-keys.html) them, [rotating their cryptographic material](https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html), adding tags, creating aliases that refer to the KMS keys, and scheduling the KMS keys for deletion.

In this post, you will get to know how to store and retrieve secrets for cross account services using kms permission policy and IAM roles. Here I have used a secret manager to store the secret credentials, a custom managed key to encrypt the secret, a rds database which the secret has been stored, and an IAM role which retrieves the secret.

#Prerequisites
You’ll need an Amazon RDS Database for this post. [Getting started with RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.html) provides instructions on how to create a RDS. For this blog, I assume that I have a rds database.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4536aa8hllo852nv3da.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon RDS, aws secrets manager, aws kms, IAM role and cloudshell.

#Solution overview
The blog post consists of the following phases:
1. Store a Secret in Secrets Manager of Other Account RDS Database and Also Encrypt it Using KMS Custom Key
2. Create a Role in Another Account for EC2 to Retrieve the Database Secret Stored in Main Account

I have a rds database as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gel8kejv5vlhvvluaids.png)
##Phase 1: Store a Secret in Secrets Manager of Other Account RDS Database and Also Encrypt it Using KMS Custom Key
1. Open the secrets manager, store a secret with key value pair details. Give the secret name and store a secret. Create a custom key with symmetric key spec and other default options. Set key policy to allow other accounts access to the secret and retrieve it. Also set the resource permissions policy of secret for other accounts.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cirrrwa1sccregopduvv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbz8ll28i89nhvqtd29r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yfca8457hkacmyq2lzg0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpsy2d13bgdbm2lr25av.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcqlbahdgniq2fz7c9o4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjh3nn9brfklms65u7rk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5dxg9a9wazpstbhlmx7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yohn55sm16cqce2cu12o.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4yq7lu5hh8vh7yk9aiz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqsu60f1l3w0b46xtd2w.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/li9oyn90a3p3seim6cst.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rct33xed5co51ukdecx4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4aht7p3yl9r841bnsgb.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it8v2bpdct1j7rj7xeel.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft5v6vrlql9e9h4cglg6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg6mj2jpni5ljkd4r2tg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onp1a372rz26tbf0qml3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4rb5yeclw0ouxu6cp21.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khv54li0zttmiunvgr3i.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srurpcqebb4xm2peilig.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja3tvh8grytqantsvdv3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrkfwjco2izwf2fl63tk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isxnr2vnc3icitio3hms.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6dnounemj9jc6rqfxl3.png)

##Phase 2: Create a Role in Another Account for EC2 to Retrieve the Database Secret Stored in Main Account 
1. Open the IAM console and create an IAM role for ec2 service. Add a custom inline policy to allow kms and secret manager access to the main account.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbm6mi38etlu75zvnfwa.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnoq7yfi4xcugtl1gnu9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o42teur97sklwd4gyl1k.png)

#Clean-up
Delete Secret Manager, KMS Custom Key, IAM Role, RDS Database.

#Pricing
I review the pricing and estimated cost of this example. 
Cost of RDS = $0.04
Cost of Key Management Service = $1.0
Cost of Secret Manager = 2 hours x ($0.40 per secret per month / 30 days / 24 hours + $0.05 per 10,000 API calls)

#Summary
In this post, I showed “how to store and retrieve secrets for cross account services using kms permission policy and IAM roles ”.

For more details on IAM, Checkout Get started IAM, open the [IAM console](https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1#/home). To learn more, read the [IAM documentation](https://docs.aws.amazon.com/iam/).

For more details on Key Management Service, Checkout Get started Key Management Service, open the [Key Management Service Console](https://ap-south-1.console.aws.amazon.com/kms/home?region=ap-south-1#/kms/keys). To learn more, read the [Key Management Service Documentation](https://docs.aws.amazon.com/kms/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)