package main import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) func main() { sess, err := session.NewSessionWithOptions(session.Options{ Config: aws.Config{Region: aws.String("us-west-2")}, }) if err != nil { fmt.Println("Error creating EC2 session:", err) return } svc := ec2.New(sess) resp, err := svc.DescribeInstances(nil) if err != nil { fmt.Println("Error retrieving EC2 instances:", err) return } for _, reservation := range resp.Reservations { for _, instance := range reservation.Instances { fmt.Println("Instance ID:", *instance.InstanceId) for _, tag := range instance.Tags { fmt.Printf("Tag Name: %s, Tag Value: %s\n", *tag.Key, *tag.Value) } fmt.Println("") } } }
package main import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) func main() { sess, err := session.NewSessionWithOptions(session.Options{ Config: aws.Config{Region: aws.String("us-east-1")}, }) if err != nil { fmt.Println("Error creating EC2 session:", err) return } svc := ec2.New(sess) resp, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{ InstanceIds: []*string{ aws.String("i-0123456789abcdef0"), aws.String("i-0123456789abcdef1"), }, }) if err != nil { fmt.Println("Error retrieving EC2 instances:", err) return } for _, reservation := range resp.Reservations { for _, instance := range reservation.Instances { fmt.Println("Instance ID:", *instance.InstanceId) fmt.Println("Private IP Address:", *instance.PrivateIpAddress) fmt.Println("") } } }This example retrieves information about two specific instances in the us-east-1 region (with instance IDs i-0123456789abcdef0 and i-0123456789abcdef1) and prints their instance IDs and private IP addresses to the console.