DescribeInstances.go - AWS Code Sample

DescribeInstances.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) // GetInstances retrieves information about your Amazon EC2 instances. // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // Output: // If success, a list of Amazon EC2 instances and nil // Otherwise, nil and an error from the call to DescibeInstances func GetInstances(sess *session.Session) (*ec2.DescribeInstancesOutput, error) { svc := ec2.New(sess) result, err := svc.DescribeInstances(nil) if err != nil { return nil, err } return result, nil } func main() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) result, err := GetInstances(sess) if err != nil { fmt.Println("Got an error retrieving information about your Amazon EC2 instances:") fmt.Println(err) return } for _, r := range result.Reservations { fmt.Println("Reservation ID: " + *r.ReservationId) fmt.Println("Instance IDs:") for _, i := range r.Instances { fmt.Println(" " + *i.InstanceId) } fmt.Println("") } }