英文:
How to get the public IP of my AWS EC2 instance
问题
我正在尝试使用Go语言获取我的AWS EC2实例的公共IP地址。
func getIpAdrress(svc *session.Session) {
result, err := svc.DescribeAddresses(&ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("TeamSpeakServer"),
Values: aws.StringSlice([]string{"vpc"}),
},
},
})
if err != nil {
fmt.Println("无法获取弹性IP地址,错误信息:%v", err)
}
// 如果有IP地址,则打印出来。
if len(result.Addresses) == 0 {
fmt.Printf("该地区没有弹性IP地址:%s\n", *svc.Config.Region)
} else {
fmt.Println("弹性IP地址:")
for _, addr := range result.Addresses {
fmt.Println()
}
}
}
我希望能够获取正在运行的IP地址和实例,或者通过机器ID获取IP地址,但我整天都在查看AWS文档,目前只有这些内容。
这是我遇到的错误:
svc.DescribeAddresses未定义(*session.Session类型没有DescribeAddresses字段或方法)
英文:
I'm trying to get the public IP address of my AWS EC2 instance using Go.
func getIpAdrress(svc *session.Session) {
result, err := svc.DescribeAddresses(&ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("TeamSpeakServer"),
Values: aws.StringSlice([]string{"vpc"}),
},
},
})
if err != nil {
fmt.Println("Unable to elastic IP address, %v", err)
}
// Printout the IP addresses if there are any.
if len(result.Addresses) == 0 {
fmt.Printf("No elastic IPs for %s region\n", *svc.Config.Region)
} else {
fmt.Println("Elastic IPs")
for _, addr := range result.Addresses {
fmt.Println()
}
}
}
I was hoping to get the IP address and the instance that are running, or maybe pass a machine id to get the IP, but I have been looking at AWS docs all day and this is what i have so far.
This is the error I get:
> svc.DescribeAddresses undefined (type *session.Session has no field or method DescribeAddresses)
答案1
得分: 1
func getDetaislOFAnInstance(sess *session.Session) {
ec2Svc := ec2.New(sess)
result, err := ec2Svc.DescribeInstances(nil)
if err != nil {
fmt.Println("错误", err)
} else {
fmt.Println("成功", result)
}
}
英文:
func getDetaislOFAnInstance(sess *session.Session) {
ec2Svc := ec2.New(sess)
result, err := ec2Svc.DescribeInstances(nil)
if err != nil {
fmt.Println("Error", err)
} else {
fmt.Println("Success", result)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论