|
| 1 | +<p align="center"> <img src="https://user-images.githubusercontent.com/50652676/62349836-882fef80-b51e-11e9-99e3-7b974309c7e3.png" width="100" height="100"></p> |
| 2 | + |
| 3 | + |
| 4 | +<h1 align="center"> |
| 5 | + Terraform AWS KMS |
| 6 | +</h1> |
| 7 | + |
| 8 | +<p align="center" style="font-size: 1.2rem;"> |
| 9 | + This terraform module creates a KMS Customer Master Key (CMK) and its alias. |
| 10 | + </p> |
| 11 | + |
| 12 | +<p align="center"> |
| 13 | + |
| 14 | +<a href="https://www.terraform.io"> |
| 15 | + <img src="https://img.shields.io/badge/Terraform-v0.13-green" alt="Terraform"> |
| 16 | +</a> |
| 17 | +<a href="LICENSE.md"> |
| 18 | + <img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="Licence"> |
| 19 | +</a> |
| 20 | + |
| 21 | + |
| 22 | +</p> |
| 23 | +<p align="center"> |
| 24 | + |
| 25 | +<a href='https://facebook.com/sharer/sharer.php?u=https://github.com/devops4me/terraform-aws-kms'> |
| 26 | + <img title="Share on Facebook" src="https://user-images.githubusercontent.com/50652676/62817743-4f64cb80-bb59-11e9-90c7-b057252ded50.png" /> |
| 27 | +</a> |
| 28 | +<a href='https://www.linkedin.com/shareArticle?mini=true&title=Terraform+AWS+KMS&url=https://github.com/devops4mecode/terraform-aws-kms'> |
| 29 | + <img title="Share on LinkedIn" src="https://user-images.githubusercontent.com/50652676/62817742-4e339e80-bb59-11e9-87b9-a1f68cae1049.png" /> |
| 30 | +</a> |
| 31 | +<a href='https://twitter.com/intent/tweet/?text=Terraform+AWS+KMS&url=https://github.com/devops4mecode/terraform-aws-kms'> |
| 32 | + <img title="Share on Twitter" src="https://user-images.githubusercontent.com/50652676/62817740-4c69db00-bb59-11e9-8a79-3580fbbf6d5c.png" /> |
| 33 | +</a> |
| 34 | + |
| 35 | +</p> |
| 36 | +<hr> |
| 37 | +## Prerequisites |
| 38 | + |
| 39 | +This module has a few dependencies: |
| 40 | + |
| 41 | +- [Terraform 0.13](https://learn.hashicorp.com/terraform/getting-started/install.html) |
| 42 | +- [Go](https://golang.org/doc/install) |
| 43 | +- [github.com/stretchr/testify/assert](https://github.com/stretchr/testify) |
| 44 | +- [github.com/gruntwork-io/terratest/modules/terraform](https://github.com/gruntwork-io/terratest) |
| 45 | + |
| 46 | +## Examples |
| 47 | + |
| 48 | + |
| 49 | +**IMPORTANT:** Since the `master` branch used in `source` varies based on new modifications, we suggest that you use the release versions [here](https://github.com/devops4mecode/terraform-aws-kms/releases). |
| 50 | + |
| 51 | + |
| 52 | +### Simple Example |
| 53 | +Here is an example of how you can use this module in your inventory structure: |
| 54 | +```hcl |
| 55 | + module "kms_key" { |
| 56 | + source = "devops4mecode/kms/aws" |
| 57 | + version = "0.13.0" |
| 58 | + name = "kms" |
| 59 | + application = "devops4me" |
| 60 | + environment = "test" |
| 61 | + label_order = ["environment", "application", "name"] |
| 62 | + enabled = true |
| 63 | + description = "KMS key for cloudtrail" |
| 64 | + deletion_window_in_days = 7 |
| 65 | + enable_key_rotation = true |
| 66 | + alias = "alias/cloudtrail" |
| 67 | + policy = data.aws_iam_policy_document.default.json |
| 68 | + } |
| 69 | +
|
| 70 | + data "aws_iam_policy_document" "default" { |
| 71 | + version = "2012-10-17" |
| 72 | + statement { |
| 73 | + sid = "Enable IAM User Permissions" |
| 74 | + effect = "Allow" |
| 75 | + principals { |
| 76 | + type = "AWS" |
| 77 | + identifiers = ["*"] |
| 78 | + } |
| 79 | + actions = ["kms:*"] |
| 80 | + resources = ["*"] |
| 81 | + } |
| 82 | + statement { |
| 83 | + sid = "Allow CloudTrail to encrypt logs" |
| 84 | + effect = "Allow" |
| 85 | + principals { |
| 86 | + type = "Service" |
| 87 | + identifiers = ["cloudtrail.amazonaws.com"] |
| 88 | + } |
| 89 | + actions = ["kms:GenerateDataKey*"] |
| 90 | + resources = ["*"] |
| 91 | + condition { |
| 92 | + test = "StringLike" |
| 93 | + variable = "kms:EncryptionContext:aws:cloudtrail:arn" |
| 94 | + values = ["arn:aws:cloudtrail:*:XXXXXXXXXXXX:trail/*"] |
| 95 | + } |
| 96 | + } |
| 97 | +
|
| 98 | + statement { |
| 99 | + sid = "Allow CloudTrail to describe key" |
| 100 | + effect = "Allow" |
| 101 | + principals { |
| 102 | + type = "Service" |
| 103 | + identifiers = ["cloudtrail.amazonaws.com"] |
| 104 | + } |
| 105 | + actions = ["kms:DescribeKey"] |
| 106 | + resources = ["*"] |
| 107 | + } |
| 108 | +
|
| 109 | + statement { |
| 110 | + sid = "Allow principals in the account to decrypt log files" |
| 111 | + effect = "Allow" |
| 112 | + principals { |
| 113 | + type = "AWS" |
| 114 | + identifiers = ["*"] |
| 115 | + } |
| 116 | + actions = [ |
| 117 | + "kms:Decrypt", |
| 118 | + "kms:ReEncryptFrom" |
| 119 | + ] |
| 120 | + resources = ["*"] |
| 121 | + condition { |
| 122 | + test = "StringEquals" |
| 123 | + variable = "kms:CallerAccount" |
| 124 | + values = [ |
| 125 | + "XXXXXXXXXXXX"] |
| 126 | + } |
| 127 | + condition { |
| 128 | + test = "StringLike" |
| 129 | + variable = "kms:EncryptionContext:aws:cloudtrail:arn" |
| 130 | + values = ["arn:aws:cloudtrail:*:XXXXXXXXXXXX:trail/*"] |
| 131 | + } |
| 132 | + } |
| 133 | +
|
| 134 | + statement { |
| 135 | + sid = "Allow alias creation during setup" |
| 136 | + effect = "Allow" |
| 137 | + principals { |
| 138 | + type = "AWS" |
| 139 | + identifiers = ["*"] |
| 140 | + } |
| 141 | + actions = ["kms:CreateAlias"] |
| 142 | + resources = ["*"] |
| 143 | + } |
| 144 | + } |
| 145 | +
|
| 146 | +``` |
| 147 | + |
| 148 | +## Inputs |
| 149 | + |
| 150 | +| Name | Description | Type | Default | Required | |
| 151 | +|------|-------------|------|---------|:--------:| |
| 152 | +| alias | The display name of the alias. The name must start with the word `alias` followed by a forward slash. | `string` | `""` | no | |
| 153 | +| application | Application (e.g. `do4m` or `devops4me`). | `string` | `""` | no | |
| 154 | +| attributes | Additional attributes (e.g. `1`). | `list(string)` | `[]` | no | |
| 155 | +| customer\_master\_key\_spec | Specifies whether the key contains a symmetric key or an asymmetric key pair and the encryption algorithms or signing algorithms that the key supports. Valid values: SYMMETRIC\_DEFAULT, RSA\_2048, RSA\_3072, RSA\_4096, ECC\_NIST\_P256, ECC\_NIST\_P384, ECC\_NIST\_P521, or ECC\_SECG\_P256K1. Defaults to SYMMETRIC\_DEFAULT. | `string` | `"SYMMETRIC_DEFAULT"` | no | |
| 156 | +| deletion\_window\_in\_days | Duration in days after which the key is deleted after destruction of the resource. | `number` | `10` | no | |
| 157 | +| description | The description of the key as viewed in AWS console. | `string` | `"Parameter Store KMS master key"` | no | |
| 158 | +| enable\_key\_rotation | Specifies whether key rotation is enabled. | `bool` | `true` | no | |
| 159 | +| enabled | Specifies whether the kms is enabled or disabled. | `bool` | `true` | no | |
| 160 | +| environment | Environment (e.g. `prod`, `dev`, `staging`). | `string` | `""` | no | |
| 161 | +| is\_enabled | Specifies whether the key is enabled. | `bool` | `true` | no | |
| 162 | +| key\_usage | Specifies the intended use of the key. Defaults to ENCRYPT\_DECRYPT, and only symmetric encryption and decryption are supported. | `string` | `"ENCRYPT_DECRYPT"` | no | |
| 163 | +| label\_order | label order, e.g. `name`,`application`. | `list` | `[]` | no | |
| 164 | +| managedby | ManagedBy, eg 'DevOps4Me' or 'NajibRadzuan'. | `string` | `"najibradzuan@devops4me.com"` | no | |
| 165 | +| name | Name (e.g. `app` or `cluster`). | `string` | `""` | no | |
| 166 | +| policy | A valid policy JSON document. For more information about building AWS IAM policy documents with Terraform. | `string` | `""` | no | |
| 167 | +| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`). | `map(string)` | `{}` | no | |
| 168 | + |
| 169 | +## Outputs |
| 170 | + |
| 171 | +| Name | Description | |
| 172 | +|------|-------------| |
| 173 | +| alias\_arn | Alias ARN. | |
| 174 | +| alias\_name | Alias name. | |
| 175 | +| key\_arn | Key ARN. | |
| 176 | +| key\_id | Key ID. | |
| 177 | +| tags | A mapping of tags to assign to the resource. | |
| 178 | + |
| 179 | +## Testing |
| 180 | +In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system. |
| 181 | + |
| 182 | +You need to run the following command in the testing folder: |
| 183 | +```hcl |
| 184 | + go test -run Test |
| 185 | +``` |
0 commit comments