/
aws_ec2.go
72 lines (59 loc) · 1.77 KB
/
aws_ec2.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
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
const (
// We have to explicitly list account owners of the images that we want to look for.
// Otherwise, AWS will give us a massive list that doesn't contain any of the commonly
// used AMIs.
// WellKnownAccountKopeio = kops account.
WellKnownAccountKopeio = "383156758163"
// WellKnownAccountRedhat = Red Hat account.
WellKnownAccountRedhat = "309956199498"
// WellKnownAccountCoreOS = CoreOS account.
WellKnownAccountCoreOS = "595879546273"
// WellKnownAccountAmazonSystemLinux2 = AWS Linux account.
WellKnownAccountAmazonSystemLinux2 = "137112412989"
// WellKnownAccountUbuntu = Ubuntu account.
WellKnownAccountUbuntu = "099720109477"
)
// AMIResult struct represents a query for AWS AMIs. Contains a list
// of AMIs and an error.
type AMIResult struct {
Images []*ec2.Image
Err error
}
// listEC2Images lists images available in AWS EC2.
func listEC2Images() chan AMIResult {
listChan := make(chan AMIResult)
go func() {
defer close(listChan)
awsEC2 := ec2.New(awsSess, &aws.Config{Region: aws.String(awsRegion)})
amiResult := AMIResult{}
kopeioOwner := WellKnownAccountKopeio
redHatOwner := WellKnownAccountRedhat
coreOSOwner := WellKnownAccountCoreOS
awsLinux2Owner := WellKnownAccountAmazonSystemLinux2
// ubuntuOwner := WellKnownAccountUbuntu
// Get a list of images from trusted owners.
images, err := awsEC2.DescribeImages(&ec2.DescribeImagesInput{
Owners: []*string{
&kopeioOwner,
&redHatOwner,
&coreOSOwner,
&awsLinux2Owner,
// &ubuntuOwner,
},
})
if err != nil {
amiResult.Err = err
listChan <- amiResult
return
}
amiResult.Images = images.Images
listChan <- amiResult
return
}()
return listChan
}