Skip to content

Commit c4bc048

Browse files
authored
Added support for default VPC resource (terraform-aws-modules#75)
1 parent 07654cf commit c4bc048

File tree

7 files changed

+184
-3
lines changed

7 files changed

+184
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ These types of resources are supported:
2121
* [DHCP Options Set](https://www.terraform.io/docs/providers/aws/r/vpc_dhcp_options.html)
2222
* [Main VPC Routing Table](https://www.terraform.io/docs/providers/aws/r/main_route_table_assoc.html)
2323
* [Default VPC Routing Table](https://www.terraform.io/docs/providers/aws/r/default_route_table.html)
24+
* [Default VPC](https://www.terraform.io/docs/providers/aws/r/default_vpc.html)
2425

2526
Usage
2627
-----
@@ -107,6 +108,7 @@ Examples
107108

108109
* [Simple VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/simple-vpc)
109110
* [Complete VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/complete-vpc)
111+
* [Manage Default VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/manage-default-vpc)
110112
* Few tests and edge cases examples: [#46](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/issue-46-no-private-subnets), [#44](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/issue-44-asymmetric-private-subnets)
111113

112114

examples/manage-default-vpc/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Manage Default VPC
2+
==================
3+
4+
Configuration in this directory does not create new VPC resources, but it adopts [Default VPC](https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/default-vpc.html) created by AWS to allow management of it using Terraform.
5+
6+
This is not usual type of resource in Terraform, so use it carefully. More information is [here](https://www.terraform.io/docs/providers/aws/r/default_vpc.html).
7+
8+
Usage
9+
=====
10+
11+
To run this example you need to execute:
12+
13+
```bash
14+
$ terraform init
15+
$ terraform plan
16+
$ terraform apply
17+
```
18+
19+
Run `terraform destroy` when you don't need these resources.

examples/manage-default-vpc/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
module "vpc" {
6+
source = "../../"
7+
8+
create_vpc = false
9+
10+
manage_default_vpc = true
11+
default_vpc_name = "default"
12+
default_vpc_enable_dns_hostnames = true
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Default VPC
2+
output "default_vpc_id" {
3+
description = "The ID of the Default VPC"
4+
value = "${module.vpc.default_vpc_id}"
5+
}
6+
7+
output "default_vpc_cidr_block" {
8+
description = "The CIDR block of the VPC"
9+
value = "${module.vpc.default_vpc_cidr_block}"
10+
}

main.tf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,27 @@ resource "aws_vpn_gateway" "this" {
343343
###########
344344
# Defaults
345345
###########
346-
resource "aws_default_route_table" "default" {
346+
resource "aws_default_vpc" "this" {
347+
count = "${var.manage_default_vpc ? 1 : 0}"
348+
349+
enable_dns_support = "${var.default_vpc_enable_dns_support}"
350+
enable_dns_hostnames = "${var.default_vpc_enable_dns_hostnames}"
351+
enable_classiclink = "${var.default_vpc_enable_classiclink}"
352+
353+
tags = "${merge(var.tags, var.default_vpc_tags, map("Name", format("%s", var.default_vpc_name)))}"
354+
}
355+
356+
resource "aws_default_route_table" "this" {
347357
count = "${var.create_vpc ? 1 : 0}"
348358

349359
default_route_table_id = "${aws_vpc.this.default_route_table_id}"
350360

351361
tags = "${merge(var.tags, var.default_route_table_tags, map("Name", format("%s-default", var.name)))}"
352362
}
353363

354-
resource "aws_main_route_table_association" "default" {
364+
resource "aws_main_route_table_association" "this" {
355365
count = "${var.create_vpc ? 1 : 0}"
356366

357367
vpc_id = "${aws_vpc.this.id}"
358-
route_table_id = "${aws_default_route_table.default.default_route_table_id}"
368+
route_table_id = "${aws_default_route_table.this.default_route_table_id}"
359369
}

outputs.tf

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,41 @@ output "default_route_table_id" {
2424
value = "${element(concat(aws_vpc.this.*.default_route_table_id, list("")), 0)}"
2525
}
2626

27+
output "vpc_instance_tenancy" {
28+
description = "Tenancy of instances spin up within VPC"
29+
value = "${element(concat(aws_vpc.this.*.instance_tenancy, list("")), 0)}"
30+
}
31+
32+
output "vpc_enable_dns_support" {
33+
description = "Whether or not the VPC has DNS support"
34+
value = "${element(concat(aws_vpc.this.*.enable_dns_support, list("")), 0)}"
35+
}
36+
37+
output "vpc_enable_dns_hostnames" {
38+
description = "Whether or not the VPC has DNS hostname support"
39+
value = "${element(concat(aws_vpc.this.*.enable_dns_hostnames, list("")), 0)}"
40+
}
41+
42+
output "vpc_enable_classiclink" {
43+
description = "Whether or not the VPC has Classiclink enabled"
44+
value = "${element(concat(aws_vpc.this.*.enable_classiclink, list("")), 0)}"
45+
}
46+
47+
output "vpc_main_route_table_id" {
48+
description = "The ID of the main route table associated with this VPC"
49+
value = "${element(concat(aws_vpc.this.*.main_route_table_id, list("")), 0)}"
50+
}
51+
52+
//output "vpc_ipv6_association_id" {
53+
// description = "The association ID for the IPv6 CIDR block"
54+
// value = "${element(concat(aws_vpc.this.*.ipv6_association_id, list("")), 0)}"
55+
//}
56+
//
57+
//output "vpc_ipv6_cidr_block" {
58+
// description = "The IPv6 CIDR block"
59+
// value = "${element(concat(aws_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
60+
//}
61+
2762
# Subnets
2863
output "private_subnets" {
2964
description = "List of IDs of private subnets"
@@ -153,3 +188,65 @@ output "vpc_endpoint_dynamodb_pl_id" {
153188
description = "The prefix list for the DynamoDB VPC endpoint."
154189
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.prefix_list_id, list("")), 0)}"
155190
}
191+
192+
# Default VPC
193+
output "default_vpc_id" {
194+
description = "The ID of the VPC"
195+
value = "${element(concat(aws_default_vpc.this.*.id, list("")), 0)}"
196+
}
197+
198+
output "default_vpc_cidr_block" {
199+
description = "The CIDR block of the VPC"
200+
value = "${element(concat(aws_default_vpc.this.*.cidr_block, list("")), 0)}"
201+
}
202+
203+
output "default_vpc_default_security_group_id" {
204+
description = "The ID of the security group created by default on VPC creation"
205+
value = "${element(concat(aws_default_vpc.this.*.default_security_group_id, list("")), 0)}"
206+
}
207+
208+
output "default_vpc_default_network_acl_id" {
209+
description = "The ID of the default network ACL"
210+
value = "${element(concat(aws_default_vpc.this.*.default_network_acl_id, list("")), 0)}"
211+
}
212+
213+
output "default_vpc_default_route_table_id" {
214+
description = "The ID of the default route table"
215+
value = "${element(concat(aws_default_vpc.this.*.default_route_table_id, list("")), 0)}"
216+
}
217+
218+
output "default_vpc_instance_tenancy" {
219+
description = "Tenancy of instances spin up within VPC"
220+
value = "${element(concat(aws_default_vpc.this.*.instance_tenancy, list("")), 0)}"
221+
}
222+
223+
output "default_vpc_enable_dns_support" {
224+
description = "Whether or not the VPC has DNS support"
225+
value = "${element(concat(aws_default_vpc.this.*.enable_dns_support, list("")), 0)}"
226+
}
227+
228+
output "default_vpc_enable_dns_hostnames" {
229+
description = "Whether or not the VPC has DNS hostname support"
230+
value = "${element(concat(aws_default_vpc.this.*.enable_dns_hostnames, list("")), 0)}"
231+
}
232+
233+
output "default_vpc_enable_classiclink" {
234+
description = "Whether or not the VPC has Classiclink enabled"
235+
value = "${element(concat(aws_default_vpc.this.*.enable_classiclink, list("")), 0)}"
236+
}
237+
238+
output "default_vpc_main_route_table_id" {
239+
description = "The ID of the main route table associated with this VPC"
240+
value = "${element(concat(aws_default_vpc.this.*.main_route_table_id, list("")), 0)}"
241+
}
242+
243+
//output "default_vpc_ipv6_association_id" {
244+
// description = "The association ID for the IPv6 CIDR block"
245+
// value = "${element(concat(aws_default_vpc.this.*.ipv6_association_id, list("")), 0)}"
246+
//}
247+
//
248+
//output "default_vpc_ipv6_cidr_block" {
249+
// description = "The IPv6 CIDR block"
250+
// value = "${element(concat(aws_default_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
251+
//}
252+

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,33 @@ variable "dhcp_options_netbios_node_type" {
204204
description = "Specify netbios node_type for DHCP options set"
205205
default = ""
206206
}
207+
208+
variable "manage_default_vpc" {
209+
description = "Should be true to adopt and manage Default VPC"
210+
default = false
211+
}
212+
213+
variable "default_vpc_name" {
214+
description = "Name to be used on the Default VPC"
215+
default = ""
216+
}
217+
218+
variable "default_vpc_enable_dns_support" {
219+
description = "Should be true to enable DNS support in the Default VPC"
220+
default = true
221+
}
222+
223+
variable "default_vpc_enable_dns_hostnames" {
224+
description = "Should be true to enable DNS hostnames in the Default VPC"
225+
default = false
226+
}
227+
228+
variable "default_vpc_enable_classiclink" {
229+
description = "Should be true to enable ClassicLink in the Default VPC"
230+
default = false
231+
}
232+
233+
variable "default_vpc_tags" {
234+
description = "Additional tags for the Default VPC"
235+
default = {}
236+
}

0 commit comments

Comments
 (0)