Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

amazon web services - How to assume role with the new AWS GO SDK-V2 for cross account access

Following the GO SDK-v2 RC last Dec.24th, I have no idea how to create a config to assume a role in a different aws account. I couldn't find any doc or example and tried with the 'config.WithAssumeRoleCredentialsOptions' or with the 'stscreds.NewAssumeRoleProvider' without any result. Does anyone have an example or pointers for this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's the way to do it:

ctx := context.TODO()
        cfg, err := config.LoadDefaultConfig(ctx,
            config.WithRegion("us-east-1"),
            //config.WithClientLogMode(aws.LogSigning),
        )
        if err != nil {
            log.Fatal(err)
        }
        stsClient := sts.NewFromConfig(cfg)
        provider := stscreds.NewAssumeRoleProvider(stsClient, roleARN)
        cfg.Credentials = aws.NewCredentialsCache(provider)
// without the following, I'm getting an error message: api error SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided.
    creds, err := cfg.Credentials.Retrieve(context.Background())
    if err != nil {
        log.Fatal(err)
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...