package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func GetInstances() (*ec2.DescribeInstancesOutput, error) {
sess, _ := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("111111111111", "2222222222222222222222222222222", ""),
Region: aws.String("ap-east-1"),
})
svc := ec2.New(sess)
result, err := svc.DescribeInstances(nil)
if err != nil {
return nil, err
}
return result, nil
}
func main() {
result, err := GetInstances()
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(r) 打印所有返回信息
for _, i := range r.Instances {
fmt.Println(" " + *i.InstanceId + " " + *i.Tags[0].Value+" " + *i.PrivateIpAddress + " " + *i.State.Name + " " + *i.InstanceType)
}
}
}
有疑问加站长微信联系(非本文作者)