pkaramol
pkaramol

Reputation: 19312

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?

Upvotes: -1

Views: 196

Answers (1)

PRATHEESH PC
PRATHEESH PC

Reputation: 1983

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
}

Upvotes: 1

Related Questions