forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DescribeAddresses.go
65 lines (57 loc) · 2.05 KB
/
DescribeAddresses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ec2.go.describe_addresses]
package main
// snippet-start:[ec2.go.describe_addresses.imports]
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// snippet-end:[ec2.go.describe_addresses.imports]
// GetAddresses gets the Elastic IP addresses for the account's VPC
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// Output:
// If success, the list of addresses and nil
// Otherwise, nil and an error from the call to DescribeAddresses
func GetAddresses(sess *session.Session) (*ec2.DescribeAddressesOutput, error) {
// snippet-start:[ec2.go.describe_addresses.call]
svc := ec2.New(sess)
result, err := svc.DescribeAddresses(&ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("domain"),
Values: aws.StringSlice([]string{"vpc"}),
},
},
})
// snippet-end:[ec2.go.describe_addresses.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[ec2.go.describe_addresses.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[ec2.go.describe_addresses.session]
result, err := GetAddresses(sess)
if err != nil {
fmt.Println("Got an error retrieving the Elastic IP addresses")
return
}
// snippet-start:[ec2.go.describe_addresses.display]
for _, addr := range result.Addresses {
fmt.Println("IP address: ", *addr.PublicIp)
fmt.Println("Allocation ID:", *addr.AllocationId)
if addr.InstanceId != nil {
fmt.Println("Instance ID: ", *addr.InstanceId)
}
}
// snippet-end:[ec2.go.describe_addresses.display]
}
// snippet-end:[ec2.go.describe_addresses]