aws-sdk-go-v2
aws-sdk-go-v2 copied to clipboard
Contradictory documentation on usage of ec2/imds GetRegion
https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/ec2-imds/#retrieving-an-instances-region
The function returns a value of type *GetRegionOutput.
https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/ec2-imds/
includes the following example snippet:
region, err := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if err != nil {
log.Printf("Unable to retrieve the region from the EC2 instance %v\n", err)
}
fmt.Printf("region: %v\n", region)
However, this example will not produce the expected result:
region: &{eu-central-1 {map[{}:{[{<nil> false false {map[]}}]}]}}
Here is an example that works:
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("failed to load SDK configuration, %v", err)
}
client := imds.NewFromConfig(cfg)
resp, err := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if err != nil {
log.Printf("Unable to retrieve the region from the EC2 instance %v\n", err)
}
fmt.Printf("region: %v\n", resp.Region)
}
Links
https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/ec2-imds/
AWS Go SDK V2 Module Versions Used
github.com/aws/aws-sdk-go-v2/config v1.15.14 github.com/aws/aws-sdk-go-v2/ec2imds v0.1.5