← All recipes
Moderne licensed

Add a nested block to a Terraform resource

Recipe IDorg.openrewrite.terraform.AddResourceNestedBlock
Artifactorg.openrewrite.recipe:rewrite-terraform

Add a nested block (e.g. parameter { ... }) to a Terraform resource if no matching instance exists. When keyMatchers is provided, the recipe deduplicates by attribute keys so it works correctly with repeatable blocks like aws_db_parameter_group.parameter.

Single recipeProprietary

Usage

You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.terraform.AddResourceNestedBlock --recipe-option "resourceType=aws_db_parameter_group" --recipe-option "block=parameter {
  name  = "local_infile"
  value = "0"
}"

If the recipe isn’t available locally, install it with:

mod config recipes jar install org.openrewrite.recipe:rewrite-terraform:3.14.6

Options

NameTypeDescription
resourceTyperequiredStringA Terraform resource type, without the quotes.
e.g. aws_db_parameter_group
resourceNamePatternStringA regular expression matched against the resource's local name (the second label). If omitted, all resources of resourceType are considered.
e.g. rds_.*
resourceAttrMatchersListA list of attr=valueRegex entries. The resource is considered a match only if every entry matches an attribute on the resource (AND semantics). Useful to narrow scoping by family, engine, etc.
e.g. family=mysql.*|mariadb.*
keyMatchersListA list of attr=valueRegex entries identifying an existing nested block instance (e.g. name=local_infile for aws_db_parameter_group.parameter). If a sibling block of the same type matches every key, the nested block is considered already present and not inserted. Without keyMatchers, the recipe falls back to skipping insertion when any sibling block of the same type exists (suitable for single-instance blocks like lifecycle / timeouts).
e.g. name=local_infile
blockrequiredStringRaw multiline HCL of the nested block to insert, including the block type header and any labels. The block type (and labels) used for sibling lookup are derived from this value.
e.g. parameter { name = "local_infile" value = "0" }
positionStringWhere to place the new block within the resource body: first or last. Defaults to last.
e.g. last

Data tables

Structured output this recipe can produce.

  • Source files that had resultsSource files that were modified by the recipe run.org.openrewrite.table.SourcesFileResults
  • Source files that had search resultsSearch results that were found during the recipe run.org.openrewrite.table.SearchResults
  • Source files that errored on a recipeThe details of all errors produced by a recipe run.org.openrewrite.table.SourcesFileErrors
  • Recipe performanceStatistics used in analyzing the performance of recipes.org.openrewrite.table.RecipeRunStats