How to Pass Credentials from Variables to AWS SDK Version 2 for IAM Service Access?

Patricia Arquette
Release: 2024-10-31 09:41:29
Original
467 people have browsed it

How to Pass Credentials from Variables to AWS SDK Version 2 for IAM Service Access?

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:

<code class="go">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
}</code>
Copy after login

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:

<code class="go">cfg, err := config.LoadDefaultConfig(ctx, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("AKID", "SECRET_KEY", "TOKEN")))</code>
Copy after login

By incorporating this modification, credentials may be passed from variables to the SDK, enabling the retrieval and use of IAM services.

The above is the detailed content of How to Pass Credentials from Variables to AWS SDK Version 2 for IAM Service Access?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!