Q: How do I run AWS SDK v2 with credentials from variables?
To leverage SDK v2 without using the legacy Session class, you can create a new client and pass your credentials as variables. Consider this getIAMClient function for the IAM service:
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
}
// Use the StaticCredentialsProvider to pass credentials from variables.
cfg.Credentials = credentials.NewStaticCredentialsProvider("AKID", "SECRET_KEY", "TOKEN")
return iam.NewFromConfig(cfg), nil
}
This approach allows multiple users to utilize the application simultaneously without relying on environment variables.
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