Skip to content

Commit 017c942

Browse files
committed
refactor AWSSecretsManager to check region configuration after loading SDK config
1 parent d9665c9 commit 017c942

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

book/src/lib/client/aws_secrets_manager.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ Creating a new instance is straight-forward. You should either use environment v
1010
> [!NOTE]
1111
> Environment variables take precedence over shared credentials.
1212
13-
## Using environment variables
14-
You can pass required configuration as following environment variables:
15-
* `AWS_ACCESS_KEY_ID`
16-
* `AWS_SECRET_ACCESS_KEY`
17-
* `AWS_REGION`
13+
Once you have an instance of AWS Secrets Manager you gain access to following functions:
14+
* `CreateSecret(key string, val string, override bool) error`
15+
* `GetSecret(key string) (AWSSecret, error)`
16+
* `RemoveSecret(key string, noRecovery bool) error`
1817

1918
## Using shared credentials
2019
If you have shared credentials stored in `.aws/credentials` file, then the easiest way to configure the client is by setting
@@ -23,11 +22,11 @@ If you have shared credentials stored in `.aws/credentials` file, then the easie
2322
> [!WARNING]
2423
> Remember, that most probably you will need to manually create a new session for that profile before running your application.
2524
26-
2725
> [!NOTE]
2826
> You can read more about configuring the AWS SDK [here](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html).
2927
30-
Once you have an instance of AWS Secrets Manager you gain access to following functions:
31-
* `CreateSecret(key string, val string, override bool) error`
32-
* `GetSecret(key string) (AWSSecret, error)`
33-
* `RemoveSecret(key string, noRecovery bool) error`
28+
## Using environment variables
29+
You can pass required configuration as following environment variables:
30+
* `AWS_ACCESS_KEY_ID`
31+
* `AWS_SECRET_ACCESS_KEY`
32+
* `AWS_REGION`

framework/secretsmanager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ type AWSSecretsManager struct {
6666
// NewAWSSecretsManager create a new connection to AWS Secrets Manager
6767
func NewAWSSecretsManager(requestTimeout time.Duration) (*AWSSecretsManager, error) {
6868
cfg, err := config.LoadDefaultConfig(context.TODO())
69-
region := os.Getenv("AWS_REGION")
70-
if region == "" {
71-
return nil, fmt.Errorf("region is required for AWSSecretsManager, use env variable: export AWS_REGION=...: %w", err)
72-
}
73-
cfg.Region = region
7469
if err != nil {
7570
return nil, fmt.Errorf("unable to load AWS SDK config, %v", err)
7671
}
72+
if cfg.Region == "" {
73+
region := os.Getenv("AWS_REGION")
74+
if region == "" {
75+
return nil, fmt.Errorf("region is required for AWSSecretsManager, use env variable: export AWS_REGION")
76+
}
77+
cfg.Region = region
78+
}
7779
l := log.Logger.With().Str("Component", "AWSSecretsManager").Logger()
7880
l.Info().Msg("Connecting to AWS Secrets Manager")
7981
return &AWSSecretsManager{

0 commit comments

Comments
 (0)