Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active September 8, 2022 17:42
Show Gist options
  • Save mtilson/5c7b8600ab5fea1fb7a2d56581c00c85 to your computer and use it in GitHub Desktop.
Save mtilson/5c7b8600ab5fea1fb7a2d56581c00c85 to your computer and use it in GitHub Desktop.
how to deal with CIDRs in different AWS availability zones with terraform 'cidrsubnet' function [aws] [terraform]
$ terraform init
Initializing modules...
- eun1 in module
- euw2 in module
- use1 in module
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.27.0"...
- Installing hashicorp/aws v4.27.0...
- Installed hashicorp/aws v4.27.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform plan
module.use1.data.aws_availability_zones.available: Reading...
module.use1.data.aws_availability_zones.available: Read complete after 0s [id=us-east-1]
module.eun1.data.aws_availability_zones.available: Reading...
module.euw2.data.aws_availability_zones.available: Reading...
module.eun1.data.aws_availability_zones.available: Read complete after 0s [id=eu-north-1]
module.euw2.data.aws_availability_zones.available: Read complete after 0s [id=eu-west-2]
Changes to Outputs:
+ eun1_cidrsubnet_call = [
+ "cidrsubnet(10.36.0.0/16, 8, 1)",
+ "cidrsubnet(10.36.0.0/16, 8, 2)",
+ "cidrsubnet(10.36.0.0/16, 8, 3)",
]
+ eun1_cidrsubnet_result = [
+ "10.36.1.0/24",
+ "10.36.2.0/24",
+ "10.36.3.0/24",
]
+ eun1_netnum = [
+ 1,
+ 2,
+ 3,
]
+ eun1_zone_id_lengths = [
+ 8,
+ 8,
+ 8,
]
+ eun1_zone_ids = [
+ "eun1-az1",
+ "eun1-az2",
+ "eun1-az3",
]
+ euw2_cidrsubnet_call = [
+ "cidrsubnet(10.52.0.0/16, 8, 1)",
+ "cidrsubnet(10.52.0.0/16, 8, 2)",
+ "cidrsubnet(10.52.0.0/16, 8, 3)",
]
+ euw2_cidrsubnet_result = [
+ "10.52.1.0/24",
+ "10.52.2.0/24",
+ "10.52.3.0/24",
]
+ euw2_netnum = [
+ 1,
+ 2,
+ 3,
]
+ euw2_zone_id_lengths = [
+ 8,
+ 8,
+ 8,
]
+ euw2_zone_ids = [
+ "euw2-az1",
+ "euw2-az2",
+ "euw2-az3",
]
+ use1_cidrsubnet_call = [
+ "cidrsubnet(10.6.0.0/16, 8, 1)",
+ "cidrsubnet(10.6.0.0/16, 8, 2)",
+ "cidrsubnet(10.6.0.0/16, 8, 3)",
+ "cidrsubnet(10.6.0.0/16, 8, 4)",
+ "cidrsubnet(10.6.0.0/16, 8, 5)",
+ "cidrsubnet(10.6.0.0/16, 8, 6)",
]
+ use1_cidrsubnet_result = [
+ "10.6.1.0/24",
+ "10.6.2.0/24",
+ "10.6.3.0/24",
+ "10.6.4.0/24",
+ "10.6.5.0/24",
+ "10.6.6.0/24",
]
+ use1_netnum = [
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
]
+ use1_zone_id_lengths = [
+ 8,
+ 8,
+ 8,
+ 8,
+ 8,
+ 8,
]
+ use1_zone_ids = [
+ "use1-az1",
+ "use1-az2",
+ "use1-az3",
+ "use1-az4",
+ "use1-az5",
+ "use1-az6",
]
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
.
├── main.tf
├── module
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── outputs.tf
└── providers.tf
module "eun1" {
source = "./module"
providers = {
aws = aws.eun1
}
cidr = "10.36.0.0/16"
}
module "euw2" {
source = "./module"
providers = {
aws = aws.euw2
}
cidr = "10.52.0.0/16"
}
module "use1" {
source = "./module"
providers = {
aws = aws.use1
}
cidr = "10.6.0.0/16"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
data "aws_availability_zones" "available" {
state = "available"
}
output "zone_id_lengths" {
value = [for zone_id in sort(data.aws_availability_zones.available.zone_ids) : length(zone_id)]
}
output "zone_ids" {
value = [for zone_id in sort(data.aws_availability_zones.available.zone_ids) : zone_id]
}
output "netnum" {
value = [for zone_id in sort(data.aws_availability_zones.available.zone_ids) : tonumber(substr(zone_id, length(zone_id) - 1, 1))]
}
output "cidrsubnet_call" {
value = [for zone_id in sort(data.aws_availability_zones.available.zone_ids) : format("cidrsubnet(%s, 8, %d)", var.cidr, tonumber(substr(zone_id, length(zone_id) - 1, 1)) ) ]
}
output "cidrsubnet_result" {
value = [for zone_id in sort(data.aws_availability_zones.available.zone_ids) : cidrsubnet(var.cidr, 8, tonumber(substr(zone_id, length(zone_id) - 1, 1)))]
}
variable "cidr" {
type = string
}
output "eun1_zone_ids" {
value = module.eun1.zone_ids
}
output "eun1_zone_id_lengths" {
value = module.eun1.zone_id_lengths
}
output "eun1_netnum" {
value = module.eun1.netnum
}
output "eun1_cidrsubnet_call" {
value = module.eun1.cidrsubnet_call
}
output "eun1_cidrsubnet_result" {
value = module.eun1.cidrsubnet_result
}
output "euw2_zone_ids" {
value = module.euw2.zone_ids
}
output "euw2_zone_id_lengths" {
value = module.euw2.zone_id_lengths
}
output "euw2_netnum" {
value = module.euw2.netnum
}
output "euw2_cidrsubnet_call" {
value = module.euw2.cidrsubnet_call
}
output "euw2_cidrsubnet_result" {
value = module.euw2.cidrsubnet_result
}
output "use1_zone_ids" {
value = module.use1.zone_ids
}
output "use1_zone_id_lengths" {
value = module.use1.zone_id_lengths
}
output "use1_netnum" {
value = module.use1.netnum
}
output "use1_cidrsubnet_call" {
value = module.use1.cidrsubnet_call
}
output "use1_cidrsubnet_result" {
value = module.use1.cidrsubnet_result
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.27.0"
}
}
required_version = "~> 1.2.0"
}
provider "aws" {
alias = "use1"
region = "us-east-1"
}
provider "aws" {
alias = "eun1"
region = "eu-north-1"
}
provider "aws" {
alias = "euw2"
region = "eu-west-2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment