Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

interpolation in count on module output in next module gives strconv.ParseInt #7260

Closed
ddegoede opened this issue Jun 21, 2016 · 6 comments
Closed

Comments

@ddegoede
Copy link
Contributor

Terraform Version

Terraform v0.7.0-rc2 (46a0709)

Affected Resource(s)

resource "cloudstack_ipaddress"
resource "template_file"

So it looks like it might be a core issue as it occurs on multiple resources

Terraform Configuration Files

The plan

variable "vpcs" { 
    type = "map"
    default {
        name         = "VPC"
        cidr         = "10.0.0.0/23"
        network_name = "APP,DB"
        network_cidr = "10.0.0.0/24,10.0.1.0/24"
    }
}


module "vpc" {
    source        = "./VPC"
    vpcs          = "${var.vpcs}"
}

module "bastion_ip" {
    source        = "./PUBLICIP"
    vpc_id        = "${module.vpc.vpc_id}"
}

module "privgw" {
    source        = "./PRIVGW"
    vpc_name      = "${module.vpc.vpc_name}"
}

VPC module

# Part to create the VPC and create manageable public IP address.
variable "vpcs" { 
    type = "map"
    default {
        name         = ""
        cidr         = ""
        network_name = ""
        network_cidr = ""
    }
}

# Configure the CloudStack Provider
provider "cloudstack" {
    api_url    = "https://beta-nl2.test.local"
    api_key    = "xxx-xxxx-xxxxx-xxxx"
    secret_key = "xxx-xxxx-xxxxx-xxxx"
}

# Creating the VPC's
resource "cloudstack_vpc" "vpcs" {
    lifecycle { 
        create_before_destroy = true 
    }

    count           = "${length(split(",",var.vpcs["name"]))}"
    name            = "${element(split(",",var.vpcs["name"]),count.index)}"
    cidr            = "${element(split(",",var.vpcs["cidr"]),count.index)}"
    network_domain  = "test.local"
    vpc_offering    = "MCC-KVM-VPC-SBP1"
    zone            = "BETA-NL2"
}

resource "cloudstack_network_acl" "acls" {
    count    = "${length(split(",",var.vpcs["network_name"])) * length(split(",",var.vpcs["name"]))}"
    name     = "${element(split(",",var.vpcs["network_name"]),count.index)}-ACL"
    vpc_id   = "${element(cloudstack_vpc.vpcs.*.id, count.index)}"
}

resource "cloudstack_network" "networks" {
    count               = "${length(split(",",var.vpcs["network_name"])) * length(split(",",var.vpcs["name"]))}"
    name                = "${element(split(",",var.vpcs["network_name"]),count.index)}"
    cidr                = "${element(split(",",var.vpcs["network_cidr"]),count.index)}"
    network_offering    = "MCC-VPC-NoLB"
    zone                = "BETA-NL2"
    vpc_id              = "${element(cloudstack_vpc.vpcs.*.id,format("%d", count.index % length(split(",",var.vpcs["name"]))))}"
    acl_id              = "${element(cloudstack_network_acl.acls.*.id,count.index)}"
}

# Define all outputs here.

# VPC ID
output "vpc_id" {
    value = "${join(",",cloudstack_vpc.vpcs.*.id)}"
}

# VPC name
output "vpc_name" {
    value = "${join(",",cloudstack_vpc.vpcs.*.name)}"
}

# VPC cidr
output "vpc_cidr" {
    value = "${join(",",cloudstack_vpc.vpcs.*.cidr)}"
}

# VPC source nat IP
output "vpc_source_nat_ip" {
    value = "${join(",",cloudstack_vpc.vpcs.*.source_nat_ip)}"
}

# Network ID
output "network_id" {
    value = "${join(",",cloudstack_network.networks.*.id)}"
}

# Network name
output "network_name" {
    value = "${join(",",cloudstack_network.networks.*.name)}"
}

# Network Network cidr
output "network_cidr" {
    value = "${join(",",cloudstack_network.networks.*.cidr)}"
}

# ACL ID
output "acl_id" {
    value = "${join(",",cloudstack_network_acl.acls.*.id)}"
}

# ACL name
output "acl_name" {
    value = "${join(",",cloudstack_network_acl.acls.*.name)}"
}

PUBLICIP module

# all variables required for using this module
variable "vpc_id" {
    type = "string"
    description = "Comma separated list of VPC IDs, for every ID a public IP will be aquired"
}

# Configure the CloudStack Provider
provider "cloudstack" {
    api_url    = "https://beta-nl2.test.local"
    api_key    = "xxx-xxxx-xxxxx-xxxx"
    secret_key = "xxx-xxxx-xxxxx-xxxx"
}

resource "cloudstack_ipaddress" "publicips" {
    lifecycle { 
        create_before_destroy = true 
    }

    count           = "${length(split(",",var.vpc_id))}"
    vpc             = "${element(split(",",var.vpc_id) ,count.index)}"
}

# VPC public IP ids
output "ipaddress_id" {
    value = "${join(",",cloudstack_ipaddress.publicips.*.id)}"
}

PRIVGW module

# all variables required for using this module
variable "vpc_name" {
    type = "string"
    description = "Comma separated list of VPC names, for every name a private gw will be created"
}

resource "template_file" "configure_intervpc_routing" {
    count      = "${length(split(",",var.vpc_name))}"
    template   = "${file("${path.module}/configure_privategw.tpl")}"

    vars {
        vpc_name    = "${element(split(",",var.vpc_name),count.index)}"
    }

    provisioner "local-exec" {
        command = "${replace(self.rendered, "\n", "")}"
    }
}

Debug Output

https://gist.github.com/ddegoede/088604900d5fe338e02b6b929aa6ece9

Expected Behavior

Show the resource t o be build

Actual Behavior

Syntax error

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform plan

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

@jc-m
Copy link

jc-m commented Aug 20, 2016

This is still an issue in 0.7.1 - this is very limiting the usefulness of modules.

@jc-m
Copy link

jc-m commented Aug 22, 2016

I can confirm that the bug also manifest using terraform_remote_state. The specific use case is getting a list from a list from a remote state, and using the list and then using the list in a plan to compute a count to iterate over the list.

@marionettist
Copy link

I had a very similar problem and also thought it was an issue with the core terraform code. It turned out to be an access issue with the S3 bucket containing the tfstate remote file.
The access issue was not reported and instead, it looks like when the count variable was assigned, it tried to parse an empty string and failed.
So my experience of the issue is that the error reporting was poor, with the actual issue being hidden by the ParseInt error.
I stripped down all my code, even got rid of the code computing the count that caused the error and instead output'ted the list I wanted to do the count on. That's when I found out that terraform couldn't actually get the list because it could not access the bucket containing the terraform remote state.
Just sharing this in the hope it might help you narrow down the root cause of the error you're experiencing

@jc-m
Copy link

jc-m commented Aug 27, 2016

@marionettist - I don't think that this is my problem. I have two variables that I pass to my module. One is a count, the other is a list. I can access the list using count.index. However, I cannot use the list to calculate the count using length(list) - I tried calculating the count inside the module, or outside, with the exact same outcome.

@mitchellh
Copy link
Contributor

Dup of #3888! I've been tracking these down since there are so many so similar and while some are unique this appears to be fully related to #3888.

@ghost
Copy link

ghost commented Apr 22, 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 Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants