@@ -14,7 +14,7 @@ import (
14
14
)
15
15
16
16
// returns only instance IDs of unprotected ec2 instances
17
- func getInstanceDetails (svc * ec2.Client , output * ec2.DescribeInstancesResponse , region string , logger * log.Entry ) ([]* types.Resource , error ) {
17
+ func getInstanceDetails (svc * ec2.Client , output * ec2.DescribeInstancesOutput , region string , logger * log.Entry ) ([]* types.Resource , error ) {
18
18
var ec2Instances []* types.Resource
19
19
logger .Debug ("Fetching EC2 Details" )
20
20
for _ , reservation := range output .Reservations {
@@ -42,14 +42,12 @@ func getInstanceDetails(svc *ec2.Client, output *ec2.DescribeInstancesResponse,
42
42
// GetAllEC2Instances Get all instances
43
43
func GetAllEC2Instances (cfg aws.Config , region string , logger * log.Entry ) ([]* types.Resource , error ) {
44
44
logger .Debug ("Fetching EC2 Instances" )
45
- svc := ec2 .New (cfg )
45
+ svc := ec2 .NewFromConfig (cfg )
46
46
params := & ec2.DescribeInstancesInput {}
47
47
48
48
// Build the request with its input parameters
49
- req := svc .DescribeInstancesRequest ( params )
49
+ resp , err := svc .DescribeInstances ( context . Background (), params )
50
50
51
- // Send the request, and get the response or error back
52
- resp , err := req .Send (context .Background ())
53
51
if err != nil {
54
52
return nil , err
55
53
}
@@ -63,12 +61,12 @@ func GetAllEC2Instances(cfg aws.Config, region string, logger *log.Entry) ([]*ty
63
61
64
62
// StopEC2Instances Stop Running Instances
65
63
func StopEC2Instances (cfg aws.Config , instances []* types.Resource , logger * log.Entry ) error {
66
- svc := ec2 .New (cfg )
67
- var instanceIds []string
64
+ svc := ec2 .NewFromConfig (cfg )
65
+ var instanceIds []* string
68
66
69
67
for _ , instance := range instances {
70
68
if instance .IsActive () {
71
- instanceIds = append (instanceIds , instance .UUID )
69
+ instanceIds = append (instanceIds , & instance .UUID )
72
70
}
73
71
}
74
72
@@ -82,8 +80,7 @@ func StopEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.E
82
80
InstanceIds : instanceIds ,
83
81
}
84
82
85
- req := svc .StopInstancesRequest (params )
86
- resp , err := req .Send (context .Background ())
83
+ resp , err := svc .StopInstances (context .Background (), params )
87
84
// TODO Attach error to specific instance where the error occurred if possible
88
85
if err != nil {
89
86
fmt .Println (resp , err )
@@ -93,12 +90,12 @@ func StopEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log.E
93
90
94
91
// ResumeEC2Instances Resume Stopped instances
95
92
func ResumeEC2Instances (cfg aws.Config , instances []* types.Resource , logger * log.Entry ) error {
96
- svc := ec2 .New (cfg )
97
- var instanceIds []string
93
+ svc := ec2 .NewFromConfig (cfg )
94
+ var instanceIds []* string
98
95
99
96
for _ , instance := range instances {
100
97
if instance .IsStopped () {
101
- instanceIds = append (instanceIds , instance .UUID )
98
+ instanceIds = append (instanceIds , & instance .UUID )
102
99
}
103
100
}
104
101
@@ -111,23 +108,22 @@ func ResumeEC2Instances(cfg aws.Config, instances []*types.Resource, logger *log
111
108
}
112
109
logger .Debug ("Starting EC2 Instances " , instanceIds , " ..." )
113
110
114
- req := svc .StartInstancesRequest (params )
115
- resp , err := req .Send (context .Background ())
111
+ resp , err := svc .StartInstances (context .Background (), params )
116
112
// TODO Attach error to specific instance where the error occurred if possible
117
113
if err != nil {
118
114
fmt .Println (resp , err )
119
115
}
120
116
return err
121
117
}
122
118
123
- // StartEC2Instances Start Stopped instances
119
+ // TerminateEC2Instances Shutdown instances
124
120
func TerminateEC2Instances (cfg aws.Config , instances []* types.Resource , logger * log.Entry ) error {
125
- svc := ec2 .New (cfg )
126
- var instanceIds []string
121
+ svc := ec2 .NewFromConfig (cfg )
122
+ var instanceIds []* string
127
123
128
124
for _ , instance := range instances {
129
125
if instance .IsStopped () || instance .IsActive () {
130
- instanceIds = append (instanceIds , instance .UUID )
126
+ instanceIds = append (instanceIds , & instance .UUID )
131
127
}
132
128
}
133
129
@@ -141,8 +137,7 @@ func TerminateEC2Instances(cfg aws.Config, instances []*types.Resource, logger *
141
137
InstanceIds : instanceIds ,
142
138
}
143
139
144
- req := svc .TerminateInstancesRequest (params )
145
- resp , err := req .Send (context .Background ())
140
+ resp , err := svc .TerminateInstances (context .Background (), params )
146
141
// TODO Attach error to specific instance where the error occurred if possible
147
142
if err != nil {
148
143
fmt .Println (resp , err )
0 commit comments