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 Antwort
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
SUPPORT-TECHNIKER
Yash_C
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen