Commit c4bc0483 authored by Anton Babenko's avatar Anton Babenko Committed by GitHub

Added support for default VPC resource (#75)

parent 07654cff
...@@ -21,6 +21,7 @@ These types of resources are supported: ...@@ -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) * [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) * [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 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 Usage
----- -----
...@@ -107,6 +108,7 @@ Examples ...@@ -107,6 +108,7 @@ Examples
* [Simple VPC](https://github.com/terraform-aws-modules/terraform-aws-vpc/tree/master/examples/simple-vpc) * [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) * [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) * 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)
......
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.
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
}
# 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}"
}
...@@ -343,7 +343,17 @@ resource "aws_vpn_gateway" "this" { ...@@ -343,7 +343,17 @@ resource "aws_vpn_gateway" "this" {
########### ###########
# Defaults # 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}" count = "${var.create_vpc ? 1 : 0}"
default_route_table_id = "${aws_vpc.this.default_route_table_id}" default_route_table_id = "${aws_vpc.this.default_route_table_id}"
...@@ -351,9 +361,9 @@ resource "aws_default_route_table" "default" { ...@@ -351,9 +361,9 @@ resource "aws_default_route_table" "default" {
tags = "${merge(var.tags, var.default_route_table_tags, map("Name", format("%s-default", var.name)))}" 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}" count = "${var.create_vpc ? 1 : 0}"
vpc_id = "${aws_vpc.this.id}" 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}"
} }
...@@ -24,6 +24,41 @@ output "default_route_table_id" { ...@@ -24,6 +24,41 @@ output "default_route_table_id" {
value = "${element(concat(aws_vpc.this.*.default_route_table_id, list("")), 0)}" 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 # Subnets
output "private_subnets" { output "private_subnets" {
description = "List of IDs of private subnets" description = "List of IDs of private subnets"
...@@ -153,3 +188,65 @@ output "vpc_endpoint_dynamodb_pl_id" { ...@@ -153,3 +188,65 @@ output "vpc_endpoint_dynamodb_pl_id" {
description = "The prefix list for the DynamoDB VPC endpoint." description = "The prefix list for the DynamoDB VPC endpoint."
value = "${element(concat(aws_vpc_endpoint.dynamodb.*.prefix_list_id, list("")), 0)}" 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)}"
//}
...@@ -204,3 +204,33 @@ variable "dhcp_options_netbios_node_type" { ...@@ -204,3 +204,33 @@ variable "dhcp_options_netbios_node_type" {
description = "Specify netbios node_type for DHCP options set" description = "Specify netbios node_type for DHCP options set"
default = "" 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 = {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment