Terraform Resource Block Header
Match the opening line of a Terraform `resource "type" "name" {` block, capturing the resource type and local name.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("resource\\s+\"([\\w\\-]+)\"\\s+\"([\\w\\-]+)\"\\s*\\{", "g");
const input = "resource \"aws_s3_bucket\" \"main\" { bucket = \"my-app\" }";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"resource\\s+\"([\\w\\-]+)\"\\s+\"([\\w\\-]+)\"\\s*\\{")
input_text = "resource \"aws_s3_bucket\" \"main\" { bucket = \"my-app\" }"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`resource\s+"([\w\-]+)"\s+"([\w\-]+)"\s*\{`)
input := `resource "aws_s3_bucket" "main" { bucket = "my-app" }`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
resource\s+"([\w\-]+)"\s+"([\w\-]+)"\s*\{ (flags: g)Raw source: resource\s+"([\w\-]+)"\s+"([\w\-]+)"\s*\{
How it works
Examples
Input
resource "aws_s3_bucket" "main" { bucket = "my-app" }Matches
resource "aws_s3_bucket" "main" {
Input
resource "google_storage_bucket" "backups" {Matches
resource "google_storage_bucket" "backups" {
Input
data "aws_caller_identity" "current" {}No match
—Common use cases
- •Terraform module documentation generators
- •Drift detection that catalogues resources
- •Migration tooling between providers
- •Cost-allocation reporting from .tf files
Related patterns
HCL / Terraform Variable Reference
Text ProcessingMatch Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.
GraphQL Operation Header
Text ProcessingMatch GraphQL operation headers — `query`, `mutation`, or `subscription` — capturing the operation name.
C-Style Block Comment
Text ProcessingMatch C-style /* ... */ block comments across multiple lines.
JavaScript Variable Declaration
Text ProcessingMatch JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.