@@ -1051,12 +1051,13 @@ func getTerragruntOutputJSON(ctx context.Context, pctx *ParsingContext, l log.Lo
10511051 }
10521052
10531053 if isInit {
1054- credsGetter := creds . NewGetter ( )
1055- if err = credsGetter .ObtainAndUpdateEnvIfNecessary (
1054+ mergedIAM := iam . MergeRoleOptions ( remoteStateTGConfig . GetIAMRoleOptions (), pctx . OriginalIAMRoleOptions )
1055+ if err = creds . NewGetter () .ObtainAndUpdateEnvIfNecessary (
10561056 ctx ,
10571057 l ,
10581058 pctx .Env ,
10591059 externalcmd .NewProvider (l , pctx .AuthProviderCmd , shellRunOptsFromPctx (pctx )),
1060+ amazonsts .NewProvider (l , mergedIAM , pctx .Env ),
10601061 ); err != nil {
10611062 return nil , err
10621063 }
@@ -1066,8 +1067,6 @@ func getTerragruntOutputJSON(ctx context.Context, pctx *ParsingContext, l log.Lo
10661067 pctx ,
10671068 l ,
10681069 workingDir ,
1069- remoteStateTGConfig .GetIAMRoleOptions (),
1070- credsGetter ,
10711070 )
10721071 }
10731072
@@ -1120,28 +1119,17 @@ func terragruntAlreadyInit(ctx context.Context, l log.Logger, pctx *ParsingConte
11201119}
11211120
11221121// getTerragruntOutputJSONFromInitFolder will retrieve the outputs directly from the module's working directory without
1123- // running init.
1122+ // running init. Callers must populate pctx.Env with auth-provider-cmd credentials and any
1123+ // TG_IAM_ROLE assumption beforehand.
11241124func getTerragruntOutputJSONFromInitFolder (
11251125 ctx context.Context ,
11261126 pctx * ParsingContext ,
11271127 l log.Logger ,
11281128 terraformWorkingDir string ,
1129- iamRoleOpts iam.RoleOptions ,
1130- credsGetter * creds.Getter ,
11311129) ([]byte , error ) {
11321130 targetConfigPath := pctx .TerragruntConfigPath
11331131
1134- tfRunOpts , err := setupTFRunOptsForBareTerraform (
1135- ctx ,
1136- pctx ,
1137- l ,
1138- terraformWorkingDir ,
1139- iamRoleOpts ,
1140- credsGetter ,
1141- )
1142- if err != nil {
1143- return nil , err
1144- }
1132+ tfRunOpts := setupTFRunOptsForBareTerraform (pctx , terraformWorkingDir )
11451133
11461134 l .Debugf (
11471135 "Unit '%s' is already init-ed. " +
@@ -1216,27 +1204,18 @@ func getTerragruntOutputJSONFromRemoteState(
12161204
12171205 l .Debugf ("Setting dependency working directory to %s" , tempWorkDir )
12181206
1219- credsGetter := creds . NewGetter ( )
1220- if err = credsGetter .ObtainAndUpdateEnvIfNecessary (
1207+ mergedIAM := iam . MergeRoleOptions ( iamRoleOpts , pctx . OriginalIAMRoleOptions )
1208+ if err = creds . NewGetter () .ObtainAndUpdateEnvIfNecessary (
12211209 ctx ,
12221210 l ,
12231211 pctx .Env ,
12241212 externalcmd .NewProvider (l , pctx .AuthProviderCmd , shellRunOptsFromPctx (pctx )),
1213+ amazonsts .NewProvider (l , mergedIAM , pctx .Env ),
12251214 ); err != nil {
12261215 return nil , err
12271216 }
12281217
1229- tfRunOpts , err := setupTFRunOptsForBareTerraform (
1230- ctx ,
1231- pctx ,
1232- l ,
1233- tempWorkDir ,
1234- iamRoleOpts ,
1235- credsGetter ,
1236- )
1237- if err != nil {
1238- return nil , err
1239- }
1218+ tfRunOpts := setupTFRunOptsForBareTerraform (pctx , tempWorkDir )
12401219
12411220 // To speed up dependencies processing it is possible to retrieve its output directly from the backend without init dependencies
12421221 if pctx .Experiments .Evaluate (experiment .DependencyFetchOutputFromState ) && ! pctx .NoDependencyFetchOutputFromState {
@@ -1361,38 +1340,20 @@ func getTerragruntOutputJSONFromRemoteStateS3(ctx context.Context, l log.Logger,
13611340 return jsonOutputs , nil
13621341}
13631342
1364- // setupTFRunOptsForBareTerraform builds a *tf.RunOptions that can be used to run terraform
1365- // without going through the full RunTerragrunt operation. It merges IAM roles and obtains
1366- // credentials inline.
1367- func setupTFRunOptsForBareTerraform (
1368- ctx context.Context ,
1369- pctx * ParsingContext ,
1370- l log.Logger ,
1371- workingDir string ,
1372- iamRoleOpts iam.RoleOptions ,
1373- credsGetter * creds.Getter ,
1374- ) (* tf.TFOptions , error ) {
1375- // Merge IAM options
1376- mergedIAM := iam .MergeRoleOptions (iamRoleOpts , pctx .OriginalIAMRoleOptions )
1377-
1378- // Build shell.RunOptions for this specific working dir with io.Discard as writer
1343+ // setupTFRunOptsForBareTerraform builds a *tf.TFOptions for running terraform without
1344+ // going through the full RunTerragrunt operation. Callers must obtain credentials
1345+ // (auth-provider-cmd and any TG_IAM_ROLE assumption) before invoking, so those steps
1346+ // run from the unit's directory rather than the bare-terraform working directory.
1347+ func setupTFRunOptsForBareTerraform (pctx * ParsingContext , workingDir string ) * tf.TFOptions {
13791348 shellOpts := shellRunOptsFromPctx (pctx )
13801349 shellOpts .WorkingDir = workingDir
13811350 shellOpts .Writers .Writer = io .Discard
13821351
1383- // Make sure to assume any roles set by TG_IAM_ROLE
1384- if err := credsGetter .ObtainAndUpdateEnvIfNecessary (ctx , l , pctx .Env ,
1385- externalcmd .NewProvider (l , pctx .AuthProviderCmd , shellOpts ),
1386- amazonsts .NewProvider (l , mergedIAM , pctx .Env ),
1387- ); err != nil {
1388- return nil , err
1389- }
1390-
13911352 return & tf.TFOptions {
13921353 JSONLogFormat : pctx .JSONLogFormat ,
13931354 OriginalTerragruntConfigPath : pctx .OriginalTerragruntConfigPath ,
13941355 ShellOptions : shellOpts ,
1395- }, nil
1356+ }
13961357}
13971358
13981359// runTerragruntOutputJSON uses terragrunt running functions to extract the json output from the target config.
0 commit comments