/
config.go
1292 lines (1114 loc) · 45.8 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package config
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/zclconf/go-cty/cty/gocty"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
"github.com/terraform-modules-krish/go-commons/errors"
"github.com/terraform-modules-krish/go-commons/files"
"github.com/terraform-modules-krish/terragrunt/codegen"
"github.com/terraform-modules-krish/terragrunt/options"
"github.com/terraform-modules-krish/terragrunt/remote"
"github.com/terraform-modules-krish/terragrunt/util"
)
const (
DefaultTerragruntConfigPath = "terragrunt.hcl"
DefaultTerragruntJsonConfigPath = "terragrunt.hcl.json"
)
const FoundInFile = "found_in_file"
const (
MetadataTerraform = "terraform"
MetadataTerraformBinary = "terraform_binary"
MetadataTerraformVersionConstraint = "terraform_version_constraint"
MetadataTerragruntVersionConstraint = "terragrunt_version_constraint"
MetadataRemoteState = "remote_state"
MetadataDependencies = "dependencies"
MetadataDependency = "dependency"
MetadataDownloadDir = "download_dir"
MetadataPreventDestroy = "prevent_destroy"
MetadataSkip = "skip"
MetadataIamRole = "iam_role"
MetadataIamAssumeRoleDuration = "iam_assume_role_duration"
MetadataIamAssumeRoleSessionName = "iam_assume_role_session_name"
MetadataInputs = "inputs"
MetadataLocals = "locals"
MetadataGenerateConfigs = "generate"
MetadataRetryableErrors = "retryable_errors"
MetadataRetryMaxAttempts = "retry_max_attempts"
MetadataRetrySleepIntervalSec = "retry_sleep_interval_sec"
MetadataDependentModules = "dependent_modules"
)
// Order matters, for example if none of the files are found `GetDefaultConfigPath` func returns the last element.
var DefaultTerragruntConfigPaths = []string{
DefaultTerragruntJsonConfigPath,
DefaultTerragruntConfigPath,
}
// TerragruntConfig represents a parsed and expanded configuration
// NOTE: if any attributes are added, make sure to update terragruntConfigAsCty in config_as_cty.go
type TerragruntConfig struct {
Terraform *TerraformConfig
TerraformBinary string
TerraformVersionConstraint string
TerragruntVersionConstraint string
RemoteState *remote.RemoteState
Dependencies *ModuleDependencies
DownloadDir string
PreventDestroy *bool
Skip bool
IamRole string
IamAssumeRoleDuration *int64
IamAssumeRoleSessionName string
Inputs map[string]interface{}
Locals map[string]interface{}
TerragruntDependencies []Dependency
GenerateConfigs map[string]codegen.GenerateConfig
RetryableErrors []string
RetryMaxAttempts *int
RetrySleepIntervalSec *int
// Fields used for internal tracking
// Indicates whether or not this is the result of a partial evaluation
IsPartial bool
// Map of processed includes
ProcessedIncludes IncludeConfigs
// Map to store fields metadata
FieldsMetadata map[string]map[string]interface{}
// List of dependent modules
DependentModulesPath []*string
}
func (conf *TerragruntConfig) String() string {
return fmt.Sprintf("TerragruntConfig{Terraform = %v, RemoteState = %v, Dependencies = %v, PreventDestroy = %v}", conf.Terraform, conf.RemoteState, conf.Dependencies, conf.PreventDestroy)
}
// GetIAMRoleOptions is a helper function that converts the Terragrunt config IAM role attributes to
// options.IAMRoleOptions struct.
func (conf *TerragruntConfig) GetIAMRoleOptions() options.IAMRoleOptions {
configIAMRoleOptions := options.IAMRoleOptions{
RoleARN: conf.IamRole,
AssumeRoleSessionName: conf.IamAssumeRoleSessionName,
}
if conf.IamAssumeRoleDuration != nil {
configIAMRoleOptions.AssumeRoleDuration = *conf.IamAssumeRoleDuration
}
return configIAMRoleOptions
}
// terragruntConfigFile represents the configuration supported in a Terragrunt configuration file (i.e.
// terragrunt.hcl)
type terragruntConfigFile struct {
Terraform *TerraformConfig `hcl:"terraform,block"`
TerraformBinary *string `hcl:"terraform_binary,attr"`
TerraformVersionConstraint *string `hcl:"terraform_version_constraint,attr"`
TerragruntVersionConstraint *string `hcl:"terragrunt_version_constraint,attr"`
Inputs *cty.Value `hcl:"inputs,attr"`
// We allow users to configure remote state (backend) via blocks:
//
// remote_state {
// backend = "s3"
// config = { ... }
// }
//
// Or as attributes:
//
// remote_state = {
// backend = "s3"
// config = { ... }
// }
RemoteState *remoteStateConfigFile `hcl:"remote_state,block"`
RemoteStateAttr *cty.Value `hcl:"remote_state,optional"`
Dependencies *ModuleDependencies `hcl:"dependencies,block"`
DownloadDir *string `hcl:"download_dir,attr"`
PreventDestroy *bool `hcl:"prevent_destroy,attr"`
Skip *bool `hcl:"skip,attr"`
IamRole *string `hcl:"iam_role,attr"`
IamAssumeRoleDuration *int64 `hcl:"iam_assume_role_duration,attr"`
IamAssumeRoleSessionName *string `hcl:"iam_assume_role_session_name,attr"`
TerragruntDependencies []Dependency `hcl:"dependency,block"`
// We allow users to configure code generation via blocks:
//
// generate "example" {
// path = "example.tf"
// contents = "example"
// }
//
// Or via attributes:
//
// generate = {
// example = {
// path = "example.tf"
// contents = "example"
// }
// }
GenerateAttrs *cty.Value `hcl:"generate,optional"`
GenerateBlocks []terragruntGenerateBlock `hcl:"generate,block"`
RetryableErrors []string `hcl:"retryable_errors,optional"`
RetryMaxAttempts *int `hcl:"retry_max_attempts,optional"`
RetrySleepIntervalSec *int `hcl:"retry_sleep_interval_sec,optional"`
// This struct is used for validating and parsing the entire terragrunt config. Since locals and include are
// evaluated in a completely separate cycle, it should not be evaluated here. Otherwise, we can't support self
// referencing other elements in the same block.
// We don't want to use the special Remain keyword here, as that would cause the checker to support parsing config
// that have extraneous, unsupported blocks and attributes.
Locals *terragruntLocal `hcl:"locals,block"`
Include []terragruntIncludeIgnore `hcl:"include,block"`
}
// We use a struct designed to not parse the block, as locals and includes are parsed and decoded using a special
// routine that allows references to the other locals in the same block.
type terragruntLocal struct {
Remain hcl.Body `hcl:",remain"`
}
type terragruntIncludeIgnore struct {
Name string `hcl:"name,label"`
Remain hcl.Body `hcl:",remain"`
}
// Configuration for Terraform remote state as parsed from a terragrunt.hcl config file
type remoteStateConfigFile struct {
Backend string `hcl:"backend,attr"`
DisableInit *bool `hcl:"disable_init,attr"`
DisableDependencyOptimization *bool `hcl:"disable_dependency_optimization,attr"`
Generate *remoteStateConfigGenerate `hcl:"generate,attr"`
Config cty.Value `hcl:"config,attr"`
}
func (remoteState *remoteStateConfigFile) String() string {
return fmt.Sprintf("remoteStateConfigFile{Backend = %v, Config = %v}", remoteState.Backend, remoteState.Config)
}
// Convert the parsed config file remote state struct to the internal representation struct of remote state
// configurations.
func (remoteState *remoteStateConfigFile) toConfig() (*remote.RemoteState, error) {
remoteStateConfig, err := parseCtyValueToMap(remoteState.Config)
if err != nil {
return nil, err
}
config := &remote.RemoteState{}
config.Backend = remoteState.Backend
if remoteState.Generate != nil {
config.Generate = &remote.RemoteStateGenerate{
Path: remoteState.Generate.Path,
IfExists: remoteState.Generate.IfExists,
}
}
config.Config = remoteStateConfig
if remoteState.DisableInit != nil {
config.DisableInit = *remoteState.DisableInit
}
if remoteState.DisableDependencyOptimization != nil {
config.DisableDependencyOptimization = *remoteState.DisableDependencyOptimization
}
config.FillDefaults()
if err := config.Validate(); err != nil {
return nil, err
}
return config, err
}
type remoteStateConfigGenerate struct {
// We use cty instead of hcl, since we are using this type to convert an attr and not a block.
Path string `cty:"path"`
IfExists string `cty:"if_exists"`
}
// Struct used to parse generate blocks. This will later be converted to GenerateConfig structs so that we can go
// through the codegen routine.
type terragruntGenerateBlock struct {
Name string `hcl:",label"`
Path string `hcl:"path,attr" mapstructure:"path"`
IfExists string `hcl:"if_exists,attr" mapstructure:"if_exists"`
CommentPrefix *string `hcl:"comment_prefix,attr" mapstructure:"comment_prefix"`
Contents string `hcl:"contents,attr" mapstructure:"contents"`
DisableSignature *bool `hcl:"disable_signature,attr" mapstructure:"disable_signature"`
Disable *bool `hcl:"disable,attr" mapstructure:"disable"`
}
type IncludeConfigs map[string]IncludeConfig
// ContainsPath returns true if the given path is contained in at least one configuration.
func (cfgs IncludeConfigs) ContainsPath(path string) bool {
for _, cfg := range cfgs {
if cfg.Path == path {
return true
}
}
return false
}
// IncludeConfig represents the configuration settings for a parent Terragrunt configuration file that you can
// include into a child Terragrunt configuration file. You can have more than one include config.
type IncludeConfig struct {
Name string `hcl:"name,label"`
Path string `hcl:"path,attr"`
Expose *bool `hcl:"expose,attr"`
MergeStrategy *string `hcl:"merge_strategy,attr"`
}
func (cfg *IncludeConfig) String() string {
return fmt.Sprintf("IncludeConfig{Path = %s, Expose = %v, MergeStrategy = %v}", cfg.Path, cfg.Expose, cfg.MergeStrategy)
}
func (cfg *IncludeConfig) GetExpose() bool {
if cfg == nil || cfg.Expose == nil {
return false
}
return *cfg.Expose
}
func (cfg *IncludeConfig) GetMergeStrategy() (MergeStrategyType, error) {
if cfg.MergeStrategy == nil {
return ShallowMerge, nil
}
strategy := *cfg.MergeStrategy
switch strategy {
case string(NoMerge):
return NoMerge, nil
case string(ShallowMerge):
return ShallowMerge, nil
case string(DeepMerge):
return DeepMerge, nil
case string(DeepMergeMapOnly):
return DeepMergeMapOnly, nil
default:
return NoMerge, errors.WithStackTrace(InvalidMergeStrategyType(strategy))
}
}
type MergeStrategyType string
const (
NoMerge MergeStrategyType = "no_merge"
ShallowMerge MergeStrategyType = "shallow"
DeepMerge MergeStrategyType = "deep"
DeepMergeMapOnly MergeStrategyType = "deep_map_only"
)
// ModuleDependencies represents the paths to other Terraform modules that must be applied before the current module
// can be applied
type ModuleDependencies struct {
Paths []string `hcl:"paths,attr" cty:"paths"`
}
// Merge appends the paths in the provided ModuleDependencies object into this ModuleDependencies object.
func (deps *ModuleDependencies) Merge(source *ModuleDependencies) {
if source == nil {
return
}
for _, path := range source.Paths {
if !util.ListContainsElement(deps.Paths, path) {
deps.Paths = append(deps.Paths, path)
}
}
}
func (deps *ModuleDependencies) String() string {
return fmt.Sprintf("ModuleDependencies{Paths = %v}", deps.Paths)
}
// Hook specifies terraform commands (apply/plan) and array of os commands to execute
type Hook struct {
Name string `hcl:"name,label" cty:"name"`
Commands []string `hcl:"commands,attr" cty:"commands"`
Execute []string `hcl:"execute,attr" cty:"execute"`
RunOnError *bool `hcl:"run_on_error,attr" cty:"run_on_error"`
SuppressStdout *bool `hcl:"suppress_stdout,attr" cty:"suppress_stdout"`
WorkingDir *string `hcl:"working_dir,attr" cty:"working_dir"`
}
type ErrorHook struct {
Name string `hcl:"name,label" cty:"name"`
Commands []string `hcl:"commands,attr" cty:"commands"`
Execute []string `hcl:"execute,attr" cty:"execute"`
OnErrors []string `hcl:"on_errors,attr" cty:"on_errors"`
SuppressStdout *bool `hcl:"suppress_stdout,attr" cty:"suppress_stdout"`
WorkingDir *string `hcl:"working_dir,attr" cty:"working_dir"`
}
func (conf *Hook) String() string {
return fmt.Sprintf("Hook{Name = %s, Commands = %v}", conf.Name, len(conf.Commands))
}
func (conf *ErrorHook) String() string {
return fmt.Sprintf("Hook{Name = %s, Commands = %v}", conf.Name, len(conf.Commands))
}
// TerraformConfig specifies where to find the Terraform configuration files
// NOTE: If any attributes or blocks are added here, be sure to add it to ctyTerraformConfig in config_as_cty.go as
// well.
type TerraformConfig struct {
ExtraArgs []TerraformExtraArguments `hcl:"extra_arguments,block"`
Source *string `hcl:"source,attr"`
BeforeHooks []Hook `hcl:"before_hook,block"`
AfterHooks []Hook `hcl:"after_hook,block"`
ErrorHooks []ErrorHook `hcl:"error_hook,block"`
// Ideally we can avoid the pointer to list slice, but if it is not a pointer, Terraform requires the attribute to
// be defined and we want to make this optional.
IncludeInCopy *[]string `hcl:"include_in_copy,attr"`
}
func (conf *TerraformConfig) String() string {
return fmt.Sprintf("TerraformConfig{Source = %v}", conf.Source)
}
func (conf *TerraformConfig) GetBeforeHooks() []Hook {
if conf == nil {
return nil
}
return conf.BeforeHooks
}
func (conf *TerraformConfig) GetAfterHooks() []Hook {
if conf == nil {
return nil
}
return conf.AfterHooks
}
func (conf *TerraformConfig) GetErrorHooks() []ErrorHook {
if conf == nil {
return nil
}
return conf.ErrorHooks
}
func (conf *TerraformConfig) ValidateHooks() error {
beforeAndAfterHooks := append(conf.GetBeforeHooks(), conf.GetAfterHooks()...)
for _, curHook := range beforeAndAfterHooks {
if len(curHook.Execute) < 1 || curHook.Execute[0] == "" {
return InvalidArgError(fmt.Sprintf("Error with hook %s. Need at least one non-empty argument in 'execute'.", curHook.Name))
}
}
for _, curHook := range conf.GetErrorHooks() {
if len(curHook.Execute) < 1 || curHook.Execute[0] == "" {
return InvalidArgError(fmt.Sprintf("Error with hook %s. Need at least one non-empty argument in 'execute'.", curHook.Name))
}
}
return nil
}
// TerraformExtraArguments sets a list of arguments to pass to Terraform if command fits any in the `Commands` list
type TerraformExtraArguments struct {
Name string `hcl:"name,label" cty:"name"`
Arguments *[]string `hcl:"arguments,attr" cty:"arguments"`
RequiredVarFiles *[]string `hcl:"required_var_files,attr" cty:"required_var_files"`
OptionalVarFiles *[]string `hcl:"optional_var_files,attr" cty:"optional_var_files"`
Commands []string `hcl:"commands,attr" cty:"commands"`
EnvVars *map[string]string `hcl:"env_vars,attr" cty:"env_vars"`
}
func (conf *TerraformExtraArguments) String() string {
return fmt.Sprintf(
"TerraformArguments{Name = %s, Arguments = %v, Commands = %v, EnvVars = %v}",
conf.Name,
conf.Arguments,
conf.Commands,
conf.EnvVars)
}
func (conf *TerraformExtraArguments) GetVarFiles(logger *logrus.Entry) []string {
var varFiles []string
// Include all specified RequiredVarFiles.
if conf.RequiredVarFiles != nil {
varFiles = append(varFiles, util.RemoveDuplicatesFromListKeepLast(*conf.RequiredVarFiles)...)
}
// If OptionalVarFiles is specified, check for each file if it exists and if so, include in the var
// files list. Note that it is possible that many files resolve to the same path, so we remove
// duplicates.
if conf.OptionalVarFiles != nil {
for _, file := range util.RemoveDuplicatesFromListKeepLast(*conf.OptionalVarFiles) {
if util.FileExists(file) {
varFiles = append(varFiles, file)
} else {
logger.Debugf("Skipping var-file %s as it does not exist", file)
}
}
}
return varFiles
}
// There are two ways a user can tell Terragrunt that it needs to download Terraform configurations from a specific
// URL: via a command-line option or via an entry in the Terragrunt configuration. If the user used one of these, this
// method returns the source URL or an empty string if there is no source url
func GetTerraformSourceUrl(terragruntOptions *options.TerragruntOptions, terragruntConfig *TerragruntConfig) (string, error) {
switch {
case terragruntOptions.Source != "":
return terragruntOptions.Source, nil
case terragruntConfig.Terraform != nil && terragruntConfig.Terraform.Source != nil:
return adjustSourceWithMap(terragruntOptions.SourceMap, *terragruntConfig.Terraform.Source, terragruntOptions.OriginalTerragruntConfigPath)
default:
return "", nil
}
}
// adjustSourceWithMap implements the --terragrunt-source-map feature. This function will check if the URL portion of a
// terraform source matches any entry in the provided source map and if it does, replace it with the configured source
// in the map. Note that this only performs literal matches with the URL portion.
//
// Example:
// Suppose terragrunt is called with:
//
// --terragrunt-source-map git::ssh://git@github.com/gruntwork-io/i-dont-exist.git=/path/to/local-modules
//
// and the terraform source is:
//
// git::ssh://git@github.com/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/app?ref=master
//
// This function will take that source and transform it to:
//
// /path/to/local-modules/fixture-source-map/modules/app
func adjustSourceWithMap(sourceMap map[string]string, source string, modulePath string) (string, error) {
// Skip logic if source map is not configured
if len(sourceMap) == 0 {
return source, nil
}
// use go-getter to split the module source string into a valid URL and subdirectory (if // is present)
moduleUrl, moduleSubdir := getter.SourceDirSubdir(source)
// if both URL and subdir are missing, something went terribly wrong
if moduleUrl == "" && moduleSubdir == "" {
return "", errors.WithStackTrace(InvalidSourceUrlWithMap{ModulePath: modulePath, ModuleSourceUrl: source})
}
// If module URL is missing, return the source as is as it will not match anything in the map.
if moduleUrl == "" {
return source, nil
}
// Before looking up in sourceMap, make sure to drop any query parameters.
moduleUrlParsed, err := url.Parse(moduleUrl)
if err != nil {
return source, err
}
moduleUrlParsed.RawQuery = ""
moduleUrlQuery := moduleUrlParsed.String()
// Check if there is an entry to replace the URL portion in the map. Return the source as is if there is no entry in
// the map.
sourcePath, hasKey := sourceMap[moduleUrlQuery]
if !hasKey {
return source, nil
}
// Since there is a source mapping, replace the module URL portion with the entry in the map, and join with the
// subdir.
// If subdir is missing, check if we can obtain a valid module name from the URL portion.
if moduleSubdir == "" {
moduleSubdirFromUrl, err := getModulePathFromSourceUrl(moduleUrl)
if err != nil {
return moduleSubdirFromUrl, err
}
moduleSubdir = moduleSubdirFromUrl
}
return util.JoinTerraformModulePath(sourcePath, moduleSubdir), nil
}
// Return the default path to use for the Terragrunt configuration that exists within the path giving preference to `terragrunt.hcl`
func GetDefaultConfigPath(workingDir string) string {
// check if a configuration file was passed as `workingDir`.
if !files.IsDir(workingDir) && files.FileExists(workingDir) {
return workingDir
}
var configPath string
for _, configPath = range DefaultTerragruntConfigPaths {
if !filepath.IsAbs(configPath) {
configPath = util.JoinPath(workingDir, configPath)
}
if files.FileExists(configPath) {
break
}
}
return configPath
}
// Returns a list of all Terragrunt config files in the given path or any subfolder of the path. A file is a Terragrunt
// config file if it has a name as returned by the DefaultConfigPath method
func FindConfigFilesInPath(rootPath string, terragruntOptions *options.TerragruntOptions) ([]string, error) {
configFiles := []string{}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if ok, err := isTerragruntModuleDir(path, terragruntOptions); err != nil {
return err
} else if !ok {
return filepath.SkipDir
}
for _, configFile := range append(DefaultTerragruntConfigPaths, filepath.Base(terragruntOptions.TerragruntConfigPath)) {
if !filepath.IsAbs(configFile) {
configFile = util.JoinPath(path, configFile)
}
if !util.IsDir(configFile) && util.FileExists(configFile) {
configFiles = append(configFiles, configFile)
break
}
}
return nil
})
return configFiles, err
}
// isTerragruntModuleDir returns true if the given path contains a Terragrunt module and false otherwise. The path
// can not contain a cache, data, or download dir.
func isTerragruntModuleDir(path string, terragruntOptions *options.TerragruntOptions) (bool, error) {
// Skip the Terragrunt cache dir
if util.ContainsPath(path, util.TerragruntCacheDir) {
return false, nil
}
// Skip the Terraform data dir
dataDir := terragruntOptions.TerraformDataDir()
if filepath.IsAbs(dataDir) {
if util.HasPathPrefix(path, dataDir) {
return false, nil
}
} else {
if util.ContainsPath(path, dataDir) {
return false, nil
}
}
canonicalPath, err := util.CanonicalPath(path, "")
if err != nil {
return false, err
}
canonicalDownloadPath, err := util.CanonicalPath(terragruntOptions.DownloadDir, "")
if err != nil {
return false, err
}
// Skip any custom download dir specified by the user
if strings.Contains(canonicalPath, canonicalDownloadPath) {
return false, nil
}
return true, nil
}
// Read the Terragrunt config file from its default location
func ReadTerragruntConfig(terragruntOptions *options.TerragruntOptions) (*TerragruntConfig, error) {
terragruntOptions.Logger.Debugf("Reading Terragrunt config file at %s", terragruntOptions.TerragruntConfigPath)
return ParseConfigFile(terragruntOptions.TerragruntConfigPath, terragruntOptions, nil, nil)
}
// Parse the Terragrunt config file at the given path. If the include parameter is not nil, then treat this as a config
// included in some other config file when resolving relative paths.
func ParseConfigFile(filename string, terragruntOptions *options.TerragruntOptions, include *IncludeConfig, dependencyOutputs *cty.Value) (*TerragruntConfig, error) {
configString, err := util.ReadFileAsString(filename)
if err != nil {
return nil, err
}
config, err := ParseConfigString(configString, terragruntOptions, include, filename, dependencyOutputs)
if err != nil {
return nil, err
}
return config, nil
}
// Parse the Terragrunt config contained in the given string and merge it with the given include config (if any). Note
// that the config parsing consists of multiple stages so as to allow referencing of data resulting from parsing
// previous config. The parsing order is:
// 1. Parse include. Include is parsed first and is used to import another config. All the config in the include block is
// then merged into the current TerragruntConfig, except for locals (by design). Note that since the include block is
// parsed first, you cannot reference locals in the include block config.
// 2. Parse locals. Since locals are parsed next, you can only reference other locals in the locals block. Although it
// is possible to merge locals from a config imported with an include block, we do not do that here to avoid
// complicated referencing issues. Please refer to the globals proposal for an alternative that allows merging from
// included config: https://github.com/gruntwork-io/terragrunt/issues/814
// Allowed References:
// - locals
// 3. Parse dependency blocks. This includes running `terragrunt output` to fetch the output data from another
// terragrunt config, so that it is accessible within the config. See PartialParseConfigString for a way to parse the
// blocks but avoid decoding.
// Note that this step is skipped if we already retrieved all the dependencies (which is the case when parsing
// included config files). This is determined by the dependencyOutputs input parameter.
// Allowed References:
// - locals
// 4. Parse everything else. At this point, all the necessary building blocks for parsing the rest of the config are
// available, so parse the rest of the config.
// Allowed References:
// - locals
// - dependency
// 5. Merge the included config with the parsed config. Note that all the config data is mergable except for `locals`
// blocks, which are only scoped to be available within the defining config.
func ParseConfigString(
configString string,
terragruntOptions *options.TerragruntOptions,
includeFromChild *IncludeConfig,
filename string,
dependencyOutputs *cty.Value,
) (*TerragruntConfig, error) {
// Parse the HCL string into an AST body that can be decoded multiple times later without having to re-parse
parser := hclparse.NewParser()
file, err := parseHcl(parser, configString, filename)
if err != nil {
return nil, err
}
// Initial evaluation of configuration to load flags like IamRole which will be used for final parsing
// https://github.com/gruntwork-io/terragrunt/issues/667
if err := setIAMRole(configString, terragruntOptions, includeFromChild, filename); err != nil {
return nil, err
}
// Decode just the Base blocks. See the function docs for DecodeBaseBlocks for more info on what base blocks are.
localsAsCty, trackInclude, err := DecodeBaseBlocks(terragruntOptions, parser, file, filename, includeFromChild, nil)
if err != nil {
return nil, err
}
// Initialize evaluation context extensions from base blocks.
contextExtensions := EvalContextExtensions{
Locals: localsAsCty,
TrackInclude: trackInclude,
DecodedDependencies: dependencyOutputs,
}
if dependencyOutputs == nil {
// Decode just the `dependency` blocks, retrieving the outputs from the target terragrunt config in the
// process.
retrievedOutputs, err := decodeAndRetrieveOutputs(file, filename, terragruntOptions, trackInclude, contextExtensions)
if err != nil {
return nil, err
}
contextExtensions.DecodedDependencies = retrievedOutputs
}
// Decode the rest of the config, passing in this config's `include` block or the child's `include` block, whichever
// is appropriate
terragruntConfigFile, err := decodeAsTerragruntConfigFile(file, filename, terragruntOptions, contextExtensions)
if err != nil {
return nil, err
}
if terragruntConfigFile == nil {
return nil, errors.WithStackTrace(CouldNotResolveTerragruntConfigInFile(filename))
}
config, err := convertToTerragruntConfig(terragruntConfigFile, filename, terragruntOptions, contextExtensions)
if err != nil {
return nil, err
}
// If this file includes another, parse and merge it. Otherwise just return this config.
if trackInclude != nil {
mergedConfig, err := handleInclude(config, trackInclude, terragruntOptions, contextExtensions.DecodedDependencies)
if err != nil {
return nil, err
}
// Saving processed includes into configuration, direct assignment since nested includes aren't supported
mergedConfig.ProcessedIncludes = trackInclude.CurrentMap
// Make sure the top level information that is not automatically merged in is captured on the merged config to
// ensure the proper representation of the config is captured.
// - Locals are deliberately not merged in so that they remain local in scope. Here, we directly set it to the
// original locals for the current config being handled, as that is the locals list that is in scope for this
// config.
mergedConfig.Locals = config.Locals
return mergedConfig, nil
}
return config, nil
}
// iamRoleCache - store for cached values of IAM roles
var iamRoleCache = NewIAMRoleOptionsCache()
// setIAMRole - extract IAM role details from Terragrunt flags block
func setIAMRole(configString string, terragruntOptions *options.TerragruntOptions, includeFromChild *IncludeConfig, filename string) error {
// Prefer the IAM Role CLI args if they were passed otherwise lazily evaluate the IamRoleOptions using the config.
if terragruntOptions.OriginalIAMRoleOptions.RoleARN != "" {
terragruntOptions.IAMRoleOptions = terragruntOptions.OriginalIAMRoleOptions
} else {
// as key is considered HCL code and include configuration
var key = fmt.Sprintf("%v-%v", configString, includeFromChild)
var config, found = iamRoleCache.Get(key)
if !found {
iamConfig, err := TerragruntConfigFromPartialConfigString(configString, terragruntOptions, includeFromChild, filename, []PartialDecodeSectionType{TerragruntFlags})
if err != nil {
return err
}
config = iamConfig.GetIAMRoleOptions()
iamRoleCache.Put(key, config)
}
// We merge the OriginalIAMRoleOptions into the one from the config, because the CLI passed IAMRoleOptions has
// precedence.
terragruntOptions.IAMRoleOptions = options.MergeIAMRoleOptions(
config,
terragruntOptions.OriginalIAMRoleOptions,
)
}
return nil
}
func decodeAsTerragruntConfigFile(
file *hcl.File,
filename string,
terragruntOptions *options.TerragruntOptions,
extensions EvalContextExtensions,
) (*terragruntConfigFile, error) {
terragruntConfig := terragruntConfigFile{}
err := decodeHcl(file, filename, &terragruntConfig, terragruntOptions, extensions)
// in case of render-json command and inputs reference error, we update the inputs with default value
if diagErr, ok := err.(hcl.Diagnostics); ok && isRenderJsonCommand(terragruntOptions) && isAttributeAccessError(diagErr) {
terragruntOptions.Logger.Warnf("Failed to decode inputs %v", diagErr)
// update unknown inputs with default value
updatedValue := map[string]cty.Value{}
for key, value := range terragruntConfig.Inputs.AsValueMap() {
if value.IsKnown() {
updatedValue[key] = value
} else {
updatedValue[key] = cty.StringVal("")
}
}
value, err := gocty.ToCtyValue(updatedValue, terragruntConfig.Inputs.Type())
if err != nil {
return nil, err
}
terragruntConfig.Inputs = &value
return &terragruntConfig, nil
}
if err != nil {
return nil, err
}
return &terragruntConfig, nil
}
// Returns the index of the Hook with the given name,
// or -1 if no Hook have the given name.
func getIndexOfHookWithName(hooks []Hook, name string) int {
for i, hook := range hooks {
if hook.Name == name {
return i
}
}
return -1
}
// isAttributeAccessError returns true if the given diagnostics indicate an error accessing an attribute
func isAttributeAccessError(diagnostics hcl.Diagnostics) bool {
for _, diagnostic := range diagnostics {
if diagnostic.Severity == hcl.DiagError && strings.Contains(diagnostic.Summary, "Unsupported attribute") {
return true
}
}
return false
}
// Returns the index of the ErrorHook with the given name,
// or -1 if no Hook have the given name.
// TODO: Figure out more DRY way to do this
func getIndexOfErrorHookWithName(hooks []ErrorHook, name string) int {
for i, hook := range hooks {
if hook.Name == name {
return i
}
}
return -1
}
// Returns the index of the extraArgs with the given name,
// or -1 if no extraArgs have the given name.
func getIndexOfExtraArgsWithName(extraArgs []TerraformExtraArguments, name string) int {
for i, extra := range extraArgs {
if extra.Name == name {
return i
}
}
return -1
}
// Convert the contents of a fully resolved Terragrunt configuration to a TerragruntConfig object
func convertToTerragruntConfig(
terragruntConfigFromFile *terragruntConfigFile,
configPath string,
terragruntOptions *options.TerragruntOptions,
contextExtensions EvalContextExtensions,
) (cfg *TerragruntConfig, err error) {
// The HCL2 parser and especially cty conversions will panic in many types of errors, so we have to recover from
// those panics here and convert them to normal errors
defer func() {
if recovered := recover(); recovered != nil {
err = errors.WithStackTrace(PanicWhileParsingConfig{RecoveredValue: recovered, ConfigFile: configPath})
}
}()
terragruntConfig := &TerragruntConfig{
IsPartial: false,
// Initialize GenerateConfigs so we can append to it
GenerateConfigs: map[string]codegen.GenerateConfig{},
}
defaultMetadata := map[string]interface{}{FoundInFile: configPath}
if terragruntConfigFromFile.RemoteState != nil {
remoteState, err := terragruntConfigFromFile.RemoteState.toConfig()
if err != nil {
return nil, err
}
terragruntConfig.RemoteState = remoteState
terragruntConfig.SetFieldMetadata(MetadataRemoteState, defaultMetadata)
}
if terragruntConfigFromFile.RemoteStateAttr != nil {
remoteStateMap, err := parseCtyValueToMap(*terragruntConfigFromFile.RemoteStateAttr)
if err != nil {
return nil, err
}
var remoteState *remote.RemoteState
if err := mapstructure.Decode(remoteStateMap, &remoteState); err != nil {
return nil, err
}
terragruntConfig.RemoteState = remoteState
terragruntConfig.SetFieldMetadata(MetadataRemoteState, defaultMetadata)
}
if err := terragruntConfigFromFile.Terraform.ValidateHooks(); err != nil {
return nil, err
}
terragruntConfig.Terraform = terragruntConfigFromFile.Terraform
if terragruntConfig.Terraform != nil { // since Terraform is nil each time avoid saving metadata when it is nil
terragruntConfig.SetFieldMetadata(MetadataTerraform, defaultMetadata)
}
if err := validateDependencies(terragruntOptions, terragruntConfigFromFile.Dependencies); err != nil {
return nil, err
}
terragruntConfig.Dependencies = terragruntConfigFromFile.Dependencies
if terragruntConfig.Dependencies != nil {
for _, item := range terragruntConfig.Dependencies.Paths {
terragruntConfig.SetFieldMetadataWithType(MetadataDependencies, item, defaultMetadata)
}
}
terragruntConfig.TerragruntDependencies = terragruntConfigFromFile.TerragruntDependencies
for _, dep := range terragruntConfig.TerragruntDependencies {
terragruntConfig.SetFieldMetadataWithType(MetadataDependency, dep.Name, defaultMetadata)
}
if terragruntConfigFromFile.TerraformBinary != nil {
terragruntConfig.TerraformBinary = *terragruntConfigFromFile.TerraformBinary
terragruntConfig.SetFieldMetadata(MetadataTerraformBinary, defaultMetadata)
}
if terragruntConfigFromFile.RetryableErrors != nil {
terragruntConfig.RetryableErrors = terragruntConfigFromFile.RetryableErrors
terragruntConfig.SetFieldMetadata(MetadataRetryableErrors, defaultMetadata)
}
if terragruntConfigFromFile.RetryMaxAttempts != nil {
terragruntConfig.RetryMaxAttempts = terragruntConfigFromFile.RetryMaxAttempts
terragruntConfig.SetFieldMetadata(MetadataRetryMaxAttempts, defaultMetadata)
}
if terragruntConfigFromFile.RetrySleepIntervalSec != nil {
terragruntConfig.RetrySleepIntervalSec = terragruntConfigFromFile.RetrySleepIntervalSec
terragruntConfig.SetFieldMetadata(MetadataRetrySleepIntervalSec, defaultMetadata)
}
if terragruntConfigFromFile.DownloadDir != nil {
terragruntConfig.DownloadDir = *terragruntConfigFromFile.DownloadDir
terragruntConfig.SetFieldMetadata(MetadataDownloadDir, defaultMetadata)
}
if terragruntConfigFromFile.TerraformVersionConstraint != nil {
terragruntConfig.TerraformVersionConstraint = *terragruntConfigFromFile.TerraformVersionConstraint
terragruntConfig.SetFieldMetadata(MetadataTerraformVersionConstraint, defaultMetadata)
}
if terragruntConfigFromFile.TerragruntVersionConstraint != nil {
terragruntConfig.TerragruntVersionConstraint = *terragruntConfigFromFile.TerragruntVersionConstraint
terragruntConfig.SetFieldMetadata(MetadataTerragruntVersionConstraint, defaultMetadata)
}
if terragruntConfigFromFile.PreventDestroy != nil {
terragruntConfig.PreventDestroy = terragruntConfigFromFile.PreventDestroy
terragruntConfig.SetFieldMetadata(MetadataPreventDestroy, defaultMetadata)
}
if terragruntConfigFromFile.Skip != nil {
terragruntConfig.Skip = *terragruntConfigFromFile.Skip
terragruntConfig.SetFieldMetadata(MetadataSkip, defaultMetadata)
}
if terragruntConfigFromFile.IamRole != nil {
terragruntConfig.IamRole = *terragruntConfigFromFile.IamRole
terragruntConfig.SetFieldMetadata(MetadataIamRole, defaultMetadata)
}
if terragruntConfigFromFile.IamAssumeRoleDuration != nil {