Multi-Region KMS Keys and its use cases
Tags: aws, security
Published: 2021-06-17 07:41:35

Hi Folks, AWS announced that a new capability in AWS Key Management Service (AWS KMS) called multi-Region keys, where it lets you replicate keys from one Amazon Web Services (AWS) Region into another. 

So today I am going to show how multi-Region keys can be used and its usefulness with few examples.

###What is special about Multi-Region keys?
>Multi-Region keys are a set of interoperable KMS keys that have the same key ID and key material, and that you can replicate to different Regions within the same partition. With symmetric multi-Region keys, you can encrypt data in one Region and decrypt it in a different Region.

AWS services also let you configure multi-Region keys for server-side encryption in case you want the same key to protect data in multiple regions. Also multi-region key will be useful for Client-side encryption where it reduces the latency and extra burden when your application is deployed in multiple regions.

### Create a multi-region KMS key
First create the multi region KMS key in a desired region. I choose us-east-1(N.Virginia) region.

Go to KMS service, click create key and choose the option multi-region key.(I will create a Symmetric Key in this tutorial but you can create asymmetric key as well)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8r913zdnxupy0alc4vq.png) 

Provide suitable alias name.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utf1ookq7lymik4c67pe.png)

Choose Key Administrator users or roles.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zaheztfgtmfmmx2rv1ok.png)

Once you followed the steps shown as below, you will able to create a Multi-region KMS key.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j140tygbesyhrzrvyipf.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fxpmmanx4ojiatxwedr.png)


This can be used in one region only until you create replicas.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pi5msn9ath037sgg7y86.png)

*Note: Multi-Region Key is consist of two types of keys.*

* Primary Key - This is the place where key is created originated initially. In our case we created the multi-region KMS key in N.Virginia(us-east-1). So primary key will be placed here.

* Replica Keys - You can create replica keys of primary key in each region. 

It has different ARNs. But all of these keys have Key ID and share same key materials.

```
Primary Key --> arn:aws:kms:us-east-1:528196604817:key/mrk-095774ce295d40a8a249e0257825598c
Replica Key -->arn:aws:kms:us-west-2:528196604817:key/mrk-095774ce295d40a8a249e0257825598c
```

Create new replica keys under regionality configuration section. For this tutorial, I will create a replica key in Oregon region. This will be used later.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xox6d7elhvpjskcojx9z.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1wio0jpj4dn8y8afjkr.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzwoh2zdq7g65kcieobt.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n68ybthzyxhu93zx7sv8.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx6g6xrb17pzmh0w2qa8.png)

*Note: Key policy needs to be defined for each replica key as well.*

######Why?
>every multi-Region key, whether primary or replica, is a fully functional, independent KMS key resource with its own key policy, aliases, grants, key description, lifecycle, and other attributes

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a79qb7vvfao178r10kkh.png)

##Applications:
 
###1) S3 Cross-region Replication:

Previously when you want to enable S3 cross region replication between two regions with server side encryption using custom KMS keys, You need to create two KMS keys in each region but KMS key materials is different for each key. Therefore AWS has to 
transparently decrypt and re-encrypting data on your behalf using the KMS keys you designate in the destination Region.

But when you use multi-region key for both S3 buckets, then this burden is reduced as the key material is same. So it is recommended to use multi-region key unless there is any special compliancy requirement exists where keys materials cannot be replicate between regions.

Lets create source and destination buckets with server-side encryption(using previously created multi-region key)

#####Creation of source bucket
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftfb97gnvk5839i3phih.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4ouebmhxcc1ern5b3bj.png)
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8koirdp5903vkjz67puj.png)

#####Creation of destination bucket

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqzefq0usyvl71ga9q5j.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79348wlpwtie2qiflhhv.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt1m2m2yep1m9sofak5i.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es0qhyshblv6jkesi9v4.png)

#####Create replication rules in source bucket

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nx21fcg4oidsg6x91ge0.png)

choose the destination bucket in replication rule
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7clax7w0hj0ysk6ljc8s.png)

To replicate objects encrypted with CMKs stored in AWS KMS, you must explicitly enable the option. Use the same multi-region KMS key.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrsrn7ijqoszym764j8t.png)

Require a IAM role for replication. Since I didn't create role earlier, I will go for create new role option.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k66n0pbg4ftm3pnh2dhl.png)

Lets have a look at the policy of the IAM role.
```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListBucket",
                "s3:GetReplicationConfiguration",
                "s3:GetObjectVersionForReplication",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectVersionTagging",
                "s3:GetObjectRetention",
                "s3:GetObjectLegalHold"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::chathra-s3-cross-region-replication-source-bucket",
                "arn:aws:s3:::chathra-s3-cross-region-replication-source-bucket/*"
            ]
        },
        {
            "Action": [
                "s3:ReplicateObject",
                "s3:ReplicateDelete",
                "s3:ReplicateTags",
                "s3:GetObjectVersionTagging",
                "s3:ObjectOwnerOverrideToBucketOwner"
            ],
            "Effect": "Allow",
            "Condition": {
                "StringLikeIfExists": {
                    "s3:x-amz-server-side-encryption": [
                        "aws:kms",
                        "AES256"
                    ],
                    "s3:x-amz-server-side-encryption-aws-kms-key-id": [
                        "arn:aws:kms:us-west-2:528196604817:key/mrk-095774ce295d40a8a249e0257825598c"
                    ]
                }
            },
            "Resource": [
                "arn:aws:s3:::chathra-s3-cross-region-replication-dest-bucket/*"
            ]
        },
        {
            "Action": [
                "kms:Decrypt"
            ],
            "Effect": "Allow",
            "Condition": {
                "StringLike": {
                    "kms:ViaService": "s3.us-east-1.amazonaws.com",
                    "kms:EncryptionContext:aws:s3:arn": [
                        "arn:aws:s3:::chathra-s3-cross-region-replication-source-bucket/*"
                    ]
                }
            },
            "Resource": [
                "arn:aws:kms:us-east-1:528196604817:key/mrk-095774ce295d40a8a249e0257825598c"
            ]
        },
        {
            "Action": [
                "kms:Encrypt"
            ],
            "Effect": "Allow",
            "Condition": {
                "StringLike": {
                    "kms:ViaService": [
                        "s3.us-west-2.amazonaws.com"
                    ],
                    "kms:EncryptionContext:aws:s3:arn": [
                        "arn:aws:s3:::chathra-s3-cross-region-replication-dest-bucket/*"
                    ]
                }
            },
            "Resource": [
                "arn:aws:kms:us-west-2:528196604817:key/mrk-095774ce295d40a8a249e0257825598c"
            ]
        }
    ]
}
```
#####Test Cross region replication

Lets upload a file to source bucket.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9eruckrhyngfs8m3v2u.png)

Once uploaded check the replication status. If its successful, the status should be "COMPLETED". 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ccnhzf8tzpnmtp2arj8.png)
 
File is successfully copied to destination bucket and its encrypted using same multi-region KMS Key

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5fc1th9mfczs0iv1pv0.png)

###Clean Up

When you schedule to delete multi region KMS key, make sure to delete both primary and replica, If you try to delete it only from the primary region, your primary key will stuck in following state without deleting.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mg3hskq64e9j3bfi1do0.png)