Skip to content

Instantly share code, notes, and snippets.

@limed
Created November 21, 2018 01:51
Show Gist options
  • Save limed/88dd47ff5d7659239d69090d520bf811 to your computer and use it in GitHub Desktop.
Save limed/88dd47ff5d7659239d69090d520bf811 to your computer and use it in GitHub Desktop.
subnet math
provider "aws" {
region = "us-west-2"
}
variable "az_number" {
default = {
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
}
}
variable "public_netnum_offset" {
default = 0
}
variable "private_netnum_offset" {
default = 100
}
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
variable "newbits" {
default = "8"
}
locals {
az = ["us-west-2a", "us-west-2b", "us-west-2c"]
}
data "aws_availability_zone" "az" {
count = "${length(local.az)}"
name = "${local.az[count.index]}"
}
resource "null_resource" "subnets" {
count = "${length(local.az)}"
triggers = {
public_subnet = "${cidrsubnet(var.vpc_cidr, var.newbits, var.az_number[data.aws_availability_zone.az.*.name_suffix[count.index]] + var.public_netnum_offset)}"
private_subnets = "${cidrsubnet(var.vpc_cidr, var.newbits, var.az_number[data.aws_availability_zone.az.*.name_suffix[count.index]] + var.private_netnum_offset)}"
}
}
output public_cidr_block {
value = "${null_resource.subnets.*.triggers.public_subnet}"
}
output private_cidr_block {
value = "${null_resource.subnets.*.triggers.private_subnets}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment