Skip to content

Commit

Permalink
Update tf_subnets with cidr_block input (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
const-bon authored Aug 21, 2017
1 parent 9c41cf1 commit a52f912
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ module "subnets" {
region = "${var.region}"
vpc_id = "${var.vpc_id}"
igw_id = "${var.igw_id}"
cidr_block = "${var.cidr_block}"
vpc_default_route_table_id = "${var.vpc_default_route_table_id}"
}
```

## Variables

| Name | Default | Description | Required |
|:----------------------------:|:--------------:|:--------------------------------------------------------:|:--------:|
| namespace | `` | Namespace (e.g. `cp` or `cloudposse`) | Yes |
| stage | `` | Stage (e.g. `prod`, `dev`, `staging`) | Yes |
| name | `` | Name (e.g. `bastion` or `db`) | Yes |
| region | `` | AWS Region where module should operate (e.g. `us-east-1`)| Yes |
| vpc_id | `` | The VPC ID where subnets will be created (e.g. `vpc-aceb2723`) | Yes |
| igw_id | `` | The Internet Gateway ID public route table will point to (e.g. `igw-9c26a123`) | Yes |
| vpc_default_route_table_id | `` | The scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes) | No |
| availability_zones | [] | The scheduling expression. (e.g. cron(0 20 * * ? *) or rate(5 minutes) | Yes |
| Name | Default | Description | Required |
|:----------------------------:|:--------------:|:------------------------------------------------------------------------------------------------------------------------------------:|:--------:|
| namespace | `` | Namespace (e.g. `cp` or `cloudposse`) | Yes |
| stage | `` | Stage (e.g. `prod`, `dev`, `staging`) | Yes |
| name | `` | Name (e.g. `bastion` or `db`) | Yes |
| region | `` | AWS Region where module should operate (e.g. `us-east-1`) | Yes |
| vpc_id | `` | The VPC ID where subnets will be created (e.g. `vpc-aceb2723`) | Yes |
| igw_id | `` | The Internet Gateway ID public route table will point to (e.g. `igw-9c26a123`) | Yes |
| cidr_block | `` | The base CIDR block which will be divided into subnet CIDR blocks (e.g. `10.0.0.0/16`) | Yes |
| vpc_default_route_table_id | `` | The default route table for public subnets. Provides access to the Internet. If not set here, will be created. (e.g. `rtb-f4f0ce12`) | No |
| availability_zones | [] | The list of Availability Zones where subnets will be created (e.g. `["us-eas-1a", "us-eas-1b"]`) | Yes |
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ provider "aws" {
data "aws_vpc" "default" {
id = "${var.vpc_id}"
}

data "aws_availability_zones" "available" {}
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ output "public_subnet_ids" {
output "private_subnet_ids" {
value = ["${aws_subnet.private.*.id}"]
}

output "test" {
value = "${data.aws_availability_zones.available.count}"
}
12 changes: 10 additions & 2 deletions private.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ resource "aws_subnet" "private" {

vpc_id = "${data.aws_vpc.default.id}"
availability_zone = "${element(var.availability_zones, count.index)}"
cidr_block = "${cidrsubnet(data.aws_vpc.default.cidr_block, length(var.availability_zones), length(var.availability_zones) + count.index)}"
tags = "${module.private_label.tags}"

cidr_block = "${
cidrsubnet(
signum(length(var.cidr_block)) == 1 ?
var.cidr_block : data.aws_vpc.default.cidr_block,
ceil(log(length(data.aws_availability_zones.available.names) * 2, 2)),
count.index)
}"

tags = "${module.private_label.tags}"
}

resource "aws_route_table" "private" {
Expand Down
16 changes: 12 additions & 4 deletions public.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ resource "aws_subnet" "public" {

vpc_id = "${data.aws_vpc.default.id}"
availability_zone = "${element(var.availability_zones, count.index)}"
cidr_block = "${cidrsubnet(data.aws_vpc.default.cidr_block, length(var.availability_zones), count.index)}"

cidr_block = "${
cidrsubnet(
signum(length(var.cidr_block)) == 1 ?
var.cidr_block : data.aws_vpc.default.cidr_block,
ceil(log(length(data.aws_availability_zones.available.names) * 2, 2)),
length(data.aws_availability_zones.available.names) + count.index)
}"

tags = "${module.public_label.tags}"
}
Expand All @@ -23,17 +30,18 @@ resource "aws_route_table" "public" {
cidr_block = "0.0.0.0/0"
gateway_id = "${var.igw_id}"
}

tags = "${module.public_label.tags}"
}

resource "aws_route_table_association" "public" {
count = "${signum(length(var.vpc_default_route_table_id)) == 1 ? 0 : length(var.availability_zones)}"
count = "${signum(length(var.vpc_default_route_table_id)) == 1 ? 0 : length(var.availability_zones)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
}

resource "aws_route_table_association" "public_default" {
count = "${signum(length(var.vpc_default_route_table_id)) == 1 ? length(var.availability_zones) : 0}"
count = "${signum(length(var.vpc_default_route_table_id)) == 1 ? length(var.availability_zones) : 0}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${var.vpc_default_route_table_id}"
}
}
4 changes: 4 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ variable "vpc_id" {
default = ""
}

variable "cidr_block" {
default = ""
}

variable "availability_zones" {
type = "list"
}
Expand Down

0 comments on commit a52f912

Please sign in to comment.