Resource Blocks

Resource Blocks #

Everything you can set is defined as a Resource Block. Resource blocks have the following format.

resource "resource_type" "resource_name" {
    ensure = "present"
    ... other key = value entries ...
}

For resources that are identified using a single string (Logins, Agent Operators, Databases, etc.), the resource_name is often used to identify the resource. It can be overridden using the appropriate attribute. Please see the Resource Block Schema section.

This is an example Resource Block that creates a SQL Server agent operator:

resource "agent_operator" "june" {
    ensure = "present"
    name = "june"
    email_address = "june@test.com"
}

A shortened version might look like this:

resource "agent_operator" "june" {
    email_address = "june@test.com"
}

A fake sample showing more data types:

resource "fake_resource" "fake" {
    ensure = "present"
    string_val = "value"
    int_val = 37
    bool_val = true
    string_array = ["a", "b", "c"]
}

Multiline strings #

These can be constructed using a heredoc format. Avoid any spaces after the <<EOF.

resource "agent_job_step" "step two" {
    job = "test"
    id = 1
    name = "step 2"
    command = <<EOF
        SELECT 1
        SELECT 2
    EOF
}

Notes:

  • Avoid any spaces before or after the EOF or between the << and the EOF. This often trips me up but it’s buried in the library I use.
  • The EOF can be any unique string not appearing in command.
  • The ending EOF needs to be on a line by itself but can have leading white space.
  • The command text will be “dedented” to remove consistent white space at the start of each line. That makes it easier to format the command. You can indent it in the HCL file and consistent leading white space will be removed before passing to SQL Server.