liveBook · Manning

Schema

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A schema is a data structure that defines the parameters, attributes, configuration values, types, requirements, computed status, defaults, and documentation associated with a Terraform component.

A Terraform schema defines the parameters, attributes, and configuration values exposed by a provider, resource, or data source. It specifies types, required or computed status, defaults, and documentation. section 12.3.1

Required, optional, and computed settings distinguish user-supplied values from values a provider may generate. Computed values may be unknown during planning. section 12.5.1

Listing 12.3 Schema example
func (p *MastodonProvider) Schema(ctx context.Context, req 
provider.SchemaRequest, resp *provider.SchemaResponse) {        #1
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{                  #2
            "host": schema.StringAttribute{                       #3
                MarkdownDescription: "Mastodon host to connect to.",
                Optional:            true,
                Default:             stringdefault.StaticString(
"mastodon.social"),                                             #4
            },
            "email": schema.StringAttribute{
                MarkdownDescription: "Username to connect to the server as.",
                Optional:            true,
                DeprecationMessage:  "Use access token instead.", #5
            },
            "password": schema.StringAttribute{
                MarkdownDescription: "Password to use for connecting to the 
server.",
                Optional:            true,
                Sensitive:           true,                        #6
                DeprecationMessage:  "Use access token instead.",
            },
            "access_token": schema.StringAttribute{
                MarkdownDescription: "Password to use for connecting to the 
server.",
                Optional:            true,
                Sensitive:           true,
            },
        },
    }
}
#1 Schemas are defined in their own functions.
#2 Schemas are maps of schema attributes.
#3 All schemas have to map to a type, such as String.
#4 Schemas can have default values.
#5 It’s possible to mark an attribute as deprecated.
#6 Sensitive attributes are filtered out from logs.

See a provider schema with typed attributes, descriptions, an optional host default, and a deprecation message. section 12.3.1

Mastodon account lookup

A data source schema can accept an account name and define computed attributes such as display name, note, and account status. section 12.5.1

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains types, required or computed status, defaults, and documentation, while leaving field-specific functionality to further treatment.
Examples
Uses Mastodon provider settings and an account data source with computed account attributes.
What you can do after
Build schemas for provider configuration and data source parameters and attributes.
How it connects
Links schemas to providers, resources, data sources, provider models, and computed values.

Also explained in

Compare how 44 books explain schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.