Skip to content

Commit 296a992

Browse files
authored
Merge pull request #202 from terraform-aws-modules/ssm_and_ec2_vpc_endpoints
Added SSM and EC2 VPC endpoints
2 parents c9bfc7e + ce5212f commit 296a992

File tree

8 files changed

+251
-64
lines changed

8 files changed

+251
-64
lines changed

README.md

Lines changed: 54 additions & 40 deletions
Large diffs are not rendered by default.

examples/complete-vpc/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Note that this example may create resources which can cost money (AWS Elastic IP
2828
| private\_subnets | List of IDs of private subnets |
2929
| public\_subnets | List of IDs of public subnets |
3030
| redshift\_subnets | List of IDs of redshift subnets |
31+
| vpc\_endpoint\_ssm\_dns\_entry | The DNS entries for the VPC Endpoint for SSM. |
32+
| vpc\_endpoint\_ssm\_id | The ID of VPC endpoint for SSM |
33+
| vpc\_endpoint\_ssm\_network\_interface\_ids | One or more network interfaces for the VPC Endpoint for SSM. |
3134
| vpc\_id | The ID of the VPC |
3235

3336
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/complete-vpc/main.tf

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ provider "aws" {
22
region = "eu-west-1"
33
}
44

5+
data "aws_security_group" "default" {
6+
name = "default"
7+
vpc_id = "${module.vpc.vpc_id}"
8+
}
9+
510
module "vpc" {
611
source = "../../"
712

@@ -19,18 +24,35 @@ module "vpc" {
1924

2025
create_database_subnet_group = false
2126

27+
enable_dns_hostnames = true
28+
enable_dns_support = true
29+
2230
enable_nat_gateway = true
2331
single_nat_gateway = true
2432

2533
enable_vpn_gateway = true
2634

27-
enable_s3_endpoint = true
28-
enable_dynamodb_endpoint = true
29-
3035
enable_dhcp_options = true
3136
dhcp_options_domain_name = "service.consul"
3237
dhcp_options_domain_name_servers = ["127.0.0.1", "10.10.0.2"]
3338

39+
# VPC endpoint for S3
40+
enable_s3_endpoint = true
41+
42+
# VPC endpoint for DynamoDB
43+
enable_dynamodb_endpoint = true
44+
45+
# VPC endpoint for SSM
46+
enable_ssm_endpoint = true
47+
ssm_endpoint_private_dns_enabled = true
48+
ssm_endpoint_security_group_ids = ["${data.aws_security_group.default.id}"]
49+
50+
// ssm_endpoint_subnet_ids = ["..."]
51+
52+
# VPC Endpoint for EC2
53+
enable_ec2_endpoint = true
54+
ec2_endpoint_private_dns_enabled = true
55+
ec2_endpoint_security_group_ids = ["${data.aws_security_group.default.id}"]
3456
tags = {
3557
Owner = "user"
3658
Environment = "staging"

examples/complete-vpc/outputs.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,37 @@ output "nat_public_ips" {
4040
description = "List of public Elastic IPs created for AWS NAT Gateway"
4141
value = ["${module.vpc.nat_public_ips}"]
4242
}
43+
44+
# VPC endpoints
45+
output "vpc_endpoint_ssm_id" {
46+
description = "The ID of VPC endpoint for SSM"
47+
value = "${module.vpc.vpc_endpoint_ssm_id}"
48+
}
49+
50+
output "vpc_endpoint_ssm_network_interface_ids" {
51+
description = "One or more network interfaces for the VPC Endpoint for SSM."
52+
value = ["${module.vpc.vpc_endpoint_ssm_network_interface_ids}"]
53+
}
54+
55+
output "vpc_endpoint_ssm_dns_entry" {
56+
description = "The DNS entries for the VPC Endpoint for SSM."
57+
value = ["${module.vpc.vpc_endpoint_ssm_dns_entry}"]
58+
}
59+
60+
//
61+
//# VPC endpoints
62+
//output "vpc_endpoint_ec2_id" {
63+
// description = "The ID of VPC endpoint for EC2"
64+
// value = "${module.vpc.vpc_endpoint_ec2_id}"
65+
//}
66+
//
67+
//output "vpc_endpoint_ec2_network_interface_ids" {
68+
// description = "One or more network interfaces for the VPC Endpoint for EC2."
69+
// value = ["${module.vpc.vpc_endpoint_ec2_network_interface_ids}"]
70+
//}
71+
//
72+
//output "vpc_endpoint_ec2_dns_entry" {
73+
// description = "The DNS entries for the VPC Endpoint for EC2."
74+
// value = ["${module.vpc.vpc_endpoint_ec2_dns_entry}"]
75+
//}
76+

examples/test_fixture/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This will destroy any existing test resources, create the resources afresh, run
2525

2626
| Name | Description | Type | Default | Required |
2727
|------|-------------|:----:|:-----:|:-----:|
28-
| region | - | string | `eu-west-1` | no |
28+
| region | | string | `"eu-west-1"` | no |
2929

3030
## Outputs
3131

main.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,48 @@ resource "aws_vpc_endpoint_route_table_association" "public_dynamodb" {
393393
route_table_id = "${aws_route_table.public.id}"
394394
}
395395

396+
######################
397+
# VPC Endpoint for SSM
398+
######################
399+
data "aws_vpc_endpoint_service" "ssm" {
400+
count = "${var.create_vpc && var.enable_ssm_endpoint ? 1 : 0}"
401+
402+
service = "ssm"
403+
}
404+
405+
resource "aws_vpc_endpoint" "ssm" {
406+
count = "${var.create_vpc && var.enable_ssm_endpoint ? 1 : 0}"
407+
408+
vpc_id = "${local.vpc_id}"
409+
service_name = "${data.aws_vpc_endpoint_service.ssm.service_name}"
410+
vpc_endpoint_type = "Interface"
411+
412+
security_group_ids = ["${var.ssm_endpoint_security_group_ids}"]
413+
subnet_ids = ["${coalescelist(var.ssm_endpoint_subnet_ids, aws_subnet.private.*.id)}"]
414+
private_dns_enabled = "${var.ssm_endpoint_private_dns_enabled}"
415+
}
416+
417+
######################
418+
# VPC Endpoint for EC2
419+
######################
420+
data "aws_vpc_endpoint_service" "ec2" {
421+
count = "${var.create_vpc && var.enable_ec2_endpoint ? 1 : 0}"
422+
423+
service = "ec2"
424+
}
425+
426+
resource "aws_vpc_endpoint" "ec2" {
427+
count = "${var.create_vpc && var.enable_ec2_endpoint ? 1 : 0}"
428+
429+
vpc_id = "${local.vpc_id}"
430+
service_name = "${data.aws_vpc_endpoint_service.ec2.service_name}"
431+
vpc_endpoint_type = "Interface"
432+
433+
security_group_ids = ["${var.ec2_endpoint_security_group_ids}"]
434+
subnet_ids = ["${coalescelist(var.ec2_endpoint_subnet_ids, aws_subnet.private.*.id)}"]
435+
private_dns_enabled = "${var.ec2_endpoint_private_dns_enabled}"
436+
}
437+
396438
##########################
397439
# Route table association
398440
##########################

outputs.tf

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,31 +193,11 @@ output "igw_id" {
193193
value = "${element(concat(aws_internet_gateway.this.*.id, list("")), 0)}"
194194
}
195195

196-
output "vpc_endpoint_s3_id" {
197-
description = "The ID of VPC endpoint for S3"
198-
value = "${element(concat(aws_vpc_endpoint.s3.*.id, list("")), 0)}"
199-
}
200-
201-
output "vpc_endpoint_s3_pl_id" {
202-
description = "The prefix list for the S3 VPC endpoint."
203-
value = "${element(concat(aws_vpc_endpoint.s3.*.prefix_list_id, list("")), 0)}"
204-
}
205-
206-
output "vpc_endpoint_dynamodb_id" {
207-
description = "The ID of VPC endpoint for DynamoDB"
208-
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.id, list("")), 0)}"
209-
}
210-
211196
output "vgw_id" {
212197
description = "The ID of the VPN Gateway"
213198
value = "${element(concat(aws_vpn_gateway.this.*.id, aws_vpn_gateway_attachment.this.*.vpn_gateway_id, list("")), 0)}"
214199
}
215200

216-
output "vpc_endpoint_dynamodb_pl_id" {
217-
description = "The prefix list for the DynamoDB VPC endpoint."
218-
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.prefix_list_id, list("")), 0)}"
219-
}
220-
221201
output "default_vpc_id" {
222202
description = "The ID of the VPC"
223203
value = "${element(concat(aws_default_vpc.this.*.id, list("")), 0)}"
@@ -278,6 +258,58 @@ output "default_vpc_main_route_table_id" {
278258
// value = "${element(concat(aws_default_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
279259
//}
280260

261+
# VPC Endpoints
262+
output "vpc_endpoint_s3_id" {
263+
description = "The ID of VPC endpoint for S3"
264+
value = "${element(concat(aws_vpc_endpoint.s3.*.id, list("")), 0)}"
265+
}
266+
267+
output "vpc_endpoint_s3_pl_id" {
268+
description = "The prefix list for the S3 VPC endpoint."
269+
value = "${element(concat(aws_vpc_endpoint.s3.*.prefix_list_id, list("")), 0)}"
270+
}
271+
272+
output "vpc_endpoint_dynamodb_id" {
273+
description = "The ID of VPC endpoint for DynamoDB"
274+
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.id, list("")), 0)}"
275+
}
276+
277+
output "vpc_endpoint_dynamodb_pl_id" {
278+
description = "The prefix list for the DynamoDB VPC endpoint."
279+
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.prefix_list_id, list("")), 0)}"
280+
}
281+
282+
output "vpc_endpoint_ssm_id" {
283+
description = "The ID of VPC endpoint for SSM"
284+
value = "${element(concat(aws_vpc_endpoint.ssm.*.id, list("")), 0)}"
285+
}
286+
287+
output "vpc_endpoint_ssm_network_interface_ids" {
288+
description = "One or more network interfaces for the VPC Endpoint for SSM."
289+
value = "${flatten(aws_vpc_endpoint.ssm.*.network_interface_ids)}"
290+
}
291+
292+
output "vpc_endpoint_ssm_dns_entry" {
293+
description = "The DNS entries for the VPC Endpoint for SSM."
294+
value = "${flatten(aws_vpc_endpoint.ssm.*.dns_entry)}"
295+
}
296+
297+
output "vpc_endpoint_ec2_id" {
298+
description = "The ID of VPC endpoint for EC2"
299+
value = "${element(concat(aws_vpc_endpoint.ec2.*.id, list("")), 0)}"
300+
}
301+
302+
output "vpc_endpoint_ec2_network_interface_ids" {
303+
description = "One or more network interfaces for the VPC Endpoint for EC2"
304+
value = "${flatten(aws_vpc_endpoint.ec2.*.network_interface_ids)}"
305+
}
306+
307+
output "vpc_endpoint_ec2_dns_entry" {
308+
description = "The DNS entries for the VPC Endpoint for EC2."
309+
value = "${flatten(aws_vpc_endpoint.ec2.*.dns_entry)}"
310+
}
311+
312+
# Static values (arguments)
281313
output "azs" {
282314
description = "A list of availability zones specified as argument to this module"
283315
value = "${var.azs}"

variables.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,46 @@ variable "enable_s3_endpoint" {
173173
default = false
174174
}
175175

176+
variable "enable_ssm_endpoint" {
177+
description = "Should be true if you want to provision an SSM endpoint to the VPC"
178+
default = false
179+
}
180+
181+
variable "ssm_endpoint_security_group_ids" {
182+
description = "The ID of one or more security groups to associate with the network interface for SSM endpoint"
183+
default = []
184+
}
185+
186+
variable "ssm_endpoint_subnet_ids" {
187+
description = "The ID of one or more subnets in which to create a network interface for SSM endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used."
188+
default = []
189+
}
190+
191+
variable "ssm_endpoint_private_dns_enabled" {
192+
description = "Whether or not to associate a private hosted zone with the specified VPC for SSM endpoint"
193+
default = false
194+
}
195+
196+
variable "enable_ec2_endpoint" {
197+
description = "Should be true if you want to provision an EC2 endpoint to the VPC"
198+
default = false
199+
}
200+
201+
variable "ec2_endpoint_security_group_ids" {
202+
description = "The ID of one or more security groups to associate with the network interface for EC2 endpoint"
203+
default = []
204+
}
205+
206+
variable "ec2_endpoint_private_dns_enabled" {
207+
description = "Whether or not to associate a private hosted zone with the specified VPC for EC2 endpoint"
208+
default = false
209+
}
210+
211+
variable "ec2_endpoint_subnet_ids" {
212+
description = "The ID of one or more subnets in which to create a network interface for EC2 endpoint. Only a single subnet within an AZ is supported. If omitted, private subnets will be used."
213+
default = []
214+
}
215+
176216
variable "map_public_ip_on_launch" {
177217
description = "Should be false if you do not want to auto-assign public IP on launch"
178218
default = true

0 commit comments

Comments
 (0)