Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for default VPC resource #75

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ These types of resources are supported:
* [DHCP Options Set](https://www.terraform.io/docs/providers/aws/r/vpc_dhcp_options.html)
* [Main VPC Routing Table](https://www.terraform.io/docs/providers/aws/r/main_route_table_assoc.html)
* [Default VPC Routing Table](https://www.terraform.io/docs/providers/aws/r/default_route_table.html)
* [Default VPC](https://www.terraform.io/docs/providers/aws/r/default_vpc.html)

Usage
-----
Expand Down Expand Up @@ -107,6 +108,7 @@ Examples

* [Simple VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/simple-vpc)
* [Complete VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/complete-vpc)
* [Manage Default VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/manage-default-vpc)
* 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)


Expand Down
19 changes: 19 additions & 0 deletions examples/manage-default-vpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Manage Default VPC
==================

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.

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).

Usage
=====

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Run `terraform destroy` when you don't need these resources.
13 changes: 13 additions & 0 deletions examples/manage-default-vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "aws" {
region = "eu-west-1"
}

module "vpc" {
source = "../../"

create_vpc = false

manage_default_vpc = true
default_vpc_name = "default"
default_vpc_enable_dns_hostnames = true
}
10 changes: 10 additions & 0 deletions examples/manage-default-vpc/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Default VPC
output "default_vpc_id" {
description = "The ID of the Default VPC"
value = "${module.vpc.default_vpc_id}"
}

output "default_vpc_cidr_block" {
description = "The CIDR block of the VPC"
value = "${module.vpc.default_vpc_cidr_block}"
}
16 changes: 13 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,27 @@ resource "aws_vpn_gateway" "this" {
###########
# Defaults
###########
resource "aws_default_route_table" "default" {
resource "aws_default_vpc" "this" {
count = "${var.manage_default_vpc ? 1 : 0}"

enable_dns_support = "${var.default_vpc_enable_dns_support}"
enable_dns_hostnames = "${var.default_vpc_enable_dns_hostnames}"
enable_classiclink = "${var.default_vpc_enable_classiclink}"

tags = "${merge(var.tags, var.default_vpc_tags, map("Name", format("%s", var.default_vpc_name)))}"
}

resource "aws_default_route_table" "this" {
count = "${var.create_vpc ? 1 : 0}"

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

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

resource "aws_main_route_table_association" "default" {
resource "aws_main_route_table_association" "this" {
count = "${var.create_vpc ? 1 : 0}"

vpc_id = "${aws_vpc.this.id}"
route_table_id = "${aws_default_route_table.default.default_route_table_id}"
route_table_id = "${aws_default_route_table.this.default_route_table_id}"
}
97 changes: 97 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ output "default_route_table_id" {
value = "${element(concat(aws_vpc.this.*.default_route_table_id, list("")), 0)}"
}

output "vpc_instance_tenancy" {
description = "Tenancy of instances spin up within VPC"
value = "${element(concat(aws_vpc.this.*.instance_tenancy, list("")), 0)}"
}

output "vpc_enable_dns_support" {
description = "Whether or not the VPC has DNS support"
value = "${element(concat(aws_vpc.this.*.enable_dns_support, list("")), 0)}"
}

output "vpc_enable_dns_hostnames" {
description = "Whether or not the VPC has DNS hostname support"
value = "${element(concat(aws_vpc.this.*.enable_dns_hostnames, list("")), 0)}"
}

output "vpc_enable_classiclink" {
description = "Whether or not the VPC has Classiclink enabled"
value = "${element(concat(aws_vpc.this.*.enable_classiclink, list("")), 0)}"
}

output "vpc_main_route_table_id" {
description = "The ID of the main route table associated with this VPC"
value = "${element(concat(aws_vpc.this.*.main_route_table_id, list("")), 0)}"
}

//output "vpc_ipv6_association_id" {
// description = "The association ID for the IPv6 CIDR block"
// value = "${element(concat(aws_vpc.this.*.ipv6_association_id, list("")), 0)}"
//}
//
//output "vpc_ipv6_cidr_block" {
// description = "The IPv6 CIDR block"
// value = "${element(concat(aws_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
//}

# Subnets
output "private_subnets" {
description = "List of IDs of private subnets"
Expand Down Expand Up @@ -153,3 +188,65 @@ output "vpc_endpoint_dynamodb_pl_id" {
description = "The prefix list for the DynamoDB VPC endpoint."
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.prefix_list_id, list("")), 0)}"
}

# Default VPC
output "default_vpc_id" {
description = "The ID of the VPC"
value = "${element(concat(aws_default_vpc.this.*.id, list("")), 0)}"
}

output "default_vpc_cidr_block" {
description = "The CIDR block of the VPC"
value = "${element(concat(aws_default_vpc.this.*.cidr_block, list("")), 0)}"
}

output "default_vpc_default_security_group_id" {
description = "The ID of the security group created by default on VPC creation"
value = "${element(concat(aws_default_vpc.this.*.default_security_group_id, list("")), 0)}"
}

output "default_vpc_default_network_acl_id" {
description = "The ID of the default network ACL"
value = "${element(concat(aws_default_vpc.this.*.default_network_acl_id, list("")), 0)}"
}

output "default_vpc_default_route_table_id" {
description = "The ID of the default route table"
value = "${element(concat(aws_default_vpc.this.*.default_route_table_id, list("")), 0)}"
}

output "default_vpc_instance_tenancy" {
description = "Tenancy of instances spin up within VPC"
value = "${element(concat(aws_default_vpc.this.*.instance_tenancy, list("")), 0)}"
}

output "default_vpc_enable_dns_support" {
description = "Whether or not the VPC has DNS support"
value = "${element(concat(aws_default_vpc.this.*.enable_dns_support, list("")), 0)}"
}

output "default_vpc_enable_dns_hostnames" {
description = "Whether or not the VPC has DNS hostname support"
value = "${element(concat(aws_default_vpc.this.*.enable_dns_hostnames, list("")), 0)}"
}

output "default_vpc_enable_classiclink" {
description = "Whether or not the VPC has Classiclink enabled"
value = "${element(concat(aws_default_vpc.this.*.enable_classiclink, list("")), 0)}"
}

output "default_vpc_main_route_table_id" {
description = "The ID of the main route table associated with this VPC"
value = "${element(concat(aws_default_vpc.this.*.main_route_table_id, list("")), 0)}"
}

//output "default_vpc_ipv6_association_id" {
// description = "The association ID for the IPv6 CIDR block"
// value = "${element(concat(aws_default_vpc.this.*.ipv6_association_id, list("")), 0)}"
//}
//
//output "default_vpc_ipv6_cidr_block" {
// description = "The IPv6 CIDR block"
// value = "${element(concat(aws_default_vpc.this.*.ipv6_cidr_block, list("")), 0)}"
//}

30 changes: 30 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,33 @@ variable "dhcp_options_netbios_node_type" {
description = "Specify netbios node_type for DHCP options set"
default = ""
}

variable "manage_default_vpc" {
description = "Should be true to adopt and manage Default VPC"
default = false
}

variable "default_vpc_name" {
description = "Name to be used on the Default VPC"
default = ""
}

variable "default_vpc_enable_dns_support" {
description = "Should be true to enable DNS support in the Default VPC"
default = true
}

variable "default_vpc_enable_dns_hostnames" {
description = "Should be true to enable DNS hostnames in the Default VPC"
default = false
}

variable "default_vpc_enable_classiclink" {
description = "Should be true to enable ClassicLink in the Default VPC"
default = false
}

variable "default_vpc_tags" {
description = "Additional tags for the Default VPC"
default = {}
}