Carina

Syntax

File structure, provider blocks, resource blocks, let bindings, and comments in the Carina DSL.

Carina configuration files use the .crn extension. A file consists of a sequence of top-level statements that declare infrastructure resources and their relationships.

File Structure

A .crn file contains zero or more top-level statements in any order:

# Provider configuration
provider awscc {
  source  = 'carina-rs/awscc'
  version = '0.7.1'
  region  = awscc.Region.ap_northeast_1
}

# Backend configuration (optional)
backend s3 {
  bucket = 'my-state-bucket'
  key    = 'infra/carina.state.json'
  region = 'ap-northeast-1'
}

# Resource declarations
let vpc = awscc.ec2.Vpc {
  cidr_block = '10.0.0.0/16'

  tags = {
    Name = 'main'
  }
}

awscc.ec2.InternetGateway {
  tags = {
    Name = 'main'
  }
}

Top-Level Construct Shapes

Carina’s top-level constructs come in two shapes, chosen by two independent questions:

  1. Does the block’s body schema depend on a kind label? The attributes valid inside provider awscc { ... } differ from those inside provider aws { ... }, and similarly for backend kinds. When the schema varies by kind, the construct carries a kind label (provider <kind>, backend <kind>).
  2. Does the construct produce a value that other code references by name? When multiple named instances are useful, the construct is written as an expression on the right-hand side of a let binding, and the binding name on the left is how the value is referenced later.

The two questions are orthogonal:

kind label neededkind label not needed
singleton / not named-referencedprovider awscc { ... }, backend s3 { ... }—
named referencelet us = provider awscc { ... }let orgs = upstream_state { ... }
  • provider / backend carry kind labels because the body schema is kind-specific. A bare provider <kind> { ... } block declares the kind’s default instance; wrapping the same expression in let <name> = provider <kind> { ... } declares a named instance. See Named provider instances below.
  • upstream_state has no kind label because there is only one kind of upstream (a sibling directory); it is an expression because projects typically reference several named upstreams.

When reading an unfamiliar top-level construct, work out which row and column of the table it sits in — that tells you whether to expect a kind label and whether to expect a let binding on the left.

Statements

The following statements are valid at the top level of a .crn file:

StatementPurpose
providerConfigure a cloud provider
backendConfigure remote state storage
letBind a name to a resource or value
Anonymous resourceDeclare a resource without a binding
import (state)Import an existing cloud resource into state
removedRemove a resource from state without deleting it
movedRename a resource in state
forIterate to create multiple resources
ifConditionally create resources
fnDefine a reusable function
requireAssert a condition with an error message
argumentsDeclare module input parameters
attributesDefine module output values (for callers who bind the module with let)
exportsDefine values published to upstream-state consumers
use (in let RHS)Load a module from a directory (let m = use { source = '...' })
Module callInstantiate a loaded module (m { ... } or let x = m { ... })
read (in let RHS)Query an existing resource as a data source
upstream_state (in let RHS)Reference another Carina project’s exports
wait (in let RHS)Block downstream resources until a resource reaches a condition

Provider Block

A configuration that declares resources must configure a provider for each provider kind it uses. The provider block specifies which provider plugin to load and its settings.

provider awscc {
  source  = 'carina-rs/awscc'
  version = '0.7.1'
  region  = awscc.Region.ap_northeast_1
}

source is required; carina init uses it to install the provider plugin. The block’s name must match the name the provider itself reports (aws for carina-rs/aws, awscc for carina-rs/awscc); otherwise loading fails with configured provider name '<name>' does not match artifact-reported name '<reported>'. It is a registry address (carina-rs/awscc, or host/namespace/name), a GitHub repository (github.com/owner/repo), or a local file:// path to a .wasm component. source, version, and revision must be string literals. version (a version constraint) or revision (a branch or tag; a commit SHA only with a github.com/... source) pins which build is installed. The two are mutually exclusive.

Multiple providers can be configured in the same directory, one default provider <kind> block per kind across all of the directory’s .crn files. A second block of the same kind, even in another file, is a validation error (duplicate provider kind); use a named provider instance for additional configurations of the same kind:

provider awscc {
  source  = 'carina-rs/awscc'
  version = '0.7.1'
  region  = awscc.Region.ap_northeast_1
}

provider aws {
  source  = 'carina-rs/aws'
  version = '0.7.1'
  region  = aws.Region.ap_northeast_1
}

Named Provider Instances

A provider <kind> { ... } block declares the kind’s default instance — the one resources reach for when they say nothing about routing. To declare additional instances of the same kind (for example, an awscc provider pinned to a different region), wrap the same expression in a let binding:

provider awscc {
  source  = 'carina-rs/awscc'
  version = '0.7.1'
  region  = awscc.Region.ap_northeast_1
}

let us = provider awscc {
  region = awscc.Region.us_east_1
}

Only the kind’s default instance carries source / version / revision. Repeating those on a named instance is rejected by the parser: they describe how to load the provider plugin, which is a property of the kind, not of an individual instance.

Resources route by attaching directives { provider = <binding> }:

awscc.s3.Bucket {
  bucket_name = 'tokyo-assets'
  # no directive -> kind's default instance (ap-northeast-1)
}

awscc.cloudfront.Distribution {
  # ...

  directives {
    provider = us   # routes to the `us` instance (us-east-1)
  }
}

Omitting directives.provider keeps the resource on the kind’s default instance. The default instance is identified by the absence of a binding name; it is not surfaced as a completion candidate at the value position of directives { provider = | } for the same reason.

State files persist the routing decision on each resource’s identifier (the provider_instance field on ResourceId / Directives). Existing state files written before named-instance support continue to load: the field defaults to null and serialises back out only when non-null.

Resource Blocks

Resources represent cloud infrastructure objects. A resource block specifies the provider, service, and resource type using dot notation, followed by a block of attributes.

Anonymous Resources

When you do not need to reference a resource elsewhere, declare it without a binding:

awscc.ec2.Vpc {
  cidr_block = '10.0.0.0/16'

  tags = {
    Name = 'main'
  }
}

If the schema declares identity paths, Carina generates the resource’s state identity from only those values; paths may name top-level attributes or nested struct fields whose leaf is a scalar or enum. Other create-only values can force replacement without changing the address. Written values are schema-canonicalized. References and composite expressions over references and literals are hashed by their deterministic authored spelling, so their addresses stay stable after apply-time resolution; generated names hash a separately typed prefix. A missing required identity value or a written expression containing an unknown or unavailable leaf is an error: write the value or bind the resource with let. An omitted optional identity_when_present() value contributes an absence marker. A schema with no declared identity falls back to configured top-level create-only attributes and, when none are configured, to the resource’s other attributes. Multiple resources using that attribute-derived fallback are rejected as ambiguous. Equal declared identity values, including equal expression spellings plus equal written values, are also rejected.

Named Resources (Let Bindings)

Use let to bind a name to a resource so you can reference its attributes:

let vpc = awscc.ec2.Vpc {
  cidr_block = '10.0.0.0/16'

  tags = {
    Name = 'main'
  }
}

let subnet = awscc.ec2.Subnet {
  vpc_id            = vpc.vpc_id
  cidr_block        = '10.0.1.0/24'
  availability_zone = 'ap-northeast-1a'
}

The let keyword is also used to bind non-resource values:

let cidr = '10.0.0.0/16'
let environments = ['dev', 'stg', 'prod']
let config = {
  dev = '10.0.0.0/16'
  stg = '10.1.0.0/16'
}

Discard Pattern

Use let _ = when you want to evaluate an expression but do not need to reference the result:

let _ = awscc.ec2.VpcGatewayAttachment {
  vpc_id              = vpc.vpc_id
  internet_gateway_id = igw.internet_gateway_id
}

Data Sources (Read Resources)

Use the read keyword to query existing cloud resources without managing them:

let identity = read aws.sts.CallerIdentity {}

The returned value can be referenced like any other bound resource:

let identity = read aws.sts.CallerIdentity {}

awscc.ec2.IpamPool {
  address_family = ipv4
  ipam_scope_id = 'ipam-scope-0123456789abcdef0'
  source_resource = {
    resource_owner = identity.account_id
    resource_id = 'vpc-0123456789abcdef0'
    resource_region = 'ap-northeast-1'
    resource_type = 'vpc'
  }
}

Directives

A directives block inside a resource carries Carina-side instructions (depends_on, create_before_destroy, prevent_destroy, force_delete, provider, ignore_changes). In provider protocol 22, only delete requests carry the resolved provider-visible directives. See the directives reference for the full list, schema-default destruction protection, and the ignore_changes path rules.

awscc.s3.Bucket {
  bucket_name = 'my-bucket'

  directives {
    prevent_destroy = true
  }
}

Attributes

Attributes are key-value pairs inside a resource block:

awscc.ec2.Vpc {
  cidr_block           = '10.0.0.0/16'
  enable_dns_support   = true
  enable_dns_hostnames = true
}

Nested Blocks

Some resources accept nested blocks for repeated or structured configuration:

awscc.ec2.Ipam {
  tier = advanced

  operating_region {
    region_name = 'ap-northeast-1'
  }
}

Local Bindings Inside Blocks

Use let inside a resource block to create block-scoped variables. These bindings are substituted into attributes, and their expressions may remain deferred until their inputs resolve. The local names are not sent as provider attributes:

awscc.ec2.Vpc {
  let base_cidr = '10.0.0.0/16'

  cidr_block = base_cidr

  tags = {
    Name = "vpc-${base_cidr}"
  }
}

Backend Block

The backend block configures where Carina stores state:

backend s3 {
  bucket = 'my-state-bucket'
  key    = 'infra/carina.state.json'
  region = 'ap-northeast-1'
}

When no backend is configured, state is stored locally in carina.state.json.

The GitHub.com backend requires all three fields:

backend github {
  repository = 'owner/infra-state'
  branch = 'carina-state'
  path = 'states/prod/carina.state.json'
}

The repository and explicit branch must already exist. Concrete expressions such as env() are accepted; unresolved and secret values are rejected. repository is an owner/repo slug. branch must be non-empty and may contain /. path is a non-empty relative repository path with no empty or .. segments, and cannot end in .lock; <path>.lock holds the lease.

Read-only state and export commands also accept github://owner/repo/path/to/state.json?branch=states%2Fprod. The branch parameter is required and percent-decoded. Unknown query keys and duplicate branch parameters are errors. Credentials use non-empty GITHUB_TOKEN, then gh auth token --hostname github.com. Require Contents read/write on the state repository, or Contents read for upstream and exports consumers.

Recommend one branch per independently applied state. The hard limit is 10,000,000 decoded bytes (10 MB), with no splitting. GitHub keeps every version in history: use a private repository with narrow access. Plaintext values, provider-populated outputs, and resume tokens of unfinished creates remain in history; values written as secrets are hashed. Once committed, values cannot be removed from every copy, including other people’s clones. S3 by default keeps only the latest state.

Concurrency is measured, not guaranteed by GitHub documentation: over 50 trials of simultaneous creates of the same lock file and 50 trials of simultaneous updates with the same SHA, each trial had exactly one winner and a 409 loser. See GitHub backend and history for the protocol’s limits and retention policy.

State Manipulation

Import

Import an existing cloud resource into Carina’s state. The address in to names the binding of the resource that adopts it:

import {
  to = awscc.ec2.Vpc 'imported_vpc'
  id = 'vpc-0123456789abcdef0'
}

let imported_vpc = awscc.ec2.Vpc {
  cidr_block = '10.0.0.0/16'

  tags = {
    Name = 'imported'
  }
}

Moved

Rename a resource in state without destroying and recreating it:

moved {
  from = awscc.ec2.Vpc 'old_name'
  to   = awscc.ec2.Vpc 'new_name'
}

See Moving resources in state for loop-element addresses and plan-time move hints.

Removed

Remove a resource from Carina’s state without deleting the actual cloud resource:

removed {
  from = awscc.ec2.Vpc 'old_vpc'
}

Upstream State

Reference values exported by another Carina project:

let network = upstream_state {
  source = "../network"
}

awscc.ec2.SecurityGroup {
  group_description = 'Example security group'
  vpc_id = network.vpc_id
}

source is required, must be a string literal without interpolation, and points at the upstream project’s directory. The path is resolved relative to the enclosing .crn file’s directory. Carina loads the upstream’s configuration, resolves its backend, reads its state, and exposes the values published by its exports block through the declared binding.

External References at a Glance

Carina has four constructs for pulling data or declarations in from outside the current file. They look different because they do different things; the table below shows the split on when the data is fetched, what is fetched, and from where:

ConstructWhenWhatFrom
let m = use { source = '...' }parse timeDSL source (a module)local directory
let x = read <type> { }plan time when inputs are known; otherwise apply timecurrent cloud stateCloud API (data source)
import { to = ..., id = ... }apply timean existing cloud resource, adopted into stateCloud API + state backend
let x = upstream_state { source = '...' }plan timeanother Carina project’s exportsstate backend

The shape differences track these semantic differences:

  • use and upstream_state both name an external location via a source attribute and share their shape accordingly.
  • read leads with the resource type because the type is the primary piece of information for a live data-source read.
  • import is a top-level state-manipulation block parallel to moved / removed, not a let-RHS expression.

Comments

Carina supports three comment styles:

# Hash-style line comment
// Slash-style line comment

/* Block comment
   can span multiple lines */

/* Block comments /* can be nested */ like this */

All comments are ignored by the parser.

User-Defined Functions

Define reusable functions with fn:

fn tag_name(env: String, service: String): String {
  join('-', [env, service, 'vpc'])
}

awscc.ec2.Vpc {
  cidr_block = '10.0.0.0/16'

  tags = {
    Name = tag_name('prod', 'web')
  }
}

See Expressions for function definitions and function type promises for annotation validation.

Require Statement

Declare argument validation inside a module’s arguments block. A require statement contains a boolean condition and the message shown when it is false:

arguments {
  subnets: list(String)
  require length(subnets) > 0, 'At least one subnet must be specified'
  cidr_block: String
  require cidr_block != '', 'CIDR block cannot be empty'
}

An arguments block may contain zero or more requirements, in any order with its parameters. Each requirement sees every argument declared by the module, including later parameters, other arguments blocks, and sibling .crn files. A require at statement position outside an arguments block reports the syntax error “require must be written inside an arguments block” at the require keyword. Nested positions, such as inside an if, for, function, resource, attributes, exports, or per-argument block, produce an ordinary syntax error. Root configurations cannot contain arguments blocks.

To be called as a module, a directory must declare at least one argument parameter or output attribute. An arguments block containing only requirements does not make the directory callable; a call reports Module not found.

When validated directly, a module directory keeps its requirements unevaluated until its arguments are supplied by a call. Per-argument validation blocks remain local to their containing argument.

The condition is a validate expression that supports comparison operators (==, !=, >, <, >=, <=), integer arithmetic (+, -, *, /, %), and logical operators (&&, ||, !).