GO SDK V2 - configure STS client enpoint url

0

Hey, I'm having trouble with using credentials issued with assume role in region ap-southeast-3. https://aws.amazon.com/premiumsupport/knowledge-center/iam-validate-access-credentials/ Following the guide, I was wondering how to configure the STS client endpoint (in Golang)?

This is how I configure the clients at the moment: Enter image description here

1 個回答
1

Hello,

You can leverage the AWS SDK for Go API Reference for STS here [1] and there's also a Dev Guide Github which shows how to configure client endpoints [2].

An example code has also been provided in reference to Dynamo DB for Customizing service client endpoints in the above Dev Guide on Github ->

customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
    if service == dynamodb.ServiceID && region == "us-west-2" {
        return aws.Endpoint{
            PartitionID:   "aws",
            URL:           "https://test.us-west-2.amazonaws.com",
            SigningRegion: "us-west-2",
        }, nil
    }
    // returning EndpointNotFoundError will allow the service to fallback to it's default resolution
    return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})

cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(customResolver))

In the context of STS, you need to specify sts.ServiceID, and to use an sts specific URL / your own custom URL like below -

func main() {
    customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
        if service == sts.ServiceID && region == "us-east-1" {
            return aws.Endpoint{
                PartitionID:   "aws",
                URL:           "https://sts.us-east-1.amazonaws.com",
                SigningRegion: "us-east-1",
            }, nil
        }
        return aws.Endpoint{}, &aws.EndpointNotFoundError{}
    })

    cfg, err := config.LoadDefaultConfig(context.Background(), config.WithRegion("us-east-1"))
    if err != nil {
        panic(fmt.Sprintf("Error configuring AWS: %s", err))
    }

    // test with an api call: //
    simpleTokenService := sts.NewFromConfig(cfg)

    output, err := simpleTokenService.GetCallerIdentity(context.Background(), &sts.GetCallerIdentityInput{})
    if err != nil {
        panic(fmt.Sprintf("Error configuring AWS: %s", err))
    }
    fmt.Printf("%v\n", *output.Account)

}

In case if you still have any questions or concerns regarding the SDK, it's usage or issues, then please feel free to reach out to our Dev team directly by pulling an issue on the Github [3] to discuss your use case.

References:

[1] https://docs.aws.amazon.com/sdk-for-go/api/service/sts/

[2] https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/

[3] https://github.com/aws/aws-sdk-go-v2/issues

profile pictureAWS
支援工程師
Yash_C
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南