'DescribeInstanceTypes fails with access denied for some AWS regions

I'm trying to get the Instance Details with the Amazon go SDK.

However, I'm getting this error for some regions.

AuthFailure: AWS was not able to validate the provided access credentials ...

A search on that error reveals that I need to do something different with the credentials. https://aws.amazon.com/premiumsupport/knowledge-center/iam-validate-access-credentials/

It's not entirely clear what I'm meant to do as there are no examples that I can find that do this in go.

How do I modify the below code to remove the error?

func (service *CloudClusterInfoService) AmazonEksFetchInfo(accessKey, secretAccessKey string) (*AmazonEksInfo, error) {
    log.Debug("[cloud] [message: sending cloud provider info request] [provider: amazon-eks]")

    resolver := endpoints.DefaultResolver()
    partitions := resolver.(endpoints.EnumPartitions).Partitions()
    eksInfo := &AmazonEksInfo{}

    for _, p := range partitions {
        // Get a list of regions where the Amazon EKS service is available
        if eks, ok := p.Services()["eks"]; ok {
            for _, region := range eks.Regions() {
                eksInfo.Regions = append(eksInfo.Regions, portaineree.Pair{
                    Name:  region.Description(),
                    Value: region.ID(),
                })

                log.Infof("Region: %s, URL: %s", region.ID(), endpoint.URL)

                sess, err := session.NewSession(&aws.Config{
                    Region:      aws.String(region.ID()),
                    Credentials: credentials.NewStaticCredentials(accessKey, secretAccessKey, ""),
                })

                if err != nil {
                    log.Errorf("[cloud] [message: failed to create AWS session] [provider: amazon-eks] [region: %s] [error: %s]", id, err)
                    continue
                }

                svc := ec2.New(sess)

                params := &ec2.DescribeInstanceTypesInput{
                    Filters: []*ec2.Filter{
                        {
                            Name:   aws.String("current-generation"),
                            Values: []*string{aws.String("true")},
                        },
                    },
                }

                types, err := svc.DescribeInstanceTypes(params)
                if err != nil {
                    log.Errorf("[cloud] [message: failed to get instance types] [provider: amazon-eks] [region: %s] [error: %s]", id, err)
                    continue
                }

                for _, v := range types.InstanceTypes {
                    fmt.Println("Instance Type: ", *v.InstanceType)
                }
            }

        }
    }

    return eksInfo, nil
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source