Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not pass null value variables #1367

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func parseTerragruntOptionsFromArgs(terragruntVersion string, args []string, wri

includeExternalDependencies := parseBooleanArg(args, OPT_TERRAGRUNT_INCLUDE_EXTERNAL_DEPENDENCIES, false)

passNullVars := parseBooleanArg(args, OPT_TERRAGRUNT_PASS_NULL_VARS, false)

iamRole, err := parseStringArg(args, OPT_TERRAGRUNT_IAM_ROLE, os.Getenv("TERRAGRUNT_IAM_ROLE"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -170,6 +172,7 @@ func parseTerragruntOptionsFromArgs(terragruntVersion string, args []string, wri
opts.HclFile = filepath.ToSlash(terragruntHclFilePath)
opts.Debug = debug
opts.AwsProviderPatchOverrides = awsProviderPatchOverrides
opts.PassNullVars = passNullVars

return opts, nil
}
Expand Down Expand Up @@ -386,18 +389,21 @@ func parseMutliStringKeyValueArg(args []string, argName string, defaultValue map
// Convert the given variables to a map of environment variables that will expose those variables to Terraform. The
// keys will be of the format TF_VAR_xxx and the values will be converted to JSON, which Terraform knows how to read
// natively.
func toTerraformEnvVars(vars map[string]interface{}) (map[string]string, error) {
func toTerraformEnvVars(vars map[string]interface{}, passNullVars bool) (map[string]string, error) {
out := map[string]string{}

for varName, varValue := range vars {
envVarName := fmt.Sprintf("TF_VAR_%s", varName)
// Do not pass null value variables if option is not set
// null assigned variables in terraform are "unset" variables (do not exist)
if varValue != nil || passNullVars {
envVarName := fmt.Sprintf("TF_VAR_%s", varName)
envVarValue, err := asTerraformEnvVarJsonValue(varValue)
if err != nil {
return nil, err
}

envVarValue, err := asTerraformEnvVarJsonValue(varValue)
if err != nil {
return nil, err
out[envVarName] = string(envVarValue)
}

out[envVarName] = string(envVarValue)
}

return out, nil
Expand Down
5 changes: 4 additions & 1 deletion cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const OPT_TERRAGRUNT_CHECK = "terragrunt-check"
const OPT_TERRAGRUNT_HCLFMT_FILE = "terragrunt-hclfmt-file"
const OPT_TERRAGRUNT_DEBUG = "terragrunt-debug"
const OPT_TERRAGRUNT_OVERRIDE_ATTR = "terragrunt-override-attr"
const OPT_TERRAGRUNT_PASS_NULL_VARS = "terragrunt-pass-null-vars"

var ALL_TERRAGRUNT_BOOLEAN_OPTS = []string{
OPT_NON_INTERACTIVE,
Expand All @@ -56,6 +57,7 @@ var ALL_TERRAGRUNT_BOOLEAN_OPTS = []string{
OPT_TERRAGRUNT_CHECK,
OPT_TERRAGRUNT_STRICT_INCLUDE,
OPT_TERRAGRUNT_DEBUG,
OPT_TERRAGRUNT_PASS_NULL_VARS,
}
var ALL_TERRAGRUNT_STRING_OPTS = []string{
OPT_TERRAGRUNT_CONFIG,
Expand Down Expand Up @@ -181,6 +183,7 @@ GLOBAL OPTIONS:
terragrunt-hclfmt-file The path to a single terragrunt.hcl file that the hclfmt command should run on.
terragrunt-override-attr A key=value attribute to override in a provider block as part of the aws-provider-patch command. May be specified multiple times.
terragrunt-debug Write terragrunt-debug.tfvars to working folder to help root-cause issues.
terragrunt-pass-null-vars Pass null value input variables to Terraform (as "null" string)

VERSION:
{{.Version}}{{if len .Authors}}
Expand Down Expand Up @@ -567,7 +570,7 @@ func runActionWithHooks(description string, terragruntOptions *options.Terragrun
// The Terragrunt configuration can contain a set of inputs to pass to Terraform as environment variables. This method
// sets these environment variables in the given terragruntOptions.
func setTerragruntInputsAsEnvVars(terragruntOptions *options.TerragruntOptions, terragruntConfig *config.TerragruntConfig) error {
asEnvVars, err := toTerraformEnvVars(terragruntConfig.Inputs)
asEnvVars, err := toTerraformEnvVars(terragruntConfig.Inputs, terragruntOptions.PassNullVars)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions docs/_docs/04_reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ prefix `--terragrunt-` (e.g., `--terragrunt-config`). The currently available op
- [terragrunt-check](#terragrunt-check)
- [terragrunt-hclfmt-file](#terragrunt-hclfmt-file)
- [terragrunt-override-attr](#terragrunt-override-attr)
- [terragrunt-pass-null-vars](#terragrunt-pass-null-vars)


### terragrunt-config
Expand Down Expand Up @@ -502,3 +503,9 @@ When passed in, run `hclfmt` only on specified `*/terragrunt.hcl` file.

A `KEY=VALUE` attribute to override in a `provider` block as part of the [aws-provider-patch
command](#aws-provider-patch). May be specified multiple times.

### terragrunt-pass-null-vars

**CLI Arg**: `--terragrunt-pass-null-vars`

When passed in, Terragrunt will pass null value input variables to Terraform as string (i.e: "null")
4 changes: 4 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ type TerragruntOptions struct {
// Attributes to override in AWS provider nested within modules as part of the aws-provider-patch command. See that
// command for more info.
AwsProviderPatchOverrides map[string]string

// Pass to Terraform null value variables as string (terraform does not export null variables)
PassNullVars bool
}

// Create a new TerragruntOptions object with reasonable defaults for real usage
Expand Down Expand Up @@ -260,6 +263,7 @@ func (terragruntOptions *TerragruntOptions) Clone(terragruntConfigPath string) *
StrictInclude: terragruntOptions.StrictInclude,
RunTerragrunt: terragruntOptions.RunTerragrunt,
AwsProviderPatchOverrides: terragruntOptions.AwsProviderPatchOverrides,
PassNullVars: terragruntOptions.PassNullVars,
}
}

Expand Down