@@ -40,12 +40,19 @@ type accountOnboardingStackInput struct {
40
40
onboardingCft string
41
41
stackId string
42
42
region string
43
+ profile string
43
44
}
44
45
45
46
// CloudFormationClient returns a AWS cloudformation client by assuming the CFT role in the specified account.
46
- func CloudFormationClient (ctx context.Context , accountId string , cftRoleName string , region string ) (* cloudformation.Client , error ) {
47
+ func CloudFormationClient (ctx context.Context , accountId , cftRoleName , region , profile string ) (* cloudformation.Client , error ) {
47
48
cftRoleArn := fmt .Sprintf ("arn:aws:iam::%s:role/%s" , accountId , cftRoleName )
48
- cfg , err := config .LoadDefaultConfig (ctx , config .WithRegion (region ))
49
+ options := []func (* config.LoadOptions ) error {
50
+ config .WithRegion (region ),
51
+ }
52
+ if profile != "" {
53
+ options = append (options , config .WithSharedConfigProfile (profile ))
54
+ }
55
+ cfg , err := config .LoadDefaultConfig (ctx , options ... )
49
56
if err != nil {
50
57
tflog .Info (ctx , "error: %s" , err )
51
58
return nil , err
@@ -95,7 +102,7 @@ func FindStackByName(ctx context.Context, name string, nextToken *string,
95
102
}
96
103
97
104
func CreateAccountOnboardingStack (ctx context.Context , input accountOnboardingStackInput ) (string , error ) {
98
- cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region )
105
+ cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region , input . profile )
99
106
if err != nil {
100
107
tflog .Info (ctx , "error: %s" , err )
101
108
return "" , err
@@ -232,7 +239,7 @@ func WaitForStackDeletion(ctx context.Context, svc *cloudformation.Client, stack
232
239
}
233
240
234
241
func DeleteStack (ctx context.Context , input accountOnboardingStackInput ) error {
235
- cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region )
242
+ cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region , input . profile )
236
243
if err != nil {
237
244
tflog .Info (ctx , "error: %s" , err )
238
245
return err
@@ -254,7 +261,7 @@ func DeleteStack(ctx context.Context, input accountOnboardingStackInput) error {
254
261
}
255
262
256
263
func ReadStack (ctx context.Context , input accountOnboardingStackInput ) (string , error ) {
257
- cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region )
264
+ cfrClient , err := CloudFormationClient (ctx , input .accountId , input .cftRoleName , input .region , input . profile )
258
265
if err != nil {
259
266
tflog .Info (ctx , "error: %s" , err )
260
267
return "" , err
@@ -293,6 +300,7 @@ func resourceAccountOnboardingStack() *schema.Resource {
293
300
func createAccountOnboardingStack (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
294
301
svc := meta .(* api.ApiClient )
295
302
mpRegion := svc .GetMPRegion (ctx )
303
+ profile := svc .GetProfile (ctx )
296
304
accountId := d .Get ("account_id" ).(string )
297
305
stackInput := accountOnboardingStackInput {
298
306
auditLogGroup : d .Get ("auditlog_group" ).(string ),
@@ -309,6 +317,7 @@ func createAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
309
317
snsTopicArn : d .Get ("sns_topic_arn" ).(string ),
310
318
accountId : accountId ,
311
319
region : mpRegion ,
320
+ profile : profile ,
312
321
}
313
322
stackId , err := CreateAccountOnboardingStack (ctx , stackInput )
314
323
if err != nil {
@@ -323,12 +332,14 @@ func createAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
323
332
func deleteAccountOnboardingStack (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
324
333
svc := meta .(* api.ApiClient )
325
334
mpRegion := svc .GetMPRegion (ctx )
335
+ profile := svc .GetProfile (ctx )
326
336
accountId := d .Get ("account_id" ).(string )
327
337
stackInput := accountOnboardingStackInput {
328
338
cftRoleName : d .Get ("cft_role_name" ).(string ),
329
339
stackId : d .Get ("stack_id" ).(string ),
330
340
accountId : accountId ,
331
341
region : mpRegion ,
342
+ profile : profile ,
332
343
}
333
344
err := DeleteStack (ctx , stackInput )
334
345
if err != nil {
@@ -341,13 +352,15 @@ func deleteAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
341
352
func readAccountOnboardingStack (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
342
353
svc := meta .(* api.ApiClient )
343
354
mpRegion := svc .GetMPRegion (ctx )
355
+ profile := svc .GetProfile (ctx )
344
356
accountId := d .Get ("account_id" ).(string )
345
357
stackId := d .Get ("stack_id" ).(string )
346
358
stackInput := accountOnboardingStackInput {
347
359
cftRoleName : d .Get ("cft_role_name" ).(string ),
348
360
stackId : stackId ,
349
361
accountId : accountId ,
350
362
region : mpRegion ,
363
+ profile : profile ,
351
364
}
352
365
stackStatus , err := ReadStack (ctx , stackInput )
353
366
if err != nil {
0 commit comments