Syntax Features

Being largely built on HCL, Cloudscript supports all of HCL's normal capabilities, along with additional features to make the language easier to work with. Below is a list of the features that were added.

Simpler For-Loops

For-loops are made simpler with a more developer friendly style, making it easier for repetitive infrastructure declaration. Further, nested for-loops are also made simpler as shown below.

for i in range (1, 3) {
    network "subnet-{i}" {
        resource_type     = "aws_subnet"
        cidr_block        = "10.10.{i}.0/24"

--Equivalent to---

network "subnet-1" {
    resource_type     = "aws_subnet"
    cidr_block        = "10.10.1.0/24"
}

network "subnet-2" {
    resource_type     = "aws_subnet"
    cidr_block        = "10.10.2.0/24"
}

network "subnet-3" {
    resource_type     = "aws_subnet"
    cidr_block        = "10.10.3.0/24"
}

Custom Types

Custom types make repetitive infrastructure declaration and configuration simpler. After the custom type is declared it can be referenced within any other part of the Cloudscript code. Custom types are also useful for ensuring that necessary specifications are declared for the infrastructure components. For example, if a custom type specifies that a resource must have a field "name" and it must be a string, any resource of that type without a name that is a string would result in an error.

type DatabaseConfig {
    engine: "postgres" | "mysql" | "sqlite"
    version: string?
    storage: number = 20
}

resource "aws_db_instance" {
    type = DatabaseConfig
    engine = "postgres"
    version = "12.3"
}

---Equivalent to---

resource "aws_db_instance" {
  engine = "postgres"
  version = "12.3"
  storage = 20
}

Calc Fields

Calc fields are mostly to be used within custom types and for loops, but they make it simpler to specify a rule by which different resources should define variables.

type ComputedInstance {
    name: string,
    domain: string,
    fqdn: string = calc { "${name}.${domain}" }
}

resource "aws_instance" {
    type = ComputedInstance
    name = "aws-api"
    domain = "example.com"
}

resource "gcp_instance" {
    type = ComputedInstance
    name = "gcp-api"
    domain = "example.com"
}

---Equivalent to---

resource "aws_instance" {
  name = "aws-api"
  domain = "example.com"
  fqdn = "aws-api.example.com"
}

resource "gcp_instance" {
  name = "gcp-api"
  domain = "example.com"
  fqdn = "gcp-api.example.com"
}

File Path Specification

Instead of using multiline strings within the IaC code itself, the path to a file containing the text can be provided.

service "webapp" {
  provider = "aws"
    iam "eks_cluster" {
      name               = "eks_cluster"
      assume_role_policy = file("role.json")
      resource_type      = "aws_iam_role"
    }
}

Last updated