hashicorp/terraform · error

argument %q is required

Error message

argument %q is required

What it means

Thrown by SDKLikeDefaults.ApplyTo when a backend configuration attribute marked Required has no value from any source. The attribute was not set in HCL config, none of its declared environment variables (EnvVars) are set, and no Fallback default exists. This emulates the legacy Terraform SDK 'Required + EnvDefaultFunc' behaviour during backend config preparation.

Source

Thrown at internal/backend/backendbase/sdklike.go:227

		rawStr := ""
		if !vStr.IsNull() {
			rawStr = vStr.AsString()
		}

		if rawStr == "" {
			for _, envName := range defs.EnvVars {
				rawStr = os.Getenv(envName)
				if rawStr != "" {
					break
				}
			}
		}
		if rawStr == "" {
			rawStr = defs.Fallback
		}
		if defs.Required && rawStr == "" {
			return cty.NilVal, fmt.Errorf("argument %q is required", attrName)
		}

		// As a special case, if we still have an empty string and the original
		// value was null then we'll preserve the null. This is a compromise,
		// assuming that SDKLikeData knows how to treat a null value as a
		// zero value anyway and if we preserve the null then the recipient
		// of this result can still use the cty.Value result directly to
		// distinguish between the value being set explicitly to empty in
		// the config vs. being entirely unset.
		if rawStr == "" && givenVal.IsNull() {
			retAttrs[attrName] = givenVal
			continue
		}

		// By the time we get here, rawStr should be empty only if the original
		// value was unset and all of the fallback environment variables were
		// also unset. Otherwise, rawStr contains a string representation of
		// a value that we now need to convert back to the type that was

View on GitHub (pinned to c9def3e214)

Solutions

  1. Set the required attribute explicitly in the backend {} block in your Terraform configuration.
  2. Export the environment variable the backend expects for that attribute (check the backend's schema EnvVars list).
  3. If the value should have a default, add a non-empty Fallback in the backend's SDKLikeDefaults registration.

Example fix

// before (missing required attr)
backend "azurerm" {
  resource_group_name  = "rg"
  container_name        = "tfstate"
}
// after
backend "azurerm" {
  resource_group_name  = "rg"
  storage_account_name = "mystorageacct"
  container_name        = "tfstate"
  key                   = "terraform.tfstate"
}
Defensive patterns

Strategy: validation

Validate before calling

// Before calling backend Configure, ensure every required attribute is present.
func requiredAttrsPresent(cfg cty.Value, required []string) error {
    for _, a := range required {
        v := cfg.GetAttr(a)
        if v.IsNull() || (v.Type() == cty.String && v.AsString() == "") {
            return fmt.Errorf("argument %q is required", a)
        }
    }
    return nil
}

Try / catch

// Go: handle the error returned from ApplyTo / Configure
val, err := defaults.ApplyTo(config)
if err != nil {
    return fmt.Errorf("backend config invalid: %w", err)
}

Prevention

When it happens

Trigger: A backend block (e.g. backend 's3' or 'azurerm') omits a required attribute and the corresponding environment variable is also unset. Triggered during terraform init when PrepareConfig/Configure calls ApplyTo and a Required attribute's rawStr is empty after checking config, EnvVars, and Fallback.

Common situations: Missing region/account/credentials in a backend block while assuming an env var like AWS_DEFAULT_REGION or ARM_STORAGE_ACCOUNT_NAME will supply it; switching machines/CI where env vars are not exported; renaming an env var without updating both shell and config.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/1f7bce47693cbca3. Report an issue: GitHub.