hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.88k stars 455 forks source link

Support for proper configuration language #3734

Open gytis-ivaskevicius opened 1 month ago

gytis-ivaskevicius commented 1 month ago

Description

HCL is verbose and not turing complete, general-purpose programming languages add additional complexity and verbosity (Maybe not as verbose as HCL)

I would love to have some proper configuration language for terrafrom. Possible candidates:

{ config, ... }:
let
  inherit (config) resource;
  compartment_id = "\${var.tenancy_ocid}";
  cidr_block = "10.10.0.0/24";

  # We can define functions to reduce verbosity
  mkSomeResource = {var1, var2}: {
    inherit var1 var2;
    abc = 123;
  };
in
{

  # using function from above
  resource.something = mkSomeResource {
    var1 = "123";
    var2 = "abc";
  };

  resource.oci_core_vcn.main = {
    inherit cidr_block compartment_id;
    dns_label = "main";
    display_name = "main";
  };

  resource.oci_core_internet_gateway.main = {
    inherit compartment_id;
    vcn_id = "\${oci_core_vcn.main.id}";
    display_name = "main";
  };

  resource.oci_core_subnet.public = {
    inherit cidr_block compartment_id;
    vcn_id = "\${oci_core_vcn.main.id}";
    display_name = "public";
    dns_label = "public";
    security_list_ids = [ "\${resource.oci_core_security_list.allow-all.id}" ];
  };

  resource.oci_core_default_route_table.ain = {
    manage_default_resource_id = "\${oci_core_vcn.main.default_route_table_id}";

    route_rules = {
      network_entity_id = "\${resource.oci_core_internet_gateway.main.id}";

      description = "internet gateway";
      destination = "0.0.0.0/0";
    };
  };

  resource.oci_core_default_security_list.default = {
    manage_default_resource_id = "\${oci_core_vcn.main.default_security_list_id}";

    egress_security_rules = [
      { protocol = "6"; destination = "0.0.0.0/0"; }
      { protocol = "17"; destination = "0.0.0.0/0"; }
    ];

    # We have `map` function to simplify definitions. Turing complete config languages are great
    ingress_security_rules = map
      (it: {
        inherit (it) description;
        tcp_options = {
          max = it.port;
          min = it.port;
        };
        protocol = "6";
        source = "0.0.0.0/0";
      }) [
      { description = "SSH"; port = 22; }
      { description = "HTTPS"; port = 443; }
      { description = "HTTP"; port = 80; }
    ];
  };

  resource.oci_core_security_list.allow-all = {
    inherit compartment_id;
    vcn_id = "\${oci_core_vcn.main.id}";

    egress_security_rules = {
      destination = "0.0.0.0/0";
      protocol = "all";
    };

    ingress_security_rules = [
      {
        protocol = "6";
        source = "0.0.0.0/0";
        description = "SSH";

        tcp_options = {
          max = 22;
          min = 22;
        };
      }
      {
        protocol = "all";
        source = "0.0.0.0/0";
      }
    ];
  };
}

References

No response

Help Wanted

Community Note