skip to Main Content

I try this example:
https://aws.github.io/aws-sdk-go-v2/docs/sdk-utilities/ec2-imds/

and go.mod is using:
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.15

but the localip prints as:
&{{0xc0002d1680} {map[{}:{[{ false false {map[]}}]}]}}

Anyone have this working?

2

Answers


  1. Chosen as BEST ANSWER

    This solves the problem if anyone is interested: https://github.com/natemarks/ec2metadata


  2. The client.GetMetadata function returns a GetMetadataOutput object with a Content element which is the HTTP resp.Body of type io.Reader. That element needs to be additionally converted to a string, e.g., via a function like this

    func copyToString(r io.Reader) (res string, err error) {
        var sb strings.Builder
        if _, err = io.Copy(&sb, r); err == nil {
            res = sb.String()
        }
        return
    }
    

    So, a correct version of the AWS sample should return

    localip_str, err := copyToString(localip.Content)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search