aws-sdk-go-v2
aws-sdk-go-v2 copied to clipboard
Contradictory documentation on usage of ec2/imds GetMetadata
https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/feature/ec2/[email protected]#Client.GetMetadata
The content will be returned as a string
The function returns a value of type *GetMetadataOutput
. There is no field of string
type to be found in GetMetadataOutput
.
https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/ec2-imds/
includes the following example snippet:
localip, err := client.GetMetadata(context.TODO(), &imds.GetMetadataInput{
Path: "local-ipv4",
})
if err != nil {
log.Printf("Unable to retrieve the private IP address from the EC2 instance: %s\n", err)
return
}
fmt.Printf("local-ip: %v\n", localip)
However, as already established, because the return value of GetMetadata()
is not a string, this example will not produce the expected result:
local-ip: &{0xc000092140 {map[{}:{[{<nil> false false {map[]}}]}]}}
If the caller is responsible for closing GetMetadataOutput.Content
, that obligation should probably be stated in the documentation.
The documentation for other functions, like GetUserData()
, may also need to be amended.
Version of AWS SDK for Go?
github.com/aws/aws-sdk-go-v2/config v1.1.2
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.0.3
Version of Go (go version
)?
go version go1.16.1 darwin/amd64
Steps to reproduce
Here is an example that works:
package main
import (
"context"
"fmt"
"io"
"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.Fatal(err)
}
client := imds.NewFromConfig(cfg)
res, err := client.GetMetadata(context.TODO(), &imds.GetMetadataInput{
Path: "instance-id",
})
if err != nil {
log.Fatal(err)
}
defer res.Content.Close()
id, err := io.ReadAll(res.Content)
if err != nil {
log.Fatal(err)
}
fmt.Printf("instance-id: %s\n", id)
}
Thanks for bring this to our attention @saj, we will get this updated in our next documentation refresh.
We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.