Blocks of type "resource" are not expected here

I’m a noob to Terraform and I created my first tf file. I am having trouble trying to define the resources for my configuration. Not sure why I am getting this error when initializing. Below is my config file. Any help is greatly appreciated.

Blocks of type “resource” are not expected here.

terraform {
  backend "remote" {
    organization = "RTR-limited"

    workspaces {
      name = "Juniper-Stage"
    }
  required_providers {
    junos = {
      source = "jeremmfr/junos"
      version = "1.5.0"
    }
   }
  }
  resource junos_interface "interface_switch_demo" {
    name         = "ge-0/0/5"
    description  = "interfaceSwitchDemo"
    trunk        = true
    vlan_members = ["100"]
  }
}

Hi @exd42062!

I used my moderator powers to edit your question to mark the code example as code, so we can see the indentation as you wrote it.

Having done that, what jumps out to me based on matching up the braces and indentation is that you seem to be missing a closing brace } for your backend "remote" block, just before the required_providers block.

I think that missing brace is causing the parser to think your resource block is nested inside the terraform block, which it therefore reports as invalid because resource blocks belong at the top level.

Ahhhh. Thank you for the quick reply. Next time I will insert as code. I will update it now.

I updated but i am still getting errors.

There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

**Error:** **Unsupported block type**

on main.tf line 5, in terraform:

5: workspaces {

Blocks of type "workspaces" are not expected here.

**Error:** **Unsupported block type**

on main.tf line 14, in terraform:

14: resource junos_interface "interface_switch_demo" {

Blocks of type "resource" are not expected here.
terraform {
  backend "remote" {
    organization = "RTR-limited"
  }
    workspaces {
      name = "Juniper-Stage"
    }
  required_providers {
    junos = {
      source = "jeremmfr/junos"
      version = "1.5.0"
    }
   }
  resource junos_interface "interface_switch_demo" {
    name         = "ge-0/0/5"
    description  = "interfaceSwitchDemo"
    trunk        = true
    vlan_members = ["100"]
  }
}

Also, how did you insert as code?

You seem to have inserted the extra brace before the workspaces block rather than before the required_providers block, so now the workspaces block is outside of the backend "remote" block.

Here’s a full example using the correct syntax:

terraform {
  backend "remote" {
    organization = "RTR-limited"

    workspaces {
      name = "Juniper-Stage"
    }
  }
  required_providers {
    junos = {
      source  = "jeremmfr/junos"
      version = "1.5.0"
    }
  }
  resource "junos_interface" "interface_switch_demo" {
    name         = "ge-0/0/5"
    description  = "interfaceSwitchDemo"
    trunk        = true
    vlan_members = ["100"]
  }
}

Testing inserting code.

terraform {
  backend "remote" {
    organization = "RTR-limited"
  }
    workspaces {
      name = "Juniper-Stage"
    }
  required_providers {
    junos = {
      source = "jeremmfr/junos"
      version = "1.5.0"
    }
   }
  resource junos_interface "interface_switch_demo" {
    name         = "ge-0/0/5"
    description  = "interfaceSwitchDemo"
    trunk        = true
    vlan_members = ["100"]
  }
}

I copied and pasted what you sent and I still get an error.

latest code

terraform {
  backend "remote" {
    organization = "RTR-limited"

    workspaces {
      name = "Juniper-Stage"
    }
  }
  required_providers {
    junos = {
      source  = "jeremmfr/junos"
      version = "1.5.0"
    }
  }
  resource "junos_interface" "interface_switch_demo" {
    name         = "ge-0/0/5"
    description  = "interfaceSwitchDemo"
    trunk        = true
    vlan_members = ["100"]
  }
}

Am I missing something else?

I think figured it out…i didnt realize terraform {} was its own block separate from the rest of the config file.

terraform {
  required_providers {
    junos = {
      source = "jeremmfr/junos"
      version = "1.5.0"
    }
  }
}

provider "junos" {
  # Configuration options
}

Hi @exd42062,

Indeed, what I shared in my most recent comment wasn’t correct either. I apparently made an editorial error while I was changing what you shared.