Skip to content
New issue

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

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

Already on GitHub? # to your account

panic in cidrsubnet function #18107

Closed
tonygyerr opened this issue May 23, 2018 · 10 comments
Closed

panic in cidrsubnet function #18107

tonygyerr opened this issue May 23, 2018 · 10 comments
Labels
bug config crash v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@tonygyerr
Copy link

tonygyerr commented May 23, 2018

Terraform Version

Terraform v0.11.7

  • provider.aws v1.19.0
  • provider.random v0.1.0
  • provider.template v0.1.1
...

Terraform Configuration Files

...

Debug Output

Crash Output

H:\My Documents\terraform\region\east\outage\env\nonprd\version\01\outage\terraf
orm>terraform plan
panic: runtime error: index out of range

goroutine 5497 [running]:
github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr.int
ToIP(0x14ed0fa0, 0x20, 0x14ed0fb0, 0x14ed0fa0, 0x20)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparen
tlymart/go-cidr/cidr/wrangling.go:27 +0xbb
github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr.ins
ertNumIntoIP(0x150a66a0, 0x4, 0x4, 0xe, 0x80000018, 0x15038a20, 0x55d497, 0x1edf
900)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparen
tlymart/go-cidr/cidr/wrangling.go:37 +0xa6
github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr.Sub
net(0x15038a20, 0x80000000, 0xe, 0x10, 0x10, 0x15038a20)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparen
tlymart/go-cidr/cidr/cidr.go:47 +0x122
github.com/hashicorp/terraform/config.interpolationFuncCidrSubnet.func1(0x150389
e0, 0x3, 0x3, 0x150389e0, 0x3, 0x3, 0x8)
        /opt/gopath/src/github.com/hashicorp/terraform/config/interpolate_funcs.
go:317 +0x129
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.(*evalCall).Eval(
0x14ed6720, 0x250d080, 0x14ed6408, 0x1563ce48, 0x0, 0x0, 0x1563ce48, 0x150389c0,
 0x1563ce48)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:283 +0x264
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.(*evalVisitor).vi
sit(0x1563ce40, 0x2510cc0, 0x1563cd20, 0x3, 0x150387c0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:215 +0xad
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.(*evalVisitor).(g
ithub.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.visit)-fm(0x2510cc
0, 0x1563cd20, 0x2510cc0, 0x1563ce10)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:175 +0x30
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil/ast.(*Call).Accep
t(0x1563cd20, 0x14ed6688, 0x40e9cc, 0x8)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/ast/call.go:20 +0xb6
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil/ast.(*Output).Acc
ept(0x15038600, 0x14ed6688, 0x0, 0x1563ce40)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/ast/output.go:20 +0x59
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.(*evalVisitor).Vi
sit(0x1563ce40, 0x2510d40, 0x15038600, 0x0, 0x0, 0x0, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:175 +0x6d
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.internalEval(0x25
10d40, 0x15038600, 0x14ed0ea0, 0x0, 0x8dd1e5, 0x14f9f3c0, 0x2510d40, 0x15038600)

        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:153 +0x5bc
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hil.Eval(0x2510d40, 0
x15038600, 0x14ed0ea0, 0x0, 0x2510d40, 0x15038600, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashico
rp/hil/eval.go:52 +0x35
github.com/hashicorp/terraform/config.(*RawConfig).Interpolate.func1(0x2510d40,
0x15038600, 0x2510d40, 0x15038600, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/config/raw_config.go:178
+0x30
github.com/hashicorp/terraform/config.(*interpolationWalker).Primitive(0x156c436
0, 0x1d16100, 0x14ed65d8, 0x94, 0x156c4360, 0x14fbb201)
        /opt/gopath/src/github.com/hashicorp/terraform/config/interpolate_walk.g
o:147 +0x100
github.com/hashicorp/terraform/vendor/github.com/mitchellh/reflectwalk.walkPrimi
tive(0x1d16100, 0x14ed65d8, 0x94, 0x2268100, 0x156c4360, 0x98, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchel
lh/reflectwalk/reflectwalk.go:251 +0x64
github.com/hashicorp/terraform/vendor/github.com/mitchellh/reflectwalk.walk(0x1d
16100, 0x14ed65d8, 0x94, 0x2268100, 0x156c4360, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchel
lh/reflectwalk/reflectwalk.go:179 +0x172
github.com/hashicorp/terraform/vendor/github.com/mitchellh/reflectwalk.walkMap(0
x1da6660, 0x14fcdf20, 0x15, 0x2268100, 0x156c4360, 0x40b400, 0x14cca000)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchel
lh/reflectwalk/reflectwalk.go:233 +0x210
github.com/hashicorp/terraform/vendor/github.com/mitchellh/reflectwalk.walk(0x1d
a6660, 0x14fcdf20, 0x15, 0x2268100, 0x156c4360, 0x1, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchel
lh/reflectwalk/reflectwalk.go:182 +0x2e6
github.com/hashicorp/terraform/vendor/github.com/mitchellh/reflectwalk.Walk(0x1d
a6660, 0x14fcdf20, 0x2268100, 0x156c4360, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchel
lh/reflectwalk/reflectwalk.go:92 +0xe2
github.com/hashicorp/terraform/config.(*RawConfig).interpolate(0x1544ad40, 0x14e
d6410, 0x1544ad64, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/config/raw_config.go:286
+0x125
github.com/hashicorp/terraform/config.(*RawConfig).Interpolate(0x1544ad40, 0x150
5edc0, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/config/raw_config.go:175
+0xb6
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).Interpolate(0x14f
09490, 0x1544ad40, 0x156125a0, 0x3caaee0, 0x4cb9c7, 0x14d36000)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_context_bu
iltin.go:249 +0xd5
github.com/hashicorp/terraform/terraform.(*EvalInterpolate).Eval(0x14e62b00, 0x2
51c660, 0x14f09490, 0x2, 0x2, 0x2364e60, 0x4)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_interpolat
e.go:19 +0x3a
github.com/hashicorp/terraform/terraform.EvalRaw(0x2506920, 0x14e62b00, 0x251c66
0, 0x14f09490, 0x0, 0x0, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x1
13
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0x14e62b10, 0x251c
660, 0x14f09490, 0x2, 0x2, 0x2364e60, 0x4)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_sequence.g
o:14 +0x5f
github.com/hashicorp/terraform/terraform.EvalRaw(0x2506ac0, 0x14e62b10, 0x251c66
0, 0x14f09490, 0x1dea3c0, 0x3b97c46, 0x1d16880, 0x14fd8748)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:53 +0x1
13
github.com/hashicorp/terraform/terraform.Eval(0x2506ac0, 0x14e62b10, 0x251c660,
0x14f09490, 0x14e62b10, 0x2506ac0, 0x14e62b10, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:34 +0x3
9
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x22e02c0, 0x14f456
38, 0x0, 0x0)
        /opt/gopath/src/github.com/hashicorp/terraform/terraform/graph.go:126 +0
xabd
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0x15612550, 0x22e02c0, 0
x14f45638, 0x14e52ee0)
        /opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:387 +0x300
created by github.com/hashicorp/terraform/dag.(*Walker).Update
        /opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:310 +0xfce



!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Expected Behavior

Actual Behavior

Steps to Reproduce

terraform -v
terraform providers
terraform plan

Additional Context

References

@ghost ghost added bug crash labels May 23, 2018
@jbardin jbardin added the config label May 23, 2018
@apparentlymart
Copy link
Contributor

Hi @tonygyerr! Thanks for reporting this.

Are you able to share the source code for the cidrsubnet call that triggered this? It looks like there's a bug in the underlying go-cidr library here, and so I'd like to understand better what input is triggering this to narrow down what the bug might be.

@tonygyerr
Copy link
Author

tonygyerr commented May 23, 2018

/*
 locals
*/

locals {
  base_cidr              = "${aws_vpc.api-east-vpc.cidr_block}"
  cidr_block_public_ecs  = "${cidrsubnet(var.base_cidr_pub, var.newbits_pub, var.netnum_public_ecs)}"
  cidr_block_private_ecs = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_ecs)}"
  cidr_block_private_db  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_db)}"
  cidr_block_private_lb  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_lb)}"
}

/*
 ecs subnet public
*/
resource "aws_subnet" "ecs_subnet_pub" {
  count                   = "${var.amount_public_ecs_subnets}"
  cidr_block              = "${cidrsubnet(local.cidr_block_public_ecs, ceil(log(var.max_subnets_pub, var.newbits_pub)), count.index)}"
  vpc_id                  = "${aws_vpc.api-east-vpc.id}"
  availability_zone       = "${data.aws_availability_zones.main.names[count.index]}"
  map_public_ip_on_launch = true
  depends_on              = ["aws_internet_gateway.api-nonprd-east-ig"]

  tags {
    Name = "${var.tags["Name"]}-ecs-pub-${count.index}-subnet"
  }
}

/*
 ecs subnet private
*/
resource "aws_subnet" "ecs_subnet_prv" {
  count                   = "${var.amount_private_ecs_subnets}"
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_ecs + count.index))}"
//cidr_block              = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, 2)), (6 + count.index))}"  
  vpc_id                  = "${aws_vpc.api-east-vpc.id}"
  availability_zone       = "${data.aws_availability_zones.main.names[count.index]}"
  map_public_ip_on_launch = false

  tags {
    Name = "${var.tags["Name"]}-ecs-prv-${count.index}-subnet"
  }
}

/*
 database subnet private
*/
resource "aws_subnet" "db_subnet_prv" {
  count                   = "${var.amount_private_db_subnets}"
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_db + count.index))}"
//cidr_block              = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, 2)), (9 + count.index))}" #base_cidr_prv 10.20.100.0/20
  vpc_id                  = "${aws_vpc.api-east-vpc.id}"
  availability_zone       = "${data.aws_availability_zones.main.names[count.index]}"
  map_public_ip_on_launch = false

  tags {
    Name = "${var.tags["Name"]}-db-prv-${count.index}-subnet"
  }
}

/*
 load balancer subnet private
*/
resource "aws_subnet" "lb_subnet_prv" {
  count                   = "${var.amount_private_lb_subnets}"
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
//cidr_block              = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, 2)), (12 + count.index))}"
  vpc_id                  = "${aws_vpc.api-east-vpc.id}"
  availability_zone       = "${data.aws_availability_zones.main.names[count.index]}"
  map_public_ip_on_launch = false

  tags {
    Name = "${var.tags["Name"]}-alb-prv-${count.index}-subnet"
  }
}

/*
  rds aurora api notification database subnet group
*/
resource "aws_db_subnet_group" "apidb_subnet_group" {
  name        = "apidb_subnet_group"
  description = "apidb subnet group"
  subnet_ids  = ["${element(aws_subnet.db_subnet_prv.*.id, count.index)}"]
}

/* 
variables
*/

variable "base_cidr_pub" {
  description = "cidr for vpc"
  type        = "string"
  default     = "10.20.100.0/20"
}

variable "newbits_pub" {
  description = "see https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet_iprange_newbits_netnum_"
  type        = "string"
  default     = 3
}

variable "newbits_prv" {
  description = "see https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet_iprange_newbits_netnum_"
  type        = "string"
  default     = 1
  //default     = 4
}

variable "max_subnets_prv" {
  description = "Maximum number of subnets which can be created for CIDR blocks calculation. Default to length of `names` argument"
  default     = "16"
}

variable "netnum_public_ecs" {
  type    = "string"
  default = "1"
}

variable "netnum_private_ecs" {
  type    = "string"
  default = "6"
}

variable "netnum_private_db" {
  type    = "string"
  default = "9"
}

variable "netnum_private_lb" {
  type    = "string"
  default = "12"
}

@apparentlymart
Copy link
Contributor

Thanks @tonygyerr!

Do you happen to know what values those various variables were set to when you ran Terraform and got this crash? Specifically, the variables that are used with cidrsubnet such as base_cidr_pub, newbits_pub, netnum_public_ecs, etc?

I'm trying to figure out which of the cidrsubnet calls here was the cause of the crash. If you know what variable values were used when you applied this then I can apply you config and debug it to see where it's crashing.

@tonygyerr
Copy link
Author

i updated the variables in the prior message.

@apparentlymart
Copy link
Contributor

Thanks!

I was able to reproduce the crash with the following modified configuration. Unfortunately, I've not yet been able to identify which of the cidrsubnet calls is failing here, because each time I attempted to simplify the configuration further, or to exclude some of the resources with -target, the crash went away. 😖

Unfortunately I need to break now, so I'll need to return to this another day and investigate further. Thanks again for reporting this!

provider "aws" {
  region = "us-east-1"
}

/*
 locals
*/

locals {
  base_cidr              = "${aws_vpc.aee-ece-outage-east-vpc.cidr_block}"
  cidr_block_public_ecs  = "${cidrsubnet(var.base_cidr_pub, var.newbits_pub, var.netnum_public_ecs)}"
  cidr_block_private_ecs = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_ecs)}"
  cidr_block_private_db  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_db)}"
  cidr_block_private_lb  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_lb)}"
}

/*
 ecs subnet public
*/
resource "aws_subnet" "ecs_subnet_pub" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_public_ecs, ceil(log(var.max_subnets_pub, var.newbits_pub)), count.index)}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true
}

/*
 ecs subnet private
*/
resource "aws_subnet" "ecs_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_ecs + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

/*
 database subnet private
*/
resource "aws_subnet" "db_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_db + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

/*
 load balancer subnet private
*/
resource "aws_subnet" "lb_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

/*
  rds aurora api notification database subnet group
*/
resource "aws_db_subnet_group" "apidb_subnet_group" {
  name        = "apidb_subnet_group"
  description = "apidb subnet group"
  subnet_ids  = ["${element(aws_subnet.db_subnet_prv.*.id, count.index)}"]
}

/* 
variables
*/

variable "base_cidr_pub" {
  description = "cidr for vpc"
  type        = "string"
  default     = "10.20.100.0/20"
}

variable "base_cidr_prv" {
  description = "cidr for vpc"
  type        = "string"
  default     = "10.20.100.0/20"
}

variable "newbits_pub" {
  type        = "string"
  default     = 3
}

variable "newbits_prv" {
  type        = "string"
  default     = 1
}

variable "max_subnets_prv" {
  description = "Maximum number of subnets which can be created for CIDR blocks calculation. Default to length of `names` argument"
  default     = "16"
}

variable "netnum_public_ecs" {
  type    = "string"
  default = "1"
}

variable "netnum_private_ecs" {
  type    = "string"
  default = "6"
}

variable "netnum_private_db" {
  type    = "string"
  default = "9"
}

variable "netnum_private_lb" {
  type    = "string"
  default = "12"
}

variable "max_subnets_pub" {
  type    = "string"
  default = "16"
}

@tonygyerr
Copy link
Author

tonygyerr commented May 23, 2018

i believe the issue is when utilizing the locals.tf

workaround while cidrsubnet bug for locals.tf is being worked on

  • create two variables (var.base_cidr_pub, var.base_cidr_prv)

--
/*
ecs subnet public
*/
resource "aws_subnet" "ecs_subnet_pub" {
count = "${var.amount_public_ecs_subnets}"
cidr_block = "${cidrsubnet(var.base_cidr_pub, ceil(log(var.max_subnets_pub, var.newbits_pub)), (var.netnum_public_ecs + count.index))}"
vpc_id = "${aws_vpc.api-east-vpc.id}"
availability_zone = "${data.aws_availability_zones.main.names[count.index]}"
map_public_ip_on_launch = true
depends_on = ["aws_internet_gateway.api-nonprd-east-ig"]

tags {
Name = "${var.tags["Name"]}-ecs-pub-${count.index}-subnet"
}
}

/*
ecs subnet private
*/
resource "aws_subnet" "ecs_subnet_prv" {
count = "${var.amount_private_ecs_subnets}"
cidr_block = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_ecs + count.index))}"
vpc_id = "${aws_vpc.api-east-vpc.id}"
availability_zone = "${data.aws_availability_zones.main.names[count.index]}"
map_public_ip_on_launch = false

tags {
Name = "${var.tags["Name"]}-ecs-prv-${count.index}-subnet"
}
}

/*
database subnet private
*/
resource "aws_subnet" "db_subnet_prv" {
count = "${var.amount_private_db_subnets}"
cidr_block = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_db + count.index))}" #base_cidr_prv 10.20.100.0/20
vpc_id = "${aws_vpc.api-east-vpc.id}"
availability_zone = "${data.aws_availability_zones.main.names[count.index]}"
map_public_ip_on_launch = false

tags {
Name = "${var.tags["Name"]}-db-prv-${count.index}-subnet"
}
}

/*
load balancer subnet private
*/
resource "aws_subnet" "lb_subnet_prv" {
count = "${var.amount_private_lb_subnets}"
cidr_block = "${cidrsubnet(var.base_cidr_prv, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
vpc_id = "${aws_vpc.api-east-vpc.id}"
availability_zone = "${data.aws_availability_zones.main.names[count.index]}"
map_public_ip_on_launch = false

tags {
Name = "${var.tags["Name"]}-alb-prv-${count.index}-subnet"
}
}

/*
rds aurora api notification database subnet group
/
resource "aws_db_subnet_group" "apidb_subnet_group" {
name = "apidb_subnet_group"
description = "apidb subnet group"
subnet_ids = ["${element(aws_subnet.db_subnet_prv.
.id, count.index)}"]
}

variable "base_cidr_pub" {
description = "cidr for vpc"
type = "string"
default = "10.20.100.0/23"
}

variable "base_cidr_prv" {
description = "cidr for vpc"
type = "string"
default = "10.20.100.0/24"
}

variable "newbits_pub" {
description = "see https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet_iprange_newbits_netnum_"
type = "string"
default = 3
}

variable "newbits_prv" {
description = "see https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet_iprange_newbits_netnum_"
type = "string"
default = 2
}

variable "netnum_public_ecs" {
type = "string"
default = "1"
}

variable "netnum_private_ecs" {
type = "string"
default = "6"
}

variable "netnum_private_db" {
type = "string"
default = "9"
}

variable "netnum_private_lb" {
type = "string"
default = "12"
}

@tonygyerr
Copy link
Author

tonygyerr commented May 24, 2018

@apparentlymart please update the base_cidr to read "${aws_vpc.api-east-vpc.cidr_block}" when you get a chance in your response above when you get a chance please.

locals {
base_cidr = "${aws_vpc.aee-ece-outage-east-vpc.cidr_block}"
cidr_block_public_ecs = "${cidrsubnet(var.base_cidr_pub, var.newbits_pub, var.netnum_public_ecs)}"
cidr_block_private_ecs = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_ecs)}"
cidr_block_private_db = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_db)}"
cidr_block_private_lb = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_lb)}"
}

@apparentlymart
Copy link
Contributor

Hi again! Sorry for the long silence here.

Since this issue is in the underlying CIDR utility library rather than Terraform itself, it's still present in the master branch after we've merged the configuration language improvements work for the forthcoming v0.12.0 release, but the new language interpreter runs functions in a recover context and so it can now report precisely where these errors are coming from.

Here's the configuration I used:

provider "aws" {
  region = "us-west-2"
}

locals {
  base_cidr              = "10.1.0.0/16"
  cidr_block_public_ecs  = "${cidrsubnet(var.base_cidr_pub, var.newbits_pub, var.netnum_public_ecs)}"
  cidr_block_private_ecs = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_ecs)}"
  cidr_block_private_db  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_db)}"
  cidr_block_private_lb  = "${cidrsubnet(var.base_cidr_prv, 4, var.netnum_private_lb)}"
}

resource "aws_subnet" "ecs_subnet_pub" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_public_ecs, ceil(log(var.max_subnets_pub, var.newbits_pub)), count.index)}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true
}

resource "aws_subnet" "ecs_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_ecs + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

resource "aws_subnet" "db_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_db + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

resource "aws_subnet" "lb_subnet_prv" {
  count                   = 1
  cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
  vpc_id                  = "vpc-1234"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = false
}

resource "aws_db_subnet_group" "apidb_subnet_group" {
  count       = 1
  name        = "apidb_subnet_group"
  description = "apidb subnet group"
  subnet_ids  = ["${element(aws_subnet.db_subnet_prv.*.id, count.index)}"]
}

variable "base_cidr_pub" {
  description = "cidr for vpc"
  type        = "string"
  default     = "10.20.100.0/20"
}

variable "base_cidr_prv" {
  description = "cidr for vpc"
  type        = "string"
  default     = "10.20.100.0/20"
}

variable "newbits_pub" {
  type    = "string"
  default = 3
}

variable "newbits_prv" {
  type    = "string"
  default = 1
}

variable "max_subnets_prv" {
  description = "Maximum number of subnets which can be created for CIDR blocks calculation. Default to length of `names` argument"
  default     = "16"
}

variable "netnum_public_ecs" {
  type    = "string"
  default = "1"
}

variable "netnum_private_ecs" {
  type    = "string"
  default = "6"
}

variable "netnum_private_db" {
  type    = "string"
  default = "9"
}

variable "netnum_private_lb" {
  type    = "string"
  default = "12"
}

variable "max_subnets_pub" {
  type    = "string"
  default = "16"
}

Here are the errors I got when applying it:

Error: Error in function call

  on cidrsubnet-crash.tf line 23, in resource "aws_subnet" "ecs_subnet_prv":
  23:   cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_ecs + count.index))}"
    |----------------
    | count.index is 0
    | local.cidr_block_private_ecs is "10.20.102.0/24"
    | var.max_subnets_prv is "16"
    | var.netnum_private_ecs is "6"
    | var.newbits_prv is "1"

Call to function "cidrsubnet" failed: panic in function implementation:
runtime error: makeslice: len out of range
goroutine 26 [running]:
runtime/debug.Stack(0x28def00, 0xc000c40401, 0x4c253b)
	/opt/go/src/runtime/debug/stack.go:24 +0xa7
github.com/zclconf/go-cty/cty/function.errorForPanic(0x2cf1920, 0x376a0f0,
0x376a0f0, 0x82)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
+0x26
github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc000c409e0,
0xc000c40a00)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:239
+0x85
panic(0x2cf1920, 0x376a0f0)
	/opt/go/src/runtime/panic.go:513 +0x1b9
math/big.nat.make(...)
	/opt/go/src/math/big/nat.go:64
math/big.nat.shl(0xc0002bacf0, 0x1, 0x5, 0xc0002bacf0, 0x1, 0x5,
0x8000000000000008, 0xc0005d1a40, 0x6, 0xc0005d1a40)
	/opt/go/src/math/big/nat.go:815 +0x215
math/big.(*Int).Lsh(0xc0005d1a40, 0xc0005d1a40, 0x8000000000000008,
0xc0005d1a20)
	/opt/go/src/math/big/int.go:931 +0x6f
github.com/apparentlymart/go-cidr/cidr.insertNumIntoIP(0xc00090cbec, 0x4, 0x4,
0x6, 0x8000000000000018, 0xc00090abd0, 0x10, 0xc00090cbe8)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/wrangling.go:35
+0x8a
github.com/apparentlymart/go-cidr/cidr.Subnet(0xc00090abd0,
0x8000000000000000, 0x6, 0x10, 0x10, 0xc00090abd0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go:47
+0xf1
github.com/hashicorp/terraform/lang/funcs.glob..func3(0xc000124cc0, 0x3, 0x3,
0x37854e0, 0xc00003c630, 0xc00003c630, 0x0, 0x0, 0xc00003c61f, 0xc000844090,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/funcs/cidr.go:107 +0x2eb
github.com/zclconf/go-cty/cty/function.Function.Call(0xc000278510,
0xc000124cc0, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:243
+0x2b5
github.com/hashicorp/hcl2/hcl/hclsyntax.(*FunctionCallExpr).Value(0xc00014ba40,
0xc0005d1600, 0x5d147c0, 0x1, 0x3, 0x0, 0x0, 0xc00002e8f0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression.go:385
+0x12a1
github.com/hashicorp/hcl2/hcl/hclsyntax.(*TemplateWrapExpr).Value(0xc000358690,
0xc0005d1600, 0xc00033eb00, 0xa, 0xc00002e990, 0xc00003c601, 0x37854e0,
0xc00003c630, 0x28dfb80)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_template.go:191
+0x41
github.com/hashicorp/hcl2/hcldec.(*AttrSpec).decode(0xc000844d80,
0xc000124c60, 0x0, 0x0, 0x0, 0xc0005d1600, 0x0, 0x0, 0x28dfb80, 0xc0009cf5f0,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:196
+0xa8
github.com/hashicorp/hcl2/hcldec.ObjectSpec.decode(0xc000844c30, 0xc000124c60,
0x0, 0x0, 0x0, 0xc0005d1600, 0x0, 0x28dfb80, 0x17762fa70012f590, 0x3158b60,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:78
+0x239
github.com/hashicorp/hcl2/hcldec.decode(0x3784120, 0xc000844bd0, 0x0, 0x0,
0x0, 0xc0005d1600, 0x3785260, 0xc000844c30, 0xc00097e100, 0xc0008454d0, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/decode.go:21
+0x118
github.com/hashicorp/hcl2/hcldec.Decode(0x3784120, 0xc000844bd0, 0x3785260,
0xc000844c30, 0xc0005d1600, 0x1, 0x0, 0x0, 0x0, 0x5d6cb40, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/public.go:15
+0x8a
github.com/hashicorp/terraform/lang.(*Scope).EvalBlock(0xc00097e190,
0x3784120, 0xc000844bd0, 0xc000680e70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x28, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/eval.go:56 +0x211
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateBlock(0xc00002f930,
0x3784120, 0xc000844bd0, 0xc000680e70, 0x0, 0x0, 0x37854e0, 0xc00003c61f,
0x33d04c0, 0xc000844090, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:277
+0x2ca
github.com/hashicorp/terraform/terraform.(*EvalDiff).Eval(0xc00072e840,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_diff.go:138
+0x18f
github.com/hashicorp/terraform/terraform.EvalRaw(0x376d980, 0xc00072e840,
0x3798600, 0xc00002f930, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc0005d0440,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x7fc613548df0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:20
+0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x376dca0, 0xc0005d0440,
0x3798600, 0xc00002f930, 0x2bef760, 0x5c23182, 0x28dfb80, 0xc0009cf310)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.Eval(0x376dca0, 0xc0005d0440,
0x3798600, 0xc00002f930, 0xc0005d0440, 0x376dca0, 0xc0005d0440, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x3321880,
0xc0009ce6b0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/graph.go:90 +0x10d1
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc0002d6d00,
0x3321880, 0xc0009ce6b0, 0xc0002bf300)
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:392 +0x373
created by github.com/hashicorp/terraform/dag.(*Walker).Update
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:314 +0x9d2
.


Error: Error in function call

  on cidrsubnet-crash.tf line 31, in resource "aws_subnet" "db_subnet_prv":
  31:   cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_db + count.index))}"
    |----------------
    | count.index is 0
    | local.cidr_block_private_ecs is "10.20.102.0/24"
    | var.max_subnets_prv is "16"
    | var.netnum_private_db is "9"
    | var.newbits_prv is "1"

Call to function "cidrsubnet" failed: panic in function implementation:
runtime error: makeslice: len out of range
goroutine 120 [running]:
runtime/debug.Stack(0x28def00, 0xc000c40401, 0x4c253b)
	/opt/go/src/runtime/debug/stack.go:24 +0xa7
github.com/zclconf/go-cty/cty/function.errorForPanic(0x2cf1920, 0x376a0f0,
0x376a0f0, 0x82)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
+0x26
github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc000c409e0,
0xc000c40a00)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:239
+0x85
panic(0x2cf1920, 0x376a0f0)
	/opt/go/src/runtime/panic.go:513 +0x1b9
math/big.nat.make(...)
	/opt/go/src/math/big/nat.go:64
math/big.nat.shl(0xc000a78540, 0x1, 0x5, 0xc000a78540, 0x1, 0x5,
0x8000000000000008, 0xc0002c42a0, 0x9, 0xc0002c42a0)
	/opt/go/src/math/big/nat.go:815 +0x215
math/big.(*Int).Lsh(0xc0002c42a0, 0xc0002c42a0, 0x8000000000000008,
0xc0002c4280)
	/opt/go/src/math/big/int.go:931 +0x6f
github.com/apparentlymart/go-cidr/cidr.insertNumIntoIP(0xc000b0995c, 0x4, 0x4,
0x9, 0x8000000000000018, 0xc000e17a40, 0x10, 0xc000b09958)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/wrangling.go:35
+0x8a
github.com/apparentlymart/go-cidr/cidr.Subnet(0xc000e17a40,
0x8000000000000000, 0x9, 0x10, 0x10, 0xc000e17a40)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go:47
+0xf1
github.com/hashicorp/terraform/lang/funcs.glob..func3(0xc0001d7620, 0x3, 0x3,
0x37854e0, 0xc00003c630, 0xc00003c630, 0x0, 0x0, 0xc00003c61f, 0xc000e451d0,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/funcs/cidr.go:107 +0x2eb
github.com/zclconf/go-cty/cty/function.Function.Call(0xc000278510,
0xc0001d7620, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:243
+0x2b5
github.com/hashicorp/hcl2/hcl/hclsyntax.(*FunctionCallExpr).Value(0xc00014bd10,
0xc00000dae0, 0x5d147c0, 0x1, 0x1, 0x0, 0x0, 0xc000d0f520, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression.go:385
+0x12a1
github.com/hashicorp/hcl2/hcl/hclsyntax.(*TemplateWrapExpr).Value(0xc000358af0,
0xc00000dae0, 0xc00033eb00, 0xa, 0xc000d0f5b0, 0xc00003c601, 0x37854e0,
0xc00003c630, 0x28dfb80)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_template.go:191
+0x41
github.com/hashicorp/hcl2/hcldec.(*AttrSpec).decode(0xc000e16060,
0xc0001d7560, 0x0, 0x0, 0x0, 0xc00000dae0, 0x0, 0x0, 0x28dfb80, 0xc000ab9220,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:196
+0xa8
github.com/hashicorp/hcl2/hcldec.ObjectSpec.decode(0xc000e45f50, 0xc0001d7560,
0x0, 0x0, 0x0, 0xc00000dae0, 0x0, 0x28dfb80, 0x17762fa70012f590, 0x3158b60,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:78
+0x239
github.com/hashicorp/hcl2/hcldec.decode(0x3784120, 0xc000e45ef0, 0x0, 0x0,
0x0, 0xc00000dae0, 0x3785260, 0xc000e45f50, 0xc0002dbe00, 0xc000e16990, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/decode.go:21
+0x118
github.com/hashicorp/hcl2/hcldec.Decode(0x3784120, 0xc000e45ef0, 0x3785260,
0xc000e45f50, 0xc00000dae0, 0x1, 0x0, 0x0, 0x0, 0x5d6cb40, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/public.go:15
+0x8a
github.com/hashicorp/terraform/lang.(*Scope).EvalBlock(0xc0002dbe00,
0x3784120, 0xc000e45ef0, 0xc000680e70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x28, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/eval.go:56 +0x211
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateBlock(0xc00002f930,
0x3784120, 0xc000e45ef0, 0xc000680e70, 0x0, 0x0, 0x37854e0, 0xc00003c61f,
0x33d04c0, 0xc000e451d0, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:277
+0x2ca
github.com/hashicorp/terraform/terraform.(*EvalDiff).Eval(0xc000122fc0,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_diff.go:138
+0x18f
github.com/hashicorp/terraform/terraform.EvalRaw(0x376d980, 0xc000122fc0,
0x3798600, 0xc00002f930, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc0008ab3c0,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x7fc613548df0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:20
+0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x376dca0, 0xc0008ab3c0,
0x3798600, 0xc00002f930, 0x2bef760, 0x5c23182, 0x28dfb80, 0xc000ab8e30)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.Eval(0x376dca0, 0xc0008ab3c0,
0x3798600, 0xc00002f930, 0xc0008ab3c0, 0x376dca0, 0xc0008ab3c0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x3321880,
0xc0005bff70, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/graph.go:90 +0x10d1
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc000795080,
0x3321880, 0xc0005bff70, 0xc0004d7340)
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:392 +0x373
created by github.com/hashicorp/terraform/dag.(*Walker).Update
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:314 +0x9d2
.


Error: Error in function call

  on cidrsubnet-crash.tf line 39, in resource "aws_subnet" "lb_subnet_prv":
  39:   cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
    |----------------
    | count.index is 0
    | local.cidr_block_private_ecs is "10.20.102.0/24"
    | var.max_subnets_prv is "16"
    | var.netnum_private_lb is "12"
    | var.newbits_prv is "1"

Call to function "cidrsubnet" failed: panic in function implementation:
runtime error: makeslice: len out of range
goroutine 121 [running]:
runtime/debug.Stack(0x28def00, 0xc000c40401, 0x4c253b)
	/opt/go/src/runtime/debug/stack.go:24 +0xa7
github.com/zclconf/go-cty/cty/function.errorForPanic(0x2cf1920, 0x376a0f0,
0x376a0f0, 0x82)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
+0x26
github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc000c409e0,
0xc000c40a00)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:239
+0x85
panic(0x2cf1920, 0x376a0f0)
	/opt/go/src/runtime/panic.go:513 +0x1b9
math/big.nat.make(...)
	/opt/go/src/math/big/nat.go:64
math/big.nat.shl(0xc000a788a0, 0x1, 0x5, 0xc000a788a0, 0x1, 0x5,
0x8000000000000008, 0xc0004b1a40, 0xc, 0xc0004b1a40)
	/opt/go/src/math/big/nat.go:815 +0x215
math/big.(*Int).Lsh(0xc0004b1a40, 0xc0004b1a40, 0x8000000000000008,
0xc0004b1a20)
	/opt/go/src/math/big/int.go:931 +0x6f
github.com/apparentlymart/go-cidr/cidr.insertNumIntoIP(0xc0007e2154, 0x4, 0x4,
0xc, 0x8000000000000018, 0xc000385b60, 0x10, 0xc0007e2150)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/wrangling.go:35
+0x8a
github.com/apparentlymart/go-cidr/cidr.Subnet(0xc000385b60,
0x8000000000000000, 0xc, 0x10, 0x10, 0xc000385b60)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go:47
+0xf1
github.com/hashicorp/terraform/lang/funcs.glob..func3(0xc00017a360, 0x3, 0x3,
0x37854e0, 0xc00003c630, 0xc00003c630, 0x0, 0x0, 0xc00003c61f, 0xc0005ee3c0,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/funcs/cidr.go:107 +0x2eb
github.com/zclconf/go-cty/cty/function.Function.Call(0xc000278510,
0xc00017a360, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/zclconf/go-cty/cty/function/function.go:243
+0x2b5
github.com/hashicorp/hcl2/hcl/hclsyntax.(*FunctionCallExpr).Value(0xc00021e000,
0xc0004b15e0, 0x5d147c0, 0x1, 0x3, 0x0, 0x0, 0xc000abfd40, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression.go:385
+0x12a1
github.com/hashicorp/hcl2/hcl/hclsyntax.(*TemplateWrapExpr).Value(0xc000358f50,
0xc0004b15e0, 0xc00033eb00, 0xa, 0xc000abfde0, 0xc00003c601, 0x37854e0,
0xc00003c630, 0x28dfb80)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_template.go:191
+0x41
github.com/hashicorp/hcl2/hcldec.(*AttrSpec).decode(0xc000384390,
0xc00017a2a0, 0x0, 0x0, 0x0, 0xc0004b15e0, 0x0, 0x0, 0x0, 0x0, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:196
+0xa8
github.com/hashicorp/hcl2/hcldec.ObjectSpec.decode(0xc000384090, 0xc00017a2a0,
0x0, 0x0, 0x0, 0xc0004b15e0, 0x0, 0x28dfb80, 0x17762fa70012f590, 0x3158b60,
...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/spec.go:78
+0x239
github.com/hashicorp/hcl2/hcldec.decode(0x3784120, 0xc000384030, 0x0, 0x0,
0x0, 0xc0004b15e0, 0x3785260, 0xc000384090, 0xc000338c00, 0xc0003849c0, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/decode.go:21
+0x118
github.com/hashicorp/hcl2/hcldec.Decode(0x3784120, 0xc000384030, 0x3785260,
0xc000384090, 0xc0004b15e0, 0x1, 0x0, 0x0, 0x0, 0x5d6cb40, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcldec/public.go:15
+0x8a
github.com/hashicorp/terraform/lang.(*Scope).EvalBlock(0xc000338cd0,
0x3784120, 0xc000384030, 0xc000680e70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x28, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/lang/eval.go:56 +0x211
github.com/hashicorp/terraform/terraform.(*BuiltinEvalContext).EvaluateBlock(0xc00002f930,
0x3784120, 0xc000384030, 0xc000680e70, 0x0, 0x0, 0x37854e0, 0xc00003c61f,
0x33d04c0, 0xc0005ee3c0, ...)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_context_builtin.go:277
+0x2ca
github.com/hashicorp/terraform/terraform.(*EvalDiff).Eval(0xc000123a40,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_diff.go:138
+0x18f
github.com/hashicorp/terraform/terraform.EvalRaw(0x376d980, 0xc000123a40,
0x3798600, 0xc00002f930, 0x0, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.(*EvalSequence).Eval(0xc0002cf980,
0x3798600, 0xc00002f930, 0x2, 0x2, 0x7fc613548df0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval_sequence.go:20
+0xfd
github.com/hashicorp/terraform/terraform.EvalRaw(0x376dca0, 0xc0002cf980,
0x3798600, 0xc00002f930, 0x2bef760, 0x5c23182, 0x28dfb80, 0xc000b205e0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:57 +0x167
github.com/hashicorp/terraform/terraform.Eval(0x376dca0, 0xc0002cf980,
0x3798600, 0xc00002f930, 0xc0002cf980, 0x376dca0, 0xc0002cf980, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/eval.go:35 +0x4d
github.com/hashicorp/terraform/terraform.(*Graph).walk.func1(0x3321880,
0xc000ab9670, 0x0, 0x0, 0x0)
	/opt/gopath/src/github.com/hashicorp/terraform/terraform/graph.go:90 +0x10d1
github.com/hashicorp/terraform/dag.(*Walker).walkVertex(0xc000795b00,
0x3321880, 0xc000ab9670, 0xc0004d7cc0)
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:392 +0x373
created by github.com/hashicorp/terraform/dag.(*Walker).Update
	/opt/gopath/src/github.com/hashicorp/terraform/dag/walk.go:314 +0x9d2
.

We'll return to this in a little while and use this information to construct a more minimal reproduction case and then find a fix for it.

One thing I noticed though from quick reading of these messages is that var.newbits_prv is 1 in all of these cases, and one bit isn't enough for network numbers starting at 12. So probably this is a missing range check in the underlying function, where it should be returning an error saying that the given network number doesn't fit inside the 1-bit address extension.

@apparentlymart apparentlymart changed the title terraform crash panic in cidrsubnet function Oct 31, 2018
@hashibot hashibot added v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases labels Aug 22, 2019
@alisdair
Copy link
Contributor

This panic seems to be fixed in the 0.13 beta release (perhaps via apparentlymart/go-cidr#16) and now results in a more useful error:

Error: Error in function call

  on main.tf line 39, in resource "aws_subnet" "lb_subnet_prv":
  39:   cidr_block              = "${cidrsubnet(local.cidr_block_private_ecs, ceil(log(var.max_subnets_prv, var.newbits_prv)), (var.netnum_private_lb + count.index))}"
    |----------------
    | count.index is 0
    | local.cidr_block_private_ecs is "10.20.102.0/24"
    | var.max_subnets_prv is "16"
    | var.netnum_private_lb is "12"
    | var.newbits_prv is "1"

Call to function "cidrsubnet" failed: value must be a whole number, between
-9223372036854775808 and 9223372036854775807.

I think the error here is that log(var.max_subnets_prv, var.newbits_prv) is log(16, 1), and log with base 1 results in infinity in our implementation (due to a divide by zero from log(1)).

@ghost
Copy link

ghost commented Jul 13, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 13, 2020
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
bug config crash v0.11 Issues (primarily bugs) reported against v0.11 releases v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

5 participants