"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Use AWS SDK v2 with Credentials from Variables?

How to Use AWS SDK v2 with Credentials from Variables?

Published on 2024-11-07
Browse:211

How to Use AWS SDK v2 with Credentials from Variables?

Running AWS SDK v2 with Credentials from Variables

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.

Latest tutorial More>

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