amolgautam
amolgautam

Reputation: 967

GO-AWS-SDK to access instance information on AWS

I am referring to the following function in the aws-go SDK : https://docs.aws.amazon.com/sdk-for-go/api/aws/client/#New

I am new to aws / go SDK for aws ( more like a weekend project )

I want to create a client on my localhost machine such that it can login on my AWS account and give me a list of all instances running on the aws account.

Right now , i am just trying to log in to the account

source code :

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/client"
    "github.com/aws/aws-sdk-go/aws/client/metadata"
    "github.com/aws/aws-sdk-go/aws/request"
)


func main() {

    param1:=aws.Config{Region: aws.String("us-east-1") }
    param2:=metadata.ClientInfo{}
    param3:=request.Handlers{}
    awsClient :=client.New(param1,param2,param3)
    fmt.Print(awsClient)
    fmt.Println(awsClient.ClientInfo)

}

output:

&{{3 0s 0s 0s 0s} {        } {<nil> <nil> <nil> <nil> <nil> 0xc0000133a0 <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>  } {{[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>} {[] <nil>}}}{        }

i have my .aws/credentials file correctly set up I can also login via cli and view all my sub accounts.

Again, i am just trying to login to my account on aws.

Please help me in following: 1) Am i using the correct API / documentation ?

2) What i understand from this output is that , the client can not communicate with aws. Am i correct ?

3) please help me resolve this issue

Thanks in advance.

Upvotes: 4

Views: 1426

Answers (1)

Geeshan
Geeshan

Reputation: 534

  1. Check if the IAM user setup in your credential file have access to list EC2 information.

  2. Test out this code which simply check for success or fail

package main

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

   "fmt"
)

func main() {
   // Load session from shared config
   sess := session.Must(session.NewSessionWithOptions(session.Options{
       SharedConfigState: session.SharedConfigEnable,
   }))

   // Create new EC2 client
   ec2Svc := ec2.New(sess)

   // Call to get detailed information on each instance
   result, err := ec2Svc.DescribeInstances(nil)
   if err != nil {
       fmt.Println("Error", err)
   } else {
       fmt.Println("Success", result)
   }
}

The developer guide is a good place to start as well if you are new to AWS/Go.

Upvotes: 1

Related Questions