Skip to content

Contradictory documentation on usage of ec2/imds GetMetadata #1173

Closed
@saj

Description

@saj

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.documentationThis is a problem with documentation.p3This is a minor priority issuequeuedThis issues is on the AWS team's backlogsEffort estimation: small

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions