terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Terraform module input variables & typescript

Open harjis opened this issue 3 years ago • 2 comments

Hi! We would like to define Terraform Modules input variables as objects like this:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.2.0"
    }
  }
}

provider "azurerm" {
  features {}
}

variable "storage_account" {
  description = "Storage Account options"
  type = object({
    replication_type: string,
    account_tier: string
  })
}


resource "azurerm_storage_account" "storage-account" {
  account_replication_type = var.storage_account.replication_type
  account_tier             = var.storage_account.account_tier
  location                 = "westeurope"
  name                     = "st-name"
  resource_group_name      = "rg-test"

  allow_nested_items_to_be_public = false
}

The problem we have is that when we use these Modules in CDKTF the generated Typescript types for storage_account is any:

export interface StorageAccountModuleOptions {
  /**
   * Storage Account options
   */
  readonly storageAccount: any;
}

Do you know if it is possible to type storageAccount more strongly so that the resulting types would be something like:

interface StorageAccountModuleStorageAccount {
  replicationType: string;
  accountTier: string;
}
export interface StorageAccountModuleOptions {
  /**
   * Storage Account options
   */
  readonly storageAccount: StorageAccountModuleStorageAccount;
}

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

References

harjis avatar Jul 14 '22 07:07 harjis

Hey, great point! We have no good way of inferring the outputs (@ansgarm brought up an idea with evaluating the TF like terraform validate does and using that type information to get the output values, but reading this issue I noticed we have no problem with reading the inputs and parsing them into a format that we can use to generate bindings from, so it's totally possible and we should do it.

DanielMSchmidt avatar Jul 15 '22 11:07 DanielMSchmidt

We could use the parsing of the variable names as well for convert: #1891

DanielMSchmidt avatar Jul 15 '22 11:07 DanielMSchmidt