amazon-web-servicesgoaws-sdkaws-sdk-go

Unable to use type Instance from aws-sdk-v2 golang module


I am importing the following go module:

import "github.com/aws/aws-sdk-go-v2/service/ec2"

I want to write a simple function that iterates over the instance tags so I am doing the following

func getInstanceDescription(instance *ec2.Instance) string {
    for _, tag := range instance.Tags {
        if *tag.Key == "Name" || *tag.Key == "name" {
            return *tag.Value
        }
    }
    return *instance.InstanceId
}

However, despite the fact that Instance is a type of this package, compiler complains

undefined: ec2.Instance compiler(UndeclaredImportedName)

What am I doing wrong?


Solution

  • You are using the Instance struct wrongly. As the Instance struct is not in ec2 package, it is in the types sub package.

    You should use as

    import "github.com/aws/aws-sdk-go-v2/service/ec2/types"
    
    
    func getInstanceDescription(instance *types.Instance) string {
        for _, tag := range instance.Tags {
            if *tag.Key == "Name" || *tag.Key == "name" {
                return *tag.Value
            }
        }
        return *instance.InstanceId
    }