terraform-ibm-modules / terraform-ibm-toolkit-vpc-subnets

Module to provision subnets in a VPC instance
Apache License 2.0
0 stars 8 forks source link

Variable values not transferred to the variables.tf file #66

Closed thomassuedbroecker closed 2 years ago

thomassuedbroecker commented 2 years ago

Variables are not transferred to the variables.tf file I want to create 1 subnet and not 3.

Here are the steps to reproduce:

Step 1: Create own BOM file

kind: BillOfMaterial
metadata:
  name: my-bom
spec:
  modules:
    # Virtual Private Cloud
    - name: ibm-vpc
      variables:
      - name: ibm-vpc_name
        value: "tsued-gitops-sample"
    - name: ibm-vpc-subnets
      variables:
      - name: ibm-vpc-subnets__count
        value: 1

Step 2: Use iascable to build the needed files

iascable build -i my-bom.yaml

Step 3: Verify created bom.yaml file

It contains the wrong number or better doesn't use the right value for the variable.

apiVersion: cloud.ibm.com/v1alpha1
kind: BillOfMaterial
metadata:
  name: my-bom
spec:
  modules:
    - name: ibm-vpc
      alias: ibm-vpc
      version: v1.16.0
      variables:
        - name: ibm-vpc_name
          value: tsued-gitops-sample
    - name: ibm-vpc-subnets
      alias: ibm-vpc-subnets
      version: v1.13.2
      variables:
        - name: ibm-vpc-subnets__count
          value: 1
    - name: ibm-resource-group
      alias: resource_group
      version: v3.2.16
  variables:
    - name: region
      type: string
      description: The IBM Cloud region where the cluster will be/has been installed.
    - name: ibmcloud_api_key
      type: string
    - name: ibm-vpc-subnets__count
      type: number
      description: The number of subnets that should be provisioned
      defaultValue: 3
    - name: resource_group_name
      type: string
      description: The name of the resource group

Step 4: Verify created variables.tf file

It contains the wrong number or better doesn't use the right value for the variable.

variable "ibm-vpc-subnets__count" {
  type = number
  description = "The number of subnets that should be provisioned"
  default = 3
}

Step 5: Verify the create evenvironment

The result is 3 subnets will be created and not 1.

timroster commented 2 years ago

@thomassuedbroecker the bom variables scoped at the module level should use the input names specified in the respective module.yaml file - not the fully expanded values expected in the final variables.tf file.

Try using something like:

kind: BillOfMaterial
metadata:
  name: my-bom
spec:
  modules:
    # Virtual Private Cloud
    - name: ibm-vpc
      variables:
      - name: name
        value: "tsued-gitops-sample"
    - name: ibm-vpc-subnets
      variables:
      - name: _count
        value: 1

iascable will build this out to this BOM:

 kind: BillOfMaterial
metadata:
  name: my-bom
spec:
  modules:
    - name: ibm-vpc
      alias: ibm-vpc
      version: v1.16.0
      variables:
        - name: name
          value: tsued-gitops-sample
    - name: ibm-vpc-subnets
      alias: ibm-vpc-subnets
      version: v1.13.2
      variables:
        - name: _count
          value: 1
    - name: ibm-resource-group
      alias: resource_group
      version: v3.2.16
  variables:
    - name: region
      type: string
      description: The IBM Cloud region where the cluster will be/has been installed.
    - name: ibmcloud_api_key
      type: string
    - name: ibm-vpc-subnets__count
      type: number
      description: The number of subnets that should be provisioned
      defaultValue: 1
    - name: resource_group_name
      type: string
      description: The name of the resource group

And you would find that in the output/my-bom/terraform/variables.tf this:

...
variable "ibm-vpc-subnets__count" {
  type = number
  description = "The number of subnets that should be provisioned"
  default = 1
}
...
triceam commented 2 years ago

This is not an issue with the vpc subnets module. This is incorrectly structured BOM yaml. if you want to specify values in the BOM for a module instance, then you need to add those variables within the module scope, and do not prefix the variables with the module name/alias. The variables block at the bottom can be used to set global-scope variables.

See an example here: https://github.com/IBM/automation-ibmcloud-infra-zos-dev/blob/main/110-ibm-zdev-network-vpc/bom.yaml#L157

You want something more like this:

apiVersion: cloud.ibm.com/v1alpha1
kind: BillOfMaterial
metadata:
  name: my-bom
spec:
  modules:
    - name: ibm-vpc
      alias: ibm-vpc
      version: v1.16.0
      variables:
        - name: name
          value: tsued-gitops-sample
    - name: ibm-vpc-subnets
      alias: ibm-vpc-subnets
      version: v1.13.2
      variables:
        - name: _count
          value: 1
    - name: ibm-resource-group
      alias: resource_group
      version: v3.2.16
  variables:
    - name: region
      type: string
      description: The IBM Cloud region where the cluster will be/has been installed.
    - name: ibmcloud_api_key
      type: string
    - name: resource_group_name
      type: string
      description: The name of the resource group