Passing Credentials from Variables to AWS SDK Version 2
This inquiry echoes a previous question regarding using AWS SDK with credentials from variables. However, in this case, SDK version 2 is utilized, which eliminates the Session feature.
To establish a new client with credentials obtained from variables for accessing the IAM service, consider the following function:
func getIAMClient(ctx context.Context) (*iam.Client, error) {
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("no-region"))
if err != nil {
return nil, errors.Wrap(err)
}
cfg.HTTPClient, err = getHTTPClient(ctx)
if err != nil {
return nil, err
}
return iam.NewFromConfig(cfg), nil
}
Since multiple users may employ the application simultaneously, utilizing ENV files is impractical. However, documentation explaining how to pass these credentials to a client may not be readily available.
Solution: Static Credentials Provider
To resolve this issue, the StaticCredentialsProvider can be utilized, as outlined in the "Static Credentials" section of the AWS SDK for Go V2 documentation:
cfg, err := config.LoadDefaultConfig(ctx, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("AKID", "SECRET_KEY", "TOKEN")))
By incorporating this modification, credentials may be passed from variables to the SDK, enabling the retrieval and use of IAM services.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3