/
aws_config.go
44 lines (36 loc) · 1.24 KB
/
aws_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
/*
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
package awsconfig
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"log"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
)
func GetSdkConfig(ctx context.Context) aws.Config {
setRetryMode := func(configuration *config.LoadOptions) error {
configuration.RetryMaxAttempts = 3
configuration.RetryMode = aws.RetryModeStandard
return nil
}
configuration, err := config.LoadDefaultConfig(ctx, setRetryMode)
if err != nil {
log.Fatal("failed to initialize AWS SDK Configuration")
}
return configuration
}
func GetSdkConfigWithRoleArn(ctx context.Context, initialConfig aws.Config, launchRoleArn string) (aws.Config, error) {
// Create an STS client with the initial config
stsClient := sts.NewFromConfig(initialConfig)
// Create a new credential provider that will assume the IAM Role provided by the launchRoleArn parameter
assumeRoleProvider := stscreds.NewAssumeRoleProvider(stsClient, launchRoleArn)
// Create a new configuration with the assume role credential provider
return config.LoadDefaultConfig(
ctx,
config.WithCredentialsProvider(assumeRoleProvider),
)
}