Skip to content

Commit 08c7659

Browse files
nloutasantonbabenko
authored andcommitted
Add support for KMS VPC endpoint creation (#243)
1 parent 39f1338 commit 08c7659

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ These types of resources are supported:
1616
* [VPN Gateway](https://www.terraform.io/docs/providers/aws/r/vpn_gateway.html)
1717
* [VPC Endpoint](https://www.terraform.io/docs/providers/aws/r/vpc_endpoint.html):
1818
* Gateway: S3, DynamoDB
19-
* Interface: EC2, SSM, EC2 Messages, SSM Messages, ECR API, ECR DKR, API Gateway
19+
* Interface: EC2, SSM, EC2 Messages, SSM Messages, ECR API, ECR DKR, API Gateway, KMS
2020
* [RDS DB Subnet Group](https://www.terraform.io/docs/providers/aws/r/db_subnet_group.html)
2121
* [ElastiCache Subnet Group](https://www.terraform.io/docs/providers/aws/r/elasticache_subnet_group.html)
2222
* [Redshift Subnet Group](https://www.terraform.io/docs/providers/aws/r/redshift_subnet_group.html)
@@ -269,6 +269,7 @@ Terraform version 0.10.3 or newer is required for this module to work.
269269
| enable\_ec2messages\_endpoint | Should be true if you want to provision an EC2MESSAGES endpoint to the VPC | string | `"false"` | no |
270270
| enable\_ecr\_api\_endpoint | Should be true if you want to provision an ecr api endpoint to the VPC | string | `"false"` | no |
271271
| enable\_ecr\_dkr\_endpoint | Should be true if you want to provision an ecr dkr endpoint to the VPC | string | `"false"` | no |
272+
| enable\_kms\_endpoint | Should be true if you want to provision a KMS endpoint to the VPC | string | `"false"` | no |
272273
| enable\_nat\_gateway | Should be true if you want to provision NAT Gateways for each of your private networks | string | `"false"` | no |
273274
| enable\_public\_redshift | Controls if redshift should have public routing table | string | `"false"` | no |
274275
| enable\_s3\_endpoint | Should be true if you want to provision an S3 endpoint to the VPC | string | `"false"` | no |
@@ -286,6 +287,9 @@ Terraform version 0.10.3 or newer is required for this module to work.
286287
| intra\_subnet\_suffix | Suffix to append to intra subnets name | string | `"intra"` | no |
287288
| intra\_subnet\_tags | Additional tags for the intra subnets | map | `{}` | no |
288289
| intra\_subnets | A list of intra subnets | list | `[]` | no |
290+
| kms\_endpoint\_private\_dns\_enabled | Whether or not to associate a private hosted zone with the specified VPC for KMS endpoint | string | `"false"` | no |
291+
| kms\_endpoint\_security\_group\_ids | The ID of one or more security groups to associate with the network interface for KMS endpoint | list | `[]` | no |
292+
| kms\_endpoint\_subnet\_ids | The ID of one or more subnets in which to create a network interface for KMS endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used. | list | `[]` | no |
289293
| manage\_default\_network\_acl | Should be true to adopt and manage Default Network ACL | string | `"false"` | no |
290294
| manage\_default\_vpc | Should be true to adopt and manage Default VPC | string | `"false"` | no |
291295
| map\_public\_ip\_on\_launch | Should be false if you do not want to auto-assign public IP on launch | string | `"true"` | no |
@@ -404,6 +408,9 @@ Terraform version 0.10.3 or newer is required for this module to work.
404408
| vpc\_endpoint\_ecr\_dkr\_dns\_entry | The DNS entries for the VPC Endpoint for ECR DKR. |
405409
| vpc\_endpoint\_ecr\_dkr\_id | The ID of VPC endpoint for ECR DKR |
406410
| vpc\_endpoint\_ecr\_dkr\_network\_interface\_ids | One or more network interfaces for the VPC Endpoint for ECR DKR. |
411+
| vpc\_endpoint\_kms\_dns\_entry | The DNS entries for the VPC Endpoint for KMS. |
412+
| vpc\_endpoint\_kms\_id | The ID of VPC endpoint for KMS |
413+
| vpc\_endpoint\_kms\_network\_interface\_ids | One or more network interfaces for the VPC Endpoint for KMS. |
407414
| vpc\_endpoint\_s3\_id | The ID of VPC endpoint for S3 |
408415
| vpc\_endpoint\_s3\_pl\_id | The prefix list for the S3 VPC endpoint. |
409416
| vpc\_endpoint\_ssm\_dns\_entry | The DNS entries for the VPC Endpoint for SSM. |

examples/complete-vpc/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ module "vpc" {
7272
ecr_dkr_endpoint_private_dns_enabled = true
7373
ecr_dkr_endpoint_security_group_ids = ["${data.aws_security_group.default.id}"]
7474

75+
# VPC endpoint for KMS
76+
enable_kms_endpoint = true
77+
kms_endpoint_private_dns_enabled = true
78+
kms_endpoint_security_group_ids = ["${data.aws_security_group.default.id}"]
79+
80+
# kms_endpoint_subnet_ids = ["..."]
81+
7582
tags = {
7683
Owner = "user"
7784
Environment = "staging"

main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,27 @@ resource "aws_vpc_endpoint" "apigw" {
809809
private_dns_enabled = "${var.apigw_endpoint_private_dns_enabled}"
810810
}
811811

812+
#######################
813+
# VPC Endpoint for KMS
814+
#######################
815+
data "aws_vpc_endpoint_service" "kms" {
816+
count = "${var.create_vpc && var.enable_kms_endpoint ? 1 : 0}"
817+
818+
service = "kms"
819+
}
820+
821+
resource "aws_vpc_endpoint" "kms" {
822+
count = "${var.create_vpc && var.enable_kms_endpoint ? 1 : 0}"
823+
824+
vpc_id = "${local.vpc_id}"
825+
service_name = "${data.aws_vpc_endpoint_service.kms.service_name}"
826+
vpc_endpoint_type = "Interface"
827+
828+
security_group_ids = ["${var.kms_endpoint_security_group_ids}"]
829+
subnet_ids = ["${coalescelist(var.kms_endpoint_subnet_ids, aws_subnet.private.*.id)}"]
830+
private_dns_enabled = "${var.kms_endpoint_private_dns_enabled}"
831+
}
832+
812833
##########################
813834
# Route table association
814835
##########################

outputs.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,21 @@ output "vpc_endpoint_ec2messages_dns_entry" {
369369
value = "${flatten(aws_vpc_endpoint.ec2messages.*.dns_entry)}"
370370
}
371371

372+
output "vpc_endpoint_kms_id" {
373+
description = "The ID of VPC endpoint for KMS"
374+
value = "${element(concat(aws_vpc_endpoint.kms.*.id, list("")), 0)}"
375+
}
376+
377+
output "vpc_endpoint_kms_network_interface_ids" {
378+
description = "One or more network interfaces for the VPC Endpoint for KMS."
379+
value = "${flatten(aws_vpc_endpoint.kms.*.network_interface_ids)}"
380+
}
381+
382+
output "vpc_endpoint_kms_dns_entry" {
383+
description = "The DNS entries for the VPC Endpoint for KMS."
384+
value = "${flatten(aws_vpc_endpoint.kms.*.dns_entry)}"
385+
}
386+
372387
output "vpc_endpoint_ecr_api_id" {
373388
description = "The ID of VPC endpoint for ECR API"
374389
value = "${element(concat(aws_vpc_endpoint.ecr_api.*.id, list("")), 0)}"

variables.tf

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,21 @@ variable "private_subnets" {
6969
}
7070

7171
variable "database_subnets" {
72-
type = "list"
7372
description = "A list of database subnets"
7473
default = []
7574
}
7675

7776
variable "redshift_subnets" {
78-
type = "list"
7977
description = "A list of redshift subnets"
8078
default = []
8179
}
8280

8381
variable "elasticache_subnets" {
84-
type = "list"
8582
description = "A list of elasticache subnets"
8683
default = []
8784
}
8885

8986
variable "intra_subnets" {
90-
type = "list"
9187
description = "A list of intra subnets"
9288
default = []
9389
}
@@ -174,8 +170,8 @@ variable "reuse_nat_ips" {
174170

175171
variable "external_nat_ip_ids" {
176172
description = "List of EIP IDs to be assigned to the NAT Gateways (used in combination with reuse_nat_ips)"
177-
type = "list"
178-
default = []
173+
174+
default = []
179175
}
180176

181177
variable "enable_dynamodb_endpoint" {
@@ -328,6 +324,26 @@ variable "ecr_dkr_endpoint_security_group_ids" {
328324
default = []
329325
}
330326

327+
variable "enable_kms_endpoint" {
328+
description = "Should be true if you want to provision a KMS endpoint to the VPC"
329+
default = false
330+
}
331+
332+
variable "kms_endpoint_security_group_ids" {
333+
description = "The ID of one or more security groups to associate with the network interface for KMS endpoint"
334+
default = []
335+
}
336+
337+
variable "kms_endpoint_subnet_ids" {
338+
description = "The ID of one or more subnets in which to create a network interface for KMS endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used."
339+
default = []
340+
}
341+
342+
variable "kms_endpoint_private_dns_enabled" {
343+
description = "Whether or not to associate a private hosted zone with the specified VPC for KMS endpoint"
344+
default = false
345+
}
346+
331347
variable "map_public_ip_on_launch" {
332348
description = "Should be false if you do not want to auto-assign public IP on launch"
333349
default = true
@@ -505,20 +521,20 @@ variable "dhcp_options_domain_name" {
505521

506522
variable "dhcp_options_domain_name_servers" {
507523
description = "Specify a list of DNS server addresses for DHCP options set, default to AWS provided"
508-
type = "list"
509-
default = ["AmazonProvidedDNS"]
524+
525+
default = ["AmazonProvidedDNS"]
510526
}
511527

512528
variable "dhcp_options_ntp_servers" {
513529
description = "Specify a list of NTP servers for DHCP options set"
514-
type = "list"
515-
default = []
530+
531+
default = []
516532
}
517533

518534
variable "dhcp_options_netbios_name_servers" {
519535
description = "Specify a list of netbios servers for DHCP options set"
520-
type = "list"
521-
default = []
536+
537+
default = []
522538
}
523539

524540
variable "dhcp_options_netbios_node_type" {

0 commit comments

Comments
 (0)