Như mình cũng đã hướng dẫn các bạn cách setup để access vào KMS của AWS account khác.
https://nimtechnology.com/2022/09/04/aws-encrypting-your-data-easily-via-kms-on-aws/#45_KMS_Other_account
Nhưng nếu sếp bạn muốn AWS Account A access tạo bộ KMS của AWS Account B
thì chúng ta phải sử dụng Assume Role.
1) Create Assume Role on Destination Account.
1.1) Create Policy
Đầu tiên cần tạo 1 Policy




sau đó bạn click ADD







1.2) Create Role and attach the previous Policy
Giờ create 1 role




Vì chúng ta đã có khai báo nó ở các bước trên.



2) Setup at Source Account
Mình sẽ đứng ở account này access vào KMS của account trước đó
Minh gọi đây là source account.
2.2) Create Policy




Bước tiếp theo là bạn tìm đến User và attach policy trên vào user đó.
3) Switch Role to Testing.
Vì chúng ta sử dụng kĩ thuật Assume Role nền đứng ở User nguồn chúng ta cần đứng ở Source Account và Switch Role sang Destination Account.
https://serverfault.com/questions/933078/how-to-switch-role-in-aws-cli
Bạn sẽ chỉnh file credential của AWS.
[k8s-mdaas-staging] #look at(1) aws_access_key_id = AKIAUR5PLRU2AXXXXXX aws_secret_access_key = bkRcgx0+QB4AJpzE20GV5LVS5AtzXXXXXXXXXX [k8s-mdaas-staging-sts] role_arn = arn:aws:iam::250887682577:role/KMSDemoAssumeRole ##Need you change this line source_profile = k8s-mdaas-staging #follow look at (1) root@LP11-D7891:~/demo-kms# aws sts get-caller-identity --profile k8s-mdaas-staging { "UserId": "AIDAUR5PLRU2IBJXXXXX", "Account": "3133635XXXXXX", "Arn": "arn:aws:iam::313363XXXXXX:user/CloudOpsAutomation" } root@LP11-D7891:~/demo-kms# aws sts get-caller-identity --profile k8s-mdaas-staging-sts { "UserId": "AROATU2QSHIIY3IOLCPIM:botocore-session-1664036610", "Account": "2508876XXXXXX", "Arn": "arn:aws:sts::25088768XXXXXX:assumed-role/KMSDemoAssumeRole/botocore-session-1664036610" }
Giờ bạn có thể Run encrypt
aws kms \ --region us-east-1 \ --profile k8s-mdaas-staging-sts \ --key-id arn:aws:kms:us-east-1:250887682577:key/e2a615c5-3689-420d-9445-ffd5feb78adb \ --plaintext fileb://data.txt \ --output text \ --query CiphertextBlob | base64 \ --decode > dataEncryptedFile