gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.97k stars 967 forks source link

Crash on MacOS when AWS credentials are empty (get_aws_account_id()) #960

Closed gregops closed 4 years ago

gregops commented 4 years ago

Not a showstopper, as the workaround is to have working AWS credentials, but a graceful error would've been better, plus it would tell me what the underlying issue was (missing creds, STS expiry, right creds but AWS permission not wide enough etc).

It seems like function get_aws_account_id() expects AWS credentials to be present and working, and if they're missing, or are present but relate to an expired STS role assumption session, Terragrunt will crash hard (segfault).

This may be a Golang AWS SDK issue, I'm not sure.

My terragrunt.hcl:

remote_state {
  backend = "s3"
  config = {
    bucket         = "bucket_name"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "eu-west-2"
    role_arn       = "arn:aws:iam::${get_aws_account_id()}:role/TerragruntAdministrator"
    encrypt        = true
    dynamodb_table = "terraform-lock"
    s3_bucket_tags = {
      owner = "Terragrunt"
      name  = "Terraform state storage"
    }
    dynamodb_table_tags = {
      owner = "Terragrunt"
      name  = "Terraform lock table"
    }
  }
}

Command (in one of sub-dirs, which sources the above terragrunt.hcl):

terragrunt plan

Output:

[terragrunt] 2019/11/24 10:44:48 Reading Terragrunt config file at /Users/greg/top-level/sub-module/terragrunt.hcl
[terragrunt] 2019/11/24 10:44:50 /Users/greg/top-level/terragrunt.hcl:10,38-57: Error in function call; Call to function "get_aws_account_id" failed: panic in function implementation: runtime error: invalid memory address or nil pointer dereference
goroutine 1 [running]:
runtime/debug.Stack(0xc00063ab80, 0x1ac46c0, 0x25f76a0)
    /usr/local/Cellar/go/1.13.4/libexec/src/runtime/debug/stack.go:24 +0x9d
github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function.errorForPanic(...)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function/error.go:44
github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function.Function.Call.func1(0xc00063af08, 0xc00063af28)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function/function.go:239 +0x8e
panic(0x1ac46c0, 0x25f76a0)
    /usr/local/Cellar/go/1.13.4/libexec/src/runtime/panic.go:679 +0x1b2
github.com/gruntwork-io/terragrunt/config.getAWSAccountID(0xc0003639c0, 0xc0000e3760, 0x102ef91, 0x1c48be0, 0xc0002b2d18, 0x10)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config_helpers.go:328 +0x44
github.com/gruntwork-io/terragrunt/config.wrapVoidToStringAsFuncImpl.func1(0x262b840, 0x0, 0x0, 0x1e96c00, 0xc0000c6199, 0xc0000c6199, 0x0, 0x0, 0xc000437390, 0x0, ...)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/cty_helpers.go:39 +0x3b
github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function.Function.Call(0xc00045c240, 0x262b840, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/zclconf/go-cty/cty/function/function.go:243 +0x30c
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax.(*FunctionCallExpr).Value(0xc0003e21e0, 0xc000441ea0, 0xd, 0xd, 0x0, 0x0, 0x1e96c00, 0xc0000c6199, 0x1a45f20)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression.go:385 +0x10c3
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax.(*TemplateExpr).Value(0xc000438300, 0xc000441ea0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression_template.go:30 +0x115
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax.(*ObjectConsExpr).Value(0xc0003cf040, 0xc000441ea0, 0x1a45f20, 0xc000437350, 0x1a26b40, 0xc00045a680, 0x100dec6, 0x0, 0x1a45f20)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/expression.go:703 +0x285
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.DecodeExpression(0x29ce510, 0xc0003cf040, 0xc000441ea0, 0x1bf47e0, 0xc00045a698, 0xc00045a698, 0x199, 0x1bf6fa0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:273 +0x59
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.decodeBodyToStruct(0x1e96100, 0xc0003a5a20, 0xc000441ea0, 0x1b34a20, 0xc00045a680, 0x199, 0xa, 0x8, 0xc0002b4de8)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:111 +0x692
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.decodeBodyToValue(0x1e96100, 0xc0003a5a20, 0xc000441ea0, 0x1b34a20, 0xc00045a680, 0x199, 0x103f283, 0x1b34a20, 0x107740)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:43 +0xd5
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.decodeBlockToValue(0xc000462820, 0xc000441ea0, 0x1b34a20, 0xc00045a680, 0x199, 0x199, 0x1ea9020, 0x1aaa2e0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:243 +0x3b5
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.decodeBodyToStruct(0x1e96100, 0xc0003a5b80, 0xc000441ea0, 0x1bd45e0, 0xc000116b00, 0x199, 0x0, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:187 +0x177f
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.decodeBodyToValue(0x1e96100, 0xc0003a5b80, 0xc000441ea0, 0x1bd45e0, 0xc000116b00, 0x199, 0x0, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:43 +0xd5
github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl.DecodeBody(0x1e96100, 0xc0003a5b80, 0xc000441ea0, 0x1a13a00, 0xc000116b00, 0xc000441720, 0xc000441ea0, 0x260f960)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/hashicorp/hcl2/gohcl/decode.go:36 +0xe2
github.com/gruntwork-io/terragrunt/config.decodeHcl(0xc0002b9f00, 0xc00010a900, 0x2b, 0x1a13a00, 0xc000116b00, 0xc0000e3760, 0xc0003639c0, 0xc000440ec0, 0xc000441720, 0x0, ...)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:417 +0x127
github.com/gruntwork-io/terragrunt/config.decodeAsTerragruntConfigFile(0xc0002b9f00, 0xc00010a900, 0x2b, 0xc0000e3760, 0xc0003639c0, 0xc000440ec0, 0xc000441720, 0xc000441720, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:392 +0xa5
github.com/gruntwork-io/terragrunt/config.ParseConfigString(0xc00040c000, 0x778, 0xc0000e3760, 0xc0003639c0, 0xc00010a900, 0x2b, 0x1, 0xc0000a8ba0, 0x59)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:341 +0x1d7
github.com/gruntwork-io/terragrunt/config.ParseConfigFile(0xc00010a900, 0x2b, 0xc0000e3760, 0xc0003639c0, 0x2b, 0x0, 0xc0002b5b08)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:279 +0x8c
github.com/gruntwork-io/terragrunt/config.parseIncludedConfig(0xc0003639c0, 0xc0000e3760, 0x4a, 0xc0000e3760, 0xc0003a6e10)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:594 +0x95
github.com/gruntwork-io/terragrunt/config.ParseConfigString(0xc00019ca80, 0x31e, 0xc0000e3760, 0x0, 0xc00010c5f0, 0x4a, 0x0, 0x0, 0x6c)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:356 +0x26c
github.com/gruntwork-io/terragrunt/config.ParseConfigFile(0xc00010c5f0, 0x4a, 0xc0000e3760, 0x0, 0x1, 0x1, 0x2)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:279 +0x8c
github.com/gruntwork-io/terragrunt/config.ReadTerragruntConfig(0xc0000e3760, 0x1, 0x1, 0x1c04ac3)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/config/config.go:268 +0xc4
github.com/gruntwork-io/terragrunt/cli.runTerragrunt(0xc0000e3760, 0x5, 0x5)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:243 +0xa6
github.com/gruntwork-io/terragrunt/cli.runCommand(0x7ffeefbff81b, 0x4, 0xc0000e3760, 0x7ffeefbff81b, 0x4)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:229 +0xb2
github.com/gruntwork-io/terragrunt/cli.runApp(0xc0000e3600, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:210 +0x116
github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli.HandleAction(0x1a63120, 0x1c47c70, 0xc0000e3600, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:490 +0xc8
github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli.(*App).Run(0xc000345ba0, 0xc0000a6040, 0x4, 0x4, 0x0, 0x0)
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:264 +0x58c
main.main()
    /private/tmp/terragrunt-20191114-11685-qlg1f9/src/github.com/gruntwork-io/terragrunt/main.go:27 +0x186
.
[terragrunt] 2019/11/24 10:44:50 Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Versions:

go: stable 1.13.4 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.13.4 (9,271 files, 414MB) *
  Poured from bottle on 2019-11-24 at 10:42:37
terragrunt: stable 0.21.6 (bottled)
Thin wrapper for Terraform e.g. for locking state
https://github.com/gruntwork-io/terragrunt
/usr/local/Cellar/terragrunt/0.21.6 (3 files, 29.5MB) *
  Poured from bottle on 2019-11-14 at 10:48:51
terraform: stable 0.12.16 (bottled), HEAD
Tool to build, change, and version infrastructure
https://www.terraform.io/
Conflicts with:
  tfenv (because tfenv symlinks terraform binaries)
/usr/local/Cellar/terraform/0.12.13 (6 files, 50.3MB) *
  Poured from bottle on 2019-11-10 at 20:24:34

MacOS 10.14.5, Homebrew 2.1.16.

brikis98 commented 4 years ago

Thanks for reporting! If anyone has a few minutes, a PR to handle that error more gracefully is very welcome 👍

yorinasub17 commented 4 years ago

Fixed in https://github.com/gruntwork-io/terragrunt/releases/tag/v0.21.8. Binaries will show up shortly.