terra_madre/terraform
Terraform Top-Level Configuration Types
This module defines typed representations of Terraform’s top-level blocks: terraform, provider, resource, data, variable, output, locals, and module.
References
- Terraform Configuration
- Provider Configuration
- Resources
- Data Sources
- Variables
- Outputs
- Locals
- Modules
Types
A complete Terraform configuration (one or more .tf files merged).
Example
Config(
terraform: option.Some(TerraformSettings(...)),
providers: [Provider(...)],
resources: [Resource(...)],
data_sources: [DataSource(...)],
variables: [Variable(...)],
outputs: [Output(...)],
locals: [Locals(...)],
modules: [Module(...)],
)
pub type Config {
Config(
terraform: option.Option(TerraformSettings),
providers: List(Provider),
resources: List(Resource),
data_sources: List(DataSource),
variables: List(Variable),
outputs: List(Output),
locals: List(Locals),
modules: List(Module),
)
}
Constructors
-
Config( terraform: option.Option(TerraformSettings), providers: List(Provider), resources: List(Resource), data_sources: List(DataSource), variables: List(Variable), outputs: List(Output), locals: List(Locals), modules: List(Module), )
Data source block - reads existing infrastructure.
Reference: https://developer.hashicorp.com/terraform/language/data-sources
Example
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-*"]
}
}
DataSource(
type_: "aws_ami",
name: "ubuntu",
attributes: dict.from_list([
#("most_recent", hcl.BoolLiteral(True)),
#("owners", hcl.ListExpr([hcl.StringLiteral("099720109477")])),
]),
blocks: [
hcl.block_with_attrs("filter", [], [
#("name", hcl.StringLiteral("name")),
#("values", hcl.ListExpr([
hcl.StringLiteral("ubuntu/images/hvm-ssd/ubuntu-focal-*"),
])),
]),
],
meta: hcl.empty_meta(),
)
pub type DataSource {
DataSource(
type_: String,
name: String,
attributes: dict.Dict(String, hcl.Expr),
blocks: List(hcl.Block),
meta: hcl.MetaArguments,
)
}
Constructors
-
DataSource( type_: String, name: String, attributes: dict.Dict(String, hcl.Expr), blocks: List(hcl.Block), meta: hcl.MetaArguments, )
Local values block.
Reference: https://developer.hashicorp.com/terraform/language/values/locals
Example
locals {
common_tags = {
Environment = var.environment
Project = "my-project"
}
instance_name = "${var.prefix}-instance"
}
Locals(dict.from_list([
#("common_tags", hcl.MapExpr([
#(hcl.IdentKey("Environment"), hcl.ref("var.environment")),
#(hcl.IdentKey("Project"), hcl.StringLiteral("my-project")),
])),
#("instance_name", hcl.TemplateExpr([
hcl.Interpolation(hcl.ref("var.prefix")),
hcl.LiteralPart("-instance"),
])),
]))
pub type Locals {
Locals(values: dict.Dict(String, hcl.Expr))
}
Constructors
Module call block.
Reference: https://developer.hashicorp.com/terraform/language/modules
Example
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
providers = {
aws = aws.west
}
}
Module(
name: "vpc",
source: "terraform-aws-modules/vpc/aws",
version: option.Some("5.0.0"),
inputs: dict.from_list([
#("name", hcl.StringLiteral("my-vpc")),
#("cidr", hcl.StringLiteral("10.0.0.0/16")),
#("azs", hcl.ListExpr([
hcl.StringLiteral("us-west-2a"),
hcl.StringLiteral("us-west-2b"),
])),
]),
providers: option.Some(dict.from_list([
#("aws", hcl.ref("aws.west")),
])),
meta: hcl.empty_meta(),
)
pub type Module {
Module(
name: String,
source: String,
version: option.Option(String),
inputs: dict.Dict(String, hcl.Expr),
providers: option.Option(dict.Dict(String, hcl.Expr)),
meta: hcl.MetaArguments,
)
}
Constructors
-
Module( name: String, source: String, version: option.Option(String), inputs: dict.Dict(String, hcl.Expr), providers: option.Option(dict.Dict(String, hcl.Expr)), meta: hcl.MetaArguments, )
Output value block.
Reference: https://developer.hashicorp.com/terraform/language/values/outputs
Example
output "instance_ip" {
value = aws_instance.web.public_ip
description = "Public IP of the web server"
sensitive = false
}
Output(
name: "instance_ip",
value: hcl.ref("aws_instance.web.public_ip"),
description: option.Some("Public IP of the web server"),
sensitive: option.Some(False),
depends_on: option.None,
precondition: [],
)
pub type Output {
Output(
name: String,
value: hcl.Expr,
description: option.Option(String),
sensitive: option.Option(Bool),
depends_on: option.Option(List(hcl.Expr)),
precondition: List(hcl.Condition),
)
}
Constructors
-
Output( name: String, value: hcl.Expr, description: option.Option(String), sensitive: option.Option(Bool), depends_on: option.Option(List(hcl.Expr)), precondition: List(hcl.Condition), )
Provider configuration block.
Reference: https://developer.hashicorp.com/terraform/language/providers/configuration
Example
provider "aws" {
region = "us-west-2"
profile = "production"
}
provider "aws" {
alias = "west"
region = "us-west-1"
}
// Primary AWS provider
Provider(
name: "aws",
alias: option.None,
attributes: dict.from_list([
#("region", hcl.StringLiteral("us-west-2")),
#("profile", hcl.StringLiteral("production")),
]),
blocks: [],
)
// Aliased provider
Provider(
name: "aws",
alias: option.Some("west"),
attributes: dict.from_list([
#("region", hcl.StringLiteral("us-west-1")),
]),
blocks: [],
)
pub type Provider {
Provider(
name: String,
alias: option.Option(String),
attributes: dict.Dict(String, hcl.Expr),
blocks: List(hcl.Block),
)
}
Constructors
-
Provider( name: String, alias: option.Option(String), attributes: dict.Dict(String, hcl.Expr), blocks: List(hcl.Block), )
Provider version requirement in required_providers block.
Example
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
ProviderRequirement("hashicorp/aws", option.Some("~> 5.0"))
pub type ProviderRequirement {
ProviderRequirement(
source: String,
version: option.Option(String),
)
}
Constructors
-
ProviderRequirement( source: String, version: option.Option(String), )
Resource block - manages infrastructure objects.
Reference: https://developer.hashicorp.com/terraform/language/resources
Example
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t2.micro"
count = 3
tags = {
Name = "Web-${count.index}"
}
lifecycle {
create_before_destroy = true
}
}
Resource(
type_: "aws_instance",
name: "web",
attributes: dict.from_list([
#("ami", hcl.StringLiteral("ami-12345")),
#("instance_type", hcl.StringLiteral("t2.micro")),
]),
blocks: [],
meta: hcl.MetaArguments(
count: option.Some(hcl.IntLiteral(3)),
for_each: option.None,
provider: option.None,
depends_on: option.None,
),
lifecycle: option.Some(hcl.Lifecycle(
create_before_destroy: option.Some(True),
prevent_destroy: option.None,
ignore_changes: option.None,
replace_triggered_by: option.None,
precondition: [],
postcondition: [],
)),
)
pub type Resource {
Resource(
type_: String,
name: String,
attributes: dict.Dict(String, hcl.Expr),
blocks: List(hcl.Block),
meta: hcl.MetaArguments,
lifecycle: option.Option(hcl.Lifecycle),
)
}
Constructors
-
Resource( type_: String, name: String, attributes: dict.Dict(String, hcl.Expr), blocks: List(hcl.Block), meta: hcl.MetaArguments, lifecycle: option.Option(hcl.Lifecycle), )
The terraform {} block for configuration settings.
Reference: https://developer.hashicorp.com/terraform/language/settings
Example
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "my-terraform-state"
key = "state.tfstate"
}
}
TerraformSettings(
required_version: option.Some(">= 1.0"),
required_providers: dict.from_list([
#("aws", ProviderRequirement("hashicorp/aws", option.Some("~> 5.0"))),
]),
backend: option.Some(hcl.block_with_attrs("s3", [], [
#("bucket", hcl.StringLiteral("my-terraform-state")),
#("key", hcl.StringLiteral("state.tfstate")),
])),
cloud: option.None,
)
pub type TerraformSettings {
TerraformSettings(
required_version: option.Option(String),
required_providers: dict.Dict(String, ProviderRequirement),
backend: option.Option(hcl.Block),
cloud: option.Option(hcl.Block),
)
}
Constructors
-
TerraformSettings( required_version: option.Option(String), required_providers: dict.Dict(String, ProviderRequirement), backend: option.Option(hcl.Block), cloud: option.Option(hcl.Block), )
Input variable block.
Reference: https://developer.hashicorp.com/terraform/language/values/variables
Example
variable "instance_type" {
type = string
default = "t2.micro"
description = "EC2 instance type"
validation {
condition = contains(["t2.micro", "t2.small"], var.instance_type)
error_message = "Must be t2.micro or t2.small"
}
}
Variable(
name: "instance_type",
type_constraint: option.Some(hcl.Identifier("string")),
default: option.Some(hcl.StringLiteral("t2.micro")),
description: option.Some("EC2 instance type"),
sensitive: option.None,
nullable: option.None,
validation: [
hcl.Condition(
condition: hcl.FunctionCall("contains", [
hcl.ListExpr([hcl.StringLiteral("t2.micro"), hcl.StringLiteral("t2.small")]),
hcl.ref("var.instance_type"),
], False),
error_message: "Must be t2.micro or t2.small",
),
],
)
pub type Variable {
Variable(
name: String,
type_constraint: option.Option(hcl.Expr),
default: option.Option(hcl.Expr),
description: option.Option(String),
sensitive: option.Option(Bool),
nullable: option.Option(Bool),
validation: List(hcl.Condition),
)
}
Constructors
-
Variable( name: String, type_constraint: option.Option(hcl.Expr), default: option.Option(hcl.Expr), description: option.Option(String), sensitive: option.Option(Bool), nullable: option.Option(Bool), validation: List(hcl.Condition), )
Values
pub fn simple_data(
type_: String,
name: String,
attrs: List(#(String, hcl.Expr)),
) -> DataSource
Create a simple data source with just attributes.
simple_data("aws_ami", "ubuntu", [#("most_recent", hcl.BoolLiteral(True))])
pub fn simple_module(
name: String,
source: String,
inputs: List(#(String, hcl.Expr)),
) -> Module
Create a simple module call.
simple_module("vpc", "./modules/vpc", [
#("cidr", hcl.StringLiteral("10.0.0.0/16")),
])
pub fn simple_output(name: String, value: hcl.Expr) -> Output
Create a simple output.
simple_output("ip", hcl.ref("aws_instance.web.public_ip"))
pub fn simple_provider(
name: String,
attrs: List(#(String, hcl.Expr)),
) -> Provider
Create a simple provider with just attributes.
simple_provider("aws", [#("region", hcl.StringLiteral("us-west-2"))])
pub fn simple_resource(
type_: String,
name: String,
attrs: List(#(String, hcl.Expr)),
) -> Resource
Create a simple resource with just attributes.
simple_resource("aws_instance", "web", [
#("ami", hcl.StringLiteral("ami-12345")),
#("instance_type", hcl.StringLiteral("t2.micro")),
])