forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ami.go
273 lines (233 loc) · 9.01 KB
/
ami.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package aws
import (
"fmt"
"sort"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/terratest/modules/logger"
)
// These are commonly used AMI account IDs.
const (
CanonicalAccountId = "099720109477"
CentOsAccountId = "679593333241"
AmazonAccountId = "amazon"
)
// DeleteAmiAndAllSnapshots will delete the given AMI along with all EBS snapshots that backed that AMI
func DeleteAmiAndAllSnapshots(t *testing.T, region string, ami string) {
err := DeleteAmiAndAllSnapshotsE(t, region, ami)
if err != nil {
t.Fatal(err)
}
}
// DeleteAmiAndAllSnapshotsE will delete the given AMI along with all EBS snapshots that backed that AMI
func DeleteAmiAndAllSnapshotsE(t *testing.T, region string, ami string) error {
snapshots, err := GetEbsSnapshotsForAmiE(t, region, ami)
if err != nil {
return err
}
err = DeleteAmiE(t, region, ami)
if err != nil {
return err
}
for _, snapshot := range snapshots {
err = DeleteEbsSnapshotE(t, region, snapshot)
if err != nil {
return err
}
}
return nil
}
// GetEbsSnapshotsForAmi retrieves the EBS snapshots which back the given AMI
func GetEbsSnapshotsForAmi(t *testing.T, region string, ami string) []string {
snapshots, err := GetEbsSnapshotsForAmiE(t, region, ami)
if err != nil {
t.Fatal(err)
}
return snapshots
}
// GetEbsSnapshotsForAmi retrieves the EBS snapshots which back the given AMI
func GetEbsSnapshotsForAmiE(t *testing.T, region string, ami string) ([]string, error) {
logger.Logf(t, "Retrieving EBS snapshots backing AMI %s", ami)
ec2Client, err := NewEc2ClientE(t, region)
if err != nil {
return nil, err
}
images, err := ec2Client.DescribeImages(&ec2.DescribeImagesInput{
ImageIds: []*string{
aws.String(ami),
},
})
if err != nil {
return nil, err
}
var snapshots []string
for _, image := range images.Images {
for _, mapping := range image.BlockDeviceMappings {
if mapping.Ebs != nil && mapping.Ebs.SnapshotId != nil {
snapshots = append(snapshots, aws.StringValue(mapping.Ebs.SnapshotId))
}
}
}
return snapshots, err
}
// GetMostRecentAmiId gets the ID of the most recent AMI in the given region that has the given owner and matches the given filters. Each
// filter should correspond to the name and values of a filter supported by DescribeImagesInput:
// https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeImagesInput
func GetMostRecentAmiId(t *testing.T, region string, ownerId string, filters map[string][]string) string {
amiID, err := GetMostRecentAmiIdE(t, region, ownerId, filters)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetMostRecentAmiIdE gets the ID of the most recent AMI in the given region that has the given owner and matches the given filters. Each
// filter should correspond to the name and values of a filter supported by DescribeImagesInput:
// https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeImagesInput
func GetMostRecentAmiIdE(t *testing.T, region string, ownerId string, filters map[string][]string) (string, error) {
ec2Client, err := NewEc2ClientE(t, region)
if err != nil {
return "", err
}
ec2Filters := []*ec2.Filter{}
for name, values := range filters {
ec2Filters = append(ec2Filters, &ec2.Filter{Name: aws.String(name), Values: aws.StringSlice(values)})
}
input := ec2.DescribeImagesInput{
Filters: ec2Filters,
Owners: []*string{aws.String(ownerId)},
}
out, err := ec2Client.DescribeImages(&input)
if err != nil {
return "", err
}
if len(out.Images) == 0 {
return "", NoImagesFound{Region: region, OwnerId: ownerId, Filters: filters}
}
mostRecentImage := mostRecentAMI(out.Images)
return aws.StringValue(mostRecentImage.ImageId), nil
}
// Image sorting code borrowed from: https://github.com/hashicorp/packer/blob/7f4112ba229309cfc0ebaa10ded2abdfaf1b22c8/builder/amazon/common/step_source_ami_info.go
type imageSort []*ec2.Image
func (a imageSort) Len() int { return len(a) }
func (a imageSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a imageSort) Less(i, j int) bool {
iTime, _ := time.Parse(time.RFC3339, *a[i].CreationDate)
jTime, _ := time.Parse(time.RFC3339, *a[j].CreationDate)
return iTime.Unix() < jTime.Unix()
}
// mostRecentAMI returns the most recent AMI out of a slice of images.
func mostRecentAMI(images []*ec2.Image) *ec2.Image {
sortedImages := images
sort.Sort(imageSort(sortedImages))
return sortedImages[len(sortedImages)-1]
}
// GetUbuntu1404Ami gets the ID of the most recent Ubuntu 14.04 HVM x86_64 EBS GP2 AMI in the given region.
func GetUbuntu1404Ami(t *testing.T, region string) string {
amiID, err := GetUbuntu1404AmiE(t, region)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetUbuntu1404AmiE gets the ID of the most recent Ubuntu 14.04 HVM x86_64 EBS GP2 AMI in the given region.
func GetUbuntu1404AmiE(t *testing.T, region string) (string, error) {
filters := map[string][]string{
"name": {"*ubuntu-trusty-14.04-amd64-server-*"},
"virtualization-type": {"hvm"},
"architecture": {"x86_64"},
"root-device-type": {"ebs"},
"block-device-mapping.volume-type": {"gp2"},
}
return GetMostRecentAmiIdE(t, region, CanonicalAccountId, filters)
}
// GetUbuntu1604Ami gets the ID of the most recent Ubuntu 16.04 HVM x86_64 EBS GP2 AMI in the given region.
func GetUbuntu1604Ami(t *testing.T, region string) string {
amiID, err := GetUbuntu1604AmiE(t, region)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetUbuntu1604AmiE gets the ID of the most recent Ubuntu 16.04 HVM x86_64 EBS GP2 AMI in the given region.
func GetUbuntu1604AmiE(t *testing.T, region string) (string, error) {
filters := map[string][]string{
"name": {"*ubuntu-xenial-16.04-amd64-server-*"},
"virtualization-type": {"hvm"},
"architecture": {"x86_64"},
"root-device-type": {"ebs"},
"block-device-mapping.volume-type": {"gp2"},
}
return GetMostRecentAmiIdE(t, region, CanonicalAccountId, filters)
}
// GetCentos7Ami returns a CentOS 7 public AMI from the given region.
// WARNING: you may have to accept the terms & conditions of this AMI in AWS MarketPlace for your AWS Account before
// you can successfully launch the AMI.
func GetCentos7Ami(t *testing.T, region string) string {
amiID, err := GetCentos7AmiE(t, region)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetCentos7AmiE returns a CentOS 7 public AMI from the given region.
// WARNING: you may have to accept the terms & conditions of this AMI in AWS MarketPlace for your AWS Account before
// you can successfully launch the AMI.
func GetCentos7AmiE(t *testing.T, region string) (string, error) {
filters := map[string][]string{
"name": {"*CentOS Linux 7 x86_64 HVM EBS*"},
"virtualization-type": {"hvm"},
"architecture": {"x86_64"},
"root-device-type": {"ebs"},
"block-device-mapping.volume-type": {"gp2"},
}
return GetMostRecentAmiIdE(t, region, CentOsAccountId, filters)
}
// GetAmazonLinuxAmi returns an Amazon Linux AMI HVM, SSD Volume Type public AMI for the given region.
func GetAmazonLinuxAmi(t *testing.T, region string) string {
amiID, err := GetAmazonLinuxAmiE(t, region)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetAmazonLinuxAmiE returns an Amazon Linux AMI HVM, SSD Volume Type public AMI for the given region.
func GetAmazonLinuxAmiE(t *testing.T, region string) (string, error) {
filters := map[string][]string{
"name": {"*amzn-ami-hvm-*-x86_64*"},
"virtualization-type": {"hvm"},
"architecture": {"x86_64"},
"root-device-type": {"ebs"},
"block-device-mapping.volume-type": {"gp2"},
}
return GetMostRecentAmiIdE(t, region, AmazonAccountId, filters)
}
// GetEcsOptimizedAmazonLinuxAmi returns an Amazon ECS-Optimized Amazon Linux AMI for the given region. This AMI is useful for running an ECS cluster.
func GetEcsOptimizedAmazonLinuxAmi(t *testing.T, region string) string {
amiID, err := GetEcsOptimizedAmazonLinuxAmiE(t, region)
if err != nil {
t.Fatal(err)
}
return amiID
}
// GetEcsOptimizedAmazonLinuxAmiE returns an Amazon ECS-Optimized Amazon Linux AMI for the given region. This AMI is useful for running an ECS cluster.
func GetEcsOptimizedAmazonLinuxAmiE(t *testing.T, region string) (string, error) {
filters := map[string][]string{
"name": {"*amzn-ami*amazon-ecs-optimized*"},
"virtualization-type": {"hvm"},
"architecture": {"x86_64"},
"root-device-type": {"ebs"},
"block-device-mapping.volume-type": {"gp2"},
}
return GetMostRecentAmiIdE(t, region, AmazonAccountId, filters)
}
// NoImagesFound is an error that occurs if no images were found.
type NoImagesFound struct {
Region string
OwnerId string
Filters map[string][]string
}
func (err NoImagesFound) Error() string {
return fmt.Sprintf("No AMIs found in %s for owner ID %s and filters: %v", err.Region, err.OwnerId, err.Filters)
}