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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠